Luca
Luca

Reputation: 23

Scheduled batch file not running

I have a .bat file. If I run it manually (double click), it works. If I schedule it, it doesn't.

I tried solutions from similar questions on StackOverflow but none of them is working. Like:

I changed general options.

I changed the user starting the scheduled task.

I'm new to scripting, so it is possible I set something wrong.

That's my code:

@echo off

"C:\Program Files (x86)\WinSCP\winscp.com" /log=winscp.log /ini=nul /command ^
"open sftp://user:[email protected] -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime"

"exit"

If I put md C:\Temp & echo %TIME%>C:\Temp\Test.txt as 2° row, it writes the Test.txt file. If i put it in the end of the file it doesn't. No problems if i run it by double click.

I need the task to run the .bat file just as if I am double clicking it. I don't know if is it possible to open the shell or run it in background, as both of them are good for me. Let me know.

Thanks for suggestions.

Edit_1)

I removed the echo %TIME% >> C:\Temp\Test.txt and kept the "exit" >> C:\Temp\Test.txt .

If I double click my bat file, this is the log:

Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] [email protected]
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Synchronization...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
H:\MyPath\MyFolder2 image.JPG | 617 KB | 50.4 KB / s | binary | 100%

And it works perfectly.

If i run it from schedule (using my same user) this is the log.

Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] [email protected]
batch abort
transfer binary
Comparison...    
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'.
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'.

System error. Code: 3.

The specified path could not be found
(I) nterrupt, (R) etry, (S) top: Stop
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'

System error. Code: 3.

The specified path could not be found

I schedule the bat file using my user and max privileges so it should find the folder in the H:\ disk.

Upvotes: 0

Views: 635

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

WinSCP most probably fails. I would guess that because the batch file working directory is not hat you think and winscp.log cannot be created there. Try using a path like for your debug test: C:\Temp\winscp.log.

The reason why the md C:\Temp & echo %TIME%>C:\Temp\Test.txt does not work at the end of your batch file is the exit command. You probably believe it's interpreted as WinSCP command. But it's not. You are missing ^ after the synchronize command. And additionally, you have a blank space after it. As a consequence, the exit is not part of WinSCP command-line, but it exits the batch file instead. Also the lines have to be indented (though I assume that it's rather due to your post formatting).

Try something like this:

@echo off

md C:\Temp
echo %TIME% > C:\Temp\Test.txt 

"C:\Program Files (x86)\WinSCP\winscp.com" /log=C:\temp\winscp.log /ini=nul /command ^
    "open sftp://user:[email protected] -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
    "option batch" ^
    "option transfer binary" ^
    "synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime" ^
    "exit" >> C:\Temp\Test.txt 

echo %TIME% >> C:\Temp\Test.txt 

See also WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?

Upvotes: 1

Related Questions