automate adb push returns empty variable - powershell

I want to make a batch file (or PowerShell if you like it better, you can choose. My code is a batch file), to automate file transfer to an Android device
So, the user would be asked the location and name of the original file
Then, the user is asked the location of the destination.
Then, the adb push command is run
The code I have is:
set /p filename="What is the location and name of the file you want to transfer? > "
set /p location="What is the location you want to set it to? > "
set fullmsg=adb push '%filename%' '%loaction%'
echo %fullmsg%
#just echoing out, to make shure it's correct
pause
What happens now is, it outputs:
adb push 'file.txt' ''
So, the location is empty. Also, what if the filename has an apostrophe (') or something like: that's right.txt
Off course, it needs to be in quotes, but what if you have a location like: \\mysharedfolder\text files\ #notice the backslash and the space in between the words text and files
Hope this explanation is good enough, cause I don't know how to explain it well.
Thanks in advance

In PowerShell:
$file = Read-Host 'What is the location and name of the file you want to transfer?'
$location = Read-Host 'What is the location you want to set it to?'
# Showing command about to be run
Write-Host "adb push $file $location"
# pausing before running command
pause
adb push $file $location

Related

Windows Powershell Basic Questions - new user

When trying to open a file with text editor VIM, I am unable to open the file unless VIM (shortcut) is in my current working directory. As an example, I am able to write start firefox to open a firefox window. However, start vim C:\filepath\filename.txt does not work unless a vim shortcut is in my current directory. How do I get around this?
Also, is there a way to have a program execute a file in the current working directory without having to reference the entire file path? For example instead of Start-Process vim C:\Users\User\Desktop\File\file.txt is there an available path shortcut like Start-Process vim ~\file.txt with ~ representing the current working directory?
The OS need to determine the full path of the exe, no matter what.
There's 2 ways that it will happen.
You're calling the executable from it's working directory
The executable location is in the Windows environment variable.
You can view the PATH variable content through this simple statement
$env:Path -split ';' | sort
You sill see that the Firefox path is listed there, but not the one from VIM.
That's why the former can be started by it's executable name and the latter require the full path.
You need to add VIM directory to your PATH variable if you want to be able to call it just by typing vim
Otherwise, if you have restricted access or don't want to edit that variable, you can also set a $vim variable, then invoke it whenever you want to call the executable.
Regarding the second part of your question
Powershell use the dot as a reference to the current directory .\file.txt.
You can also just specify the filename without anything else file.txt.
Both backslash \ & slash / work for filepath so .\file.txt and ./file.txt are both valid ways to reference the file.
Use ..\ to reference the parent directory (e.g. ..\file.txt)
$Vim = "c:\Path\To\Vim.exe"
& $vim "file.txt"
& $vim ".\file.txt"
#Forward slash also work for paths
& $vim "./file.txt"

Linux recycle bin script

I'm creating a recycle-bin script in SH shell linux in three differant scripts, delete, trash and restore.
The first two scripts are working fine; 'Delete' moves the selected file to the recycle-bin while logging a text file called 'trashinfo' which shows the original path location of the file (to be later used in restore) and 'Trash' which removes everything in the recycle-bin.
The 'restore' script should take the logged path name gained in the delete script and return the file to its original location. I've spent more time than I'd like to remember on this and cant get the restore script to work properly!
Below is the script I've written, as far as I can make out I'm grepping for the filename variable in the text file that holds the pathname, eg 'restore testfile', this is then combined with the basename command, the testfile is then moved into the location thats been grepped and combined with the basename.
Anyone have any pointers on where I'm going wrong?
if [ "$*" != -f ]
then
path=grep "$*" /usr/local/bin/trashinfo
pathname=basename "$path"
mv "$path" "$pathname"
path=$(grep "$*" /usr/local/bin/trashinfo)
pathname=$(basename "$path")

Automator: duplicate OR copy in place

I am trying to build the following services:
Change type of image, result in the same folder (image.jpg => image.jpg + image.png)
Change size of image, result in the same folder (image.jpg => image.jpg + image-800x600.jpg)
I am stuck on part where the original image is duplicated in the same folder, under a different name (the copy finder item workflow requires a hard coded destination or other option I am not familiar with).
Maybe I could use a shell script to perform the duplicating part. I know how to get the file paths passed to the run shell script workflow, but I can't figure out how to send valid paths out to the next task (change type or resize).
MAC OS version is Mountain lion 10.8.2.
You can duplicate the files before you scale them:
on run {input}
set newFiles to {}
repeat with aFile in input
tell application "Finder" to set myFile to duplicate aFile
set end of newFiles to myFile as alias
end repeat
delay 1
return newFiles
end run
You can add another AppleScript at the end to deal with the files names:
on run {input}
repeat with myFile in input
tell application "System Events" to set oldName to myFile's name
set newName to do shell script "echo " & quoted form of oldName & " | sed -E 's/ ?copy ?[0-9?]*//'"
tell application "System Events" to set myFile's name to newName
end repeat
end run

I want to prompt a user for the first 5 characters of a file then have it search for the files

I'm trying to write a script that will prompt the user for the first 5 charters of a file name then have it search the directories for any files that start with that. Then I want it to check to see if a folder is created with the file names and if not create one then move the files there. But if there is a directory for it already then to just move the files too the folder.
Break it down step by step:
"prompt the user for the first 5 characters of a file name" -- you can use the shell read command to get the data. Try a simple shell script:
#!/bin/bash
read foo
echo "foo = $foo"
"if a folder is created with the file names" -- you can use find to see if a file exists. for example:
find . -name abcde\*
"But if there is a directory for it already then to just move the files too the folder." -- the command mkdir takes a -p option so that, if the directory already exists, it won't do anything.

Send the result of multiple commands to one text file?

This is what I have so far - my dropbox public URL creation script for a directory of public URLs (getdropbox.com - gpl I think). My LIST file was created using ls in the following fashion:
ls -d ~/Dropbox/Public/PUBLICFILES/* > LIST
dropboxpuburl.sh:
for PATH in `cat LIST`
do
echo $PATH
dropbox puburl $PATH > ~/URLLIST/$PATH
done
Now this creates a whole series of files - each with the dropbox puburl in them.
The question is: How can I cause this script to redirect all the public links into one text file, each on a new line - perhaps with the name PUBLIC-DIRECTORY-LIST?
Is this what you are trying to achieve?
for PATH in `cat LIST`
do
echo $PATH
dropbox puburl $PATH >> filename
done
OK, I've got it working using the suggestions given to me here:
for PATH in `cat LIST`
do
echo $PATH
dropbox puburl $PATH
done > PUBLIC-DIRECTORY-LIST
It creates a list of the directories, and below them the public link. Now it is time to prune the directories for a clean text file of links.
The => creates the files and adds something to the first line. >> appends to it on a new line.
echo txt=>PUBLIC-DIRECTORY-LIST.txt |
echo another text>>PUBLIC-DIRECTORY-LIST.txt
You should use while read with input redirection instead of for with cat filename. Also, in order to avoid variable name conflicts I changed your path variable to lowercase since the shell uses the all-caps one already. It won't affect your interactive shell, but it could affect something in your script.
Also, assuming that you want the lines from your input file to be displayed to the screen as a progress indicator, but not captured in your output file, this echo sends it to stderr.
while read path
do
echo $path >&2
dropbox puburl $path
done < LIST > PUBLIC-DIRECTORY-LIST