WinSCP won't execute the entire script specified on a command-line - winscp

My very first post but before coming here I did a research on the issues I have. Mostly on WinSCP forums but it came to no fruition.
Using winscp.com, I would like a script to use get command to download multiple files using SFTP. Here is my version:
echo ENTER WEEK NUMBER
set /p input=""
cls
Rem Create folder based on date
set year=%date:~6,4%
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_1
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_2
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_3
c:
cd Program Files (x86)
cd Winscp
WinSCP.com /command ^
"#echo off" ^
"# Connect to server"^
"Open sftp://Uname:pass#999.999.999.999" ^
"# batch off mode" ^
"option batch off" ^
"lcd \\11.111.111.11\Folder1\""Folder 2""\#BFolder3\%year%_WK%input%\User_3" ^
"cd .."^
"cd RemoteFolder/USer_Folder" ^
"get %year%WK%input%.zip"^
"cd .."^
"cd User_2_Folder" ^
"get User2WK%input%.csv"^
"get SAV_LACWK%input%.csv"^
Issue is, the WinSCP executes cd .. after the first get command and it just stops. It won't go any further:
If I copy/paste at the command prompt, it works fine.
Any idea?
Thank you
Donne

All your absolute local paths use a wrong syntax C\: for a drive. The correct syntax is C:\.
mkdir C\:%year%Folder\
mkdir C\:%year%Folder\Data_User
...
WinSCP.com /command ^
....
"lcd C\:%year%Folder\Data_User" ^
You are missing a quote and an escape character (^) after
“# Change remote directory
You are using fancy quotes (“ and ”), instead of the plain double-quotes (") in:
“#Set local directory”^
....
“# Change remote directory
You have a space after the ^ in
"cd .."^
This effectively makes the ^ be ignored (it escapes the space, not the newline).
See WinSCP FAQ Why are some scripting commands specified on WinSCP command-line in a batch file not executed/failing?
If this does not help, we need to see the output of the batch file run (ideally with removed #echo off).

you're missing both the double quote and ^ at the end of the line before
change this:
“# Change remote directory
to:
"# Change remote directory" ^

Related

Fatal: Failed to read password file: open ~/RPI/filename.sec: no such file or directory. But it exist [linux]

Trying to run .sh file. with --password "~/RPI.filename"in it. Should read the filename but getting an error. Even though the file exists
error:
Fatal: Failed to read password file: open ~/RPI/filename.sec: no such file or directory
list of directories and files:
~/RPI$ l
g.json m1/ m2/ filename.sec startm1.sh*
I have used it before and I know the method works but no idea what on earth is happening here.
Maybe your script is running in sh instead of bash ? I think "~" is bash syntax.
There are 1½ issues here...
First, quoting a tilde prevents tilde expansion; compare ls ~/RPI.filename and ls "~/RPI.filename"
=> leave the tilde unquoted if you don't want a literal '~'
Second, is it ~/RPI.filename or ~/RPI/filename?

exiftool system cannot find path

I want to use exiftool to change the names of my files in one directory to the creation time (I'm on Windows 10).
I "installed" exiftool under C:\Windows as recommended.
I also set the path for exiftool.
I created a directory under the path C:\testordner where I copied all
my files into.
I opened the commandline in windows.
When I enter the command: C:\testordner>exiftool . everything works and i get the exif data of all files inside this directory.
When I enter the command: C:\testordner>exiftool IMG_0160.JPG it works too.
After reading the documentation I tried the following command to change the filenames of all my files in the directory to the creationdate:
C:\testordner>exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H.%M.%S%%-c.%%e" .
When I try to run this command I always get the error message: "System cannot find the specified file" (In german: Das System kann die angegebene Datei nicht finden.)
I also tried:
C:\testordner>exiftool '-FileName<CreateDate' -d %Y-%m-%d_%H.%M.%S%%.%%le .
What do I do wrong? I don't get it.
From the exiftool main page, Running in Windows:
"Note that when typing commands in the "cmd.exe" shell, you should use double quotes instead of single quotes as shown in some examples"
Under Windows CMD, change the single quotes to double quotes and your command works correctly.

Conversion of dpn, type commands from windows to bash

I recently try linux (from windows), and I find it difficult to process my following windows command to linux bash.
The windows command was:
set /p cutoff=Set BLAST E-Value Cutoff[1e-]:
for %%F in (*.fa) do program.exe -parameter1 %%F -parameter2_cutoff 1e-%cutoff% -output_file %%~dpnF.fas & type %%F %%~dpnF.fas > %%~dpnF.txt
This script takes a numeric value from user and uses it to run a program in every .fa files on a folder with the desired cutoff. Here %%~dpnF takes only the filename (without file extension). In this very script, I join the content of each input file (.fa) and its generated output (.fas) and finally merge them in final output (.txt). Here, for each Input file, there will be a final output file.
To run it in ubuntu , I try
echo "Set BLAST E-Value Cutoff[1e-]:"
read cutoff
for $f in *.fa; do program -parameter1 $f -parameter2_cutoff 1e-$cutoff -output_file $~dpnF.fas & cat $f $~dpnF.fas > $~dpnF.txt; done
Immediately it shows that linux is not supporting dpn type of command in windows and also the scripts terminates abruptly, showing no output.
Although I understand the different file extensions are not very meaningful in linux, but I have to keep it this way for other programs to process them.
I appreciate any type of help.
Thanks
The sequence %~dpn is used to get:
%~d - The drive
%~p - The path
%~n - The file name
Check the meaning of all expansions here.
The drive has no meaning in Linux. The path, full or partial, could be extracted with the command dirname and the filename could be extracted with the command basename.
The sequence %%~dpn means to get the whole pathname from root (/).
In fact, you do not need that in Linux, if a list of files was created with *.f, the list of files will be relative to the "present working directory" (command pwd), no need to extend them.
And to strip the extension from a filename, use ${f%.*}.
That cuts the string in "$f" at the last dot . and anything that follows *.
Then just add the extension you want: ${f%.*}.fas
Also, the character & has the meaning of "run the previous command in the background", which is not what you want.
And finally, the for $f should be replaced by for f.
This is a cleaner translation:
echo "Set BLAST E-Value Cutoff[1e-]:"
read cutoff
for f in *.fa; do
program -parameter1 "$f" \
-parameter2_cutoff "1e-$cutoff" \
-output_file "${f%.*}.fas"
cat "$f" "${f%.*}.fas" > "${f%.*}.txt"
done

Check if directory exists not working

I have a textfile (qrs.txt) which contains dir names (one per line) and on my server in the same directory as the script I have those folders with corresponding names from the text file.
This is my script:
#!/bin/bash
while read p; do
if [ ! -d "$p" ];
then
echo "ERROR $p" >> log.txt
else
echo "GOOD" >> log.txt
fi
done < qrs.txt
qrs.txt:
1992300000183805
1992300001176204
1992300002145500
1992300003104507
1992300004104902
1992300005133703
1992300006117802
1992300007144501
1992300008172803
1992300009189005
1992300010146307
1992300011151700
1992300012190007
1992300013126802
1992300014111508
1992300015193908
When that if statement is inside the loop it always returns error which is incorrect because I can see the folders exist. When I take it out of the loop and check for just 1, it works fine... When I echo $p on the same line as error, I can see the file name its checking is indeed correct.
What am I missing here..?
EDIT:
Screenshot of qrs.txt in hex mode:
http://i.snag.gy/25mqJ.jpg
RESOLVED!
My qrs.txt was in [dos] format originally but once converted to unix format using ":set ff=unix" the script worked like a charm!
Your script works fine.
I copied your script to my local machine. When I put blh blah in the qrs.txt file, I got ERROR for each time I ran your script. I ran it four times. I changed the blh blah to a valid path and I received GOOD.
The directory 1992300000183805 for instance, may be not be a valid path. You need the fully qualified path name! For example, /home/user/1992300000183805.
ERROR blh blah
ERROR blh blah
GOOD
GOOD
EDIT
Looking at #chepner comments, I recreated your problem:
Open your qrs.txt file in vi or vim. You should see ^M at the end of your lines. To remove the ^M characters at the end of all lines in vi, use:
:%s/^M//g
This should fix your problem. If not, in vim type this:
:set ff=unix
save the file.
Re-open qrs.txt in vim, then run the regex above again, or manually delete the ^M.
Or you can use perl:
perl -pi -e "s/\r/\n/g;" <file>
OK so looking at your provided file it seems those are relative directory names -- as such current directory is very important when you execute the script. Do you execute the script from its own directory or from the parent directory to all the (sub)directories shown in your example?
In other words have you tried:
cd <parent directory>
/path/to/yourscript.sh
?
Not to mention the location of qrs.txt seems to be specified relative rather than absolute path. So if there's no qrs.txt in the current directory I don't think your script would work.

cat using relative paths on mac

I am trying to cat 4 files one directory down to a new file, also one directory down:
cat ./dira/file.txt ./dirb/file.txt ./dirc/file.txt ./dird/file.txt > ./dire/file.txt
I can get this to work from the Terminal, but not in the following:
for i in `ls -d prefix*`
do
cd $i
pwd
  cat ./dira/file.txt ./dirb/file.txt ./dirc/file.txt ./dird/file.txt > ./dire/file.txt
done
where pwd prints the correct directory. I get the error: -bash:  : command not found.
There must be a non-breaking space at the start of one of the lines in your file (easily done by typing option-space by accident during editing). The shell would consider that to be a word and try to run the non-breaking space as a command; this produces the "bash: : command not found" error that you see.

Resources