0

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.

  • Run no matter user connection

I changed the user starting the scheduled task.

  • Mine, BTW, has maximum permissions and when double clicked, it works.

  • I allowed maximum permissions.

  • Modified "Add argument" and "Start in"

  • I set no conditions in the "conditions" tab

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:password@mySite.com -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] xxx@site.com
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] xxx@site.com
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.

Luca
  • 23
  • 5
  • You say that you've **tried with different codes but no-one works**, but have not posted a [mcve] of it! Are we supposed to guess that you haven't `Set` the working directory at the beginning of your script, and that your scheduled task is using `C:\Windows\System32` instead? – Compo May 30 '19 at 10:01
  • we need a relevant part of your batchfile ([MCVE](https://stackoverflow.com/help/minimal-reproducible-example)) and the output of `schtasks /query /tn "YourTaskName" /v /fo list` to be able to help you. – Stephan May 30 '19 at 10:16
  • As said, I can't even run a simple "pause". My bat file contains only a pause. I create a txt file. i write pause then i renamen the file in myscript.bat. If I run it, it starts then tells "press a key to continue. If i schedule it, nothing happens. Same if i write other code before the pause. – Luca May 30 '19 at 13:19
  • @Stephan: what is " the output of schtasks /query /tn "YourTaskName" /v /fo list" ? – Luca May 30 '19 at 13:20
  • Please read [What must be taken into account on executing a batch file as scheduled task?](https://stackoverflow.com/a/41821620/3074564) Your batch file is obviously not designed for running in a different environment than your user's environment. We can't help you without seeing the lines of your batch file. A scheduled task running a batch file runs it usually in background without showing a console window. So a batch file containing just `pause` is of no real help to determine if the scheduled task is configured right or not. Better would be `md C:\Temp & echo %TIME%>C:\Temp\Test.txt`. – Mofi May 30 '19 at 13:25
  • Thanks Mofi, your comment helped me a lot to debug the script. I made some tests and now i know that the problem is inseide the code. I have not found the solution but i advanced by one step. – Luca May 30 '19 at 15:13
  • WinSCP doesn't need much in the way of permissions so it probably isn't that. The working directory *shouldn't* matter since you aren't using any relative paths. I have two suggestions. Have you tried launching WinSCP directly from the task scheduler or is the command line getting in the way? Add logging to you batch by removing the echo off, run from another batch, pipe the output to a text file in the temp directory. – Señor CMasMas May 30 '19 at 15:39
  • If you add `dir H:\MyPath\MyFolder2 >> C:\Temp\Test.txt` to your batch file, what do you get? – Martin Prikryl Aug 26 '19 at 10:15

1 Answers1

1

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:password@mySite.com -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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • This was very helpful! The log says Error retrieving file list for "FromFolder\*.*". System error. Code: 3. Now i know that the problem is in the permissions (even if it's strange because i am running the script using the same user i use when double click) – Luca May 31 '19 at 09:55
  • If you need further assistance, Post a session log file both from scheduled and manual execution. – Martin Prikryl Jun 03 '19 at 05:32