Scheduled batch file not running - windows

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:
Batch not scheduled
WIN 10 task scheduler
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.

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?

Related

How can resume file transfer in WinSCP from where it is broken

i am put command to upload files FTP server and each file like 3-5GB in size. Sometime server is disconnecting in between file transfer and when it reconnects it starts from broken file but it starts as fresh file. Example, if my download completes 99% then it disconnected and it is starting from the beginning of file.
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log="D:\new\winscp_log\winscp_%yymmdd_hhmmss%.log" -rawtransfersettings PreserveTimeDirs=1 /command ^
"option batch abort" ^
"open ftps://***%%2Fsupport#***.com:%Password%#***.sharefileftp.com -hostkey="**" -rawsettings SendBuf=0 SshSimple=0 FtpPingType=0 -passive=on" ^
"put E:\MDR2\FILE_BACKUP /MDR2/FILE_BACKUP -neweronly -speed=0 -resumesupport=on" ^
"exit"
The documentation for the put command shows that there's a -resume option that is valid as long as you are using SFTP or FTP.
put E:\MDR2\FILE_BACKUP /MDR2/FILE_BACKUP -resume -neweronly -speed=0 -resumesupport=on

How to open teraterm SSH/Telnet connection from windows command prompt?

I got a solution to open a teraterm COM connection and run a macro -
"C:\Program Files (x86)\teraterm\ttermpro.exe" /I /C=7 /BAUD=115200 /M="E:\old data\Desktop\TTL\RvR\test.ttl"
However, need help on arguments to be given to open a SSH/Telnet connection to a particular host from cmd using teraterm and run a macro.
here is 100 percent working code for telnet to a remote host ip
send username password and a command on remote machine.
If you want to send any argument x as variable inside u can use VAR %x and %x% as its value
contents of test.ttl file in local folder as follows
start copy from next line by chanimg ip username and password
connect 'IP:23'
pause 2
sendln 'Remoteusername'
pause 2
sendln 'Remotepassword'
pause 2
logopen 'C:\Users\nocbb\test.txt' 0 0
sendln 'command on remote host'
pause 3
sendln 'logout'
logclose
end
copy till above line
Save as all files and test.ttl and
run in dos command
run using command
C:\Users\nocbb>"C:\Program Files (x86)"\teraterm\ttermpro /M=C:\Users\nocbb\test.ttl /nossh
Note Change the path as per installation folder of Teraterm in
ur installation of teraterm folder.

How to redirect Windows cmd output to a text file?

I am trying to monitor the cmd output of a certain executable .exe and use it for another process running at the same time.
The problem is that all cmd redirecting functions ( '>','>>','<' and '|') would only redirect the output after a successful return of the last command.
What I wanted to do is to generate some kind of streaming log of my cmd.
You can run in your process in background by using
start /b something.exe > somefile.txt
Windows 20H2: Redirection works fine when logged in as true administrator but will NOT work when logged in as a created administrative user. I have used it for years to redirect the output of a multi target backup system using Hobo copy to put the console output in a log file. I have never been able to get it to work successfully in Windows 10 ver 19 or 20 on a created administrative user.
You can prefix the command with "cmd /c" to start a new command prompt, and redirect the output of the command prompt:
cmd /c YOUR CODE > TextFileName.txt
Note : Use ONLY single greater than (>)
Since the output of cmd is going to TextFileName.txt.
Whereas this misses the error output, so you are not able to see : Request to UnKnown timed-out for each failed address.

Using ftp.exe to create some kind of log [duplicate]

I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)
I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)

Command Line & and &&

so currently what I am attempting to do is, remote onto a different server, launch a scheduled task, exit, change to a specified folder on my desktop, write a file
My code currently looks as such
C:MyOriginalFolder> psexec \\MYREMOTESERVER -u MYUSERNAME cmd
C:MYREMOTESERVER> SCHTASK.......
C:MYREMOTESERVER> exit & cd C:\\Users\ce132d & echo "Logged off" > MyLog.txt
//expected: the folder C:\\Users\ce132d should have a text file called MyLog.txt
//what happens: I end up in C:MyOriginalFolder with no MyLog.txt file created
When I remove the &'s and test it command by command, all is dandy and the expected behavior happens. But when linking them together with & and &&, the expected behavior does not happen.
So my question is this: is there some way of one-lining the actions of exiting, changing directory, and writing a text file?
I am eventually going to check if loging into the remote server was successful or not, and want to put those 3 actions into a if (successful login) {do 3 tasks} else {write error log}..
You send your remote server exit & cd C:\\Users\ce132d & echo "Logged off" > MyLog.txt.
Why do you expect that the cd ... should have any effect on your local directory?
When you send the commands line by line then your exit will EXIT the remote server context, then you are again on your local machine, therefore the rest of the commands works as expected.
The > MyLog.txt is being evaluated before any of the commands are run, including the cd. You can use > C:\Users\ce132d\MyLog.txt instead.
You can also use >> instead of > to append to a file.

Resources