Link to home
Start Free TrialLog in
Avatar of Beny9
Beny9

asked on

Windows Task Scheduler running WinSCP Batch File

Hi everyone,

I'm currently in a bit of a pickle regarding some scripts I have been writing.
I am creating a script that uploads multiple files from an FTP server that my company owns, over to another FTP server that an associate company is in control of. I have successfully transferred files to and from each of the servers via an automated method of using the Windows built-in FTP client and Task Scheduler, but the problem this time is that the nature and sensitivity of the files being transferred means we need to move them over using SFTP, something that Task Scheduler isn't too keen on running properly.

Now I am aware that there is no native SFTP client for Windows, so after a bit of research I decided to install WinSCP and have a crack at automating a script that would do the process that is required.

Here is a little information to explain what the script does...

Server (A) is my FTP server. There is a folder on this server that has 6 files within it, all of which are overwritten once an hour, each day of the week. Lets call this Folder (A).
Server (B) is our associate FTP server. There is a folder on here that the files are to be transferred to. Lets call this Folder (B).
Once the transfer between Server (A) and Server (B) has been completed, the original files held on Server (A) need to be moved from their current location of folder (A) to an archive folder named Folder (A2) on the same server. This is to preserve the integrity of the files.

The script is written using WinSCP to perform the initial transfer. This is then fired using a Batch file that runs the script using certain parameters/commands, and then proceeds to perform an XCOPY command to move the files to an archive folder on the same server.

Now the batch file runs perfectly when firing it manually (double-clicking it), but when setting it up as a scheduled task in Windows Task Scheduler, it fails to run correctly. The task will run the XCOPY command perfectly as I can see the files moving on Server [A] from folder to folder, however it is not performing the WinSCP portion of the script. It's just skipping it out.

Here's the WinSCP script "testscript.txt"...
option batch abort
option confirm off
open sftp://username:password@serverbaddress -hostkey="ssh-rsa 2048 XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"
cd /FolderB
option transfer binary
mput "C:\FTPRoot\FolderA\*"
close
exit

Open in new window


And the batch file that is running the SFTP commands...
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="testscript.txt" /ini="C:\Program Files (x86)\WinSCP\WinSCP.ini" /log="logscript.txt"
xcopy /y "C:\FTPRoot\FolderA\*.xml" "C:\FTPRoot\FolderA2\"

Open in new window


Can anyone point out what may be wrong here?
I have already tried the solution in THIS ARTICLE, however It hasn't seemed to work for me. Even forcing the scheduled task to run by right-clicking it and choosing 'run' gives me the same problem, so there is obviously something that Task Scheduler doesn't like about the batch file.

Any help is greatly appreciated! I'm getting dizzy going round in circles this much...
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

What version of windows are you running this on? I know there are some key differences between them regarding the task scheduler.
Avatar of Beny9
Beny9

ASKER

@jeffld - We're running Windows Server 2008 R2 Standard, with Service Pack 1 installed.
Can you try creating a test batch file to see if the executable is firing?  

I do not have a windows server 2008 to test this on.

I did a simple test to display the help.  I also passed a test argument to my batch program and redirected the output to a text file so I could see the results.  Can you try that on your end?

@echo off
echo ----------------- >test1.txt
echo test parm: %1 >>test1.txt
echo ----------------- >>test1.txt
"c:\program files\winscp\winscp.com" /help >>test1.txt

Open in new window


then run the test schedule and see if it created the test1.txt file and what it contains.

here is what mine contains when I did the test.  I'm testing with the latest version of winscp.

-----------------
test parm: testarg
-----------------
WinSCP, Version 5.5.0 (Build 3839)
Copyright (c) 2000-2013 Martin Prikryl

Usage:
WinSCP [/script=file] [/command command1...] [/parameter param1...]
WinSCP /ini=<inifile> /log=<logfile> /xmllog=<logfile>
WinSCP /help

 /script=      Executes batch script file. If the script does not end with
               'exit' command, normal interactive mode follows.
 /command      Executes list of script commands.
 /parameter    Passes list of parameters to script.
 /ini=         Path to configuration INI-file.
 /log=         Turns on session logging to file.
 /xmllog=      Turns on XML logging to file.
 /help         Prints this usage.

Open in new window

Avatar of Beny9

ASKER

@jeffld - I created the test batch file the same as yours, although changed the executable path from "c:\program files\winscp\winscp.com" to "c:\program files (x86)\winscp\winscp.com" as WinSCP runs in a 32bit environment on Server 2008 R2 so is placed in the "Program Files (X86)" directory instead.

It generated the same help text output file as yours...

----------------- 
test parm:  
----------------- 
WinSCP, Version 5.5.0 (Build 3839)
Copyright (c) 2000-2013 Martin Prikryl

Usage:
WinSCP [/script=file] [/command command1...] [/parameter param1...]
WinSCP /ini=<inifile> /log=<logfile> /xmllog=<logfile>
WinSCP /help

 /script=      Executes batch script file. If the script does not end with
               'exit' command, normal interactive mode follows.
 /command      Executes list of script commands.
 /parameter    Passes list of parameters to script.
 /ini=         Path to configuration INI-file.
 /log=         Turns on session logging to file.
 /xmllog=      Turns on XML logging to file.
 /help         Prints this usage.

Open in new window

So I gather we're running the same version of WinSCP.
ASKER CERTIFIED SOLUTION
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When something like this runs when you run it manually but fails as a scheduled task the first thing to check is which windows account is used to launch the task.  If it is a network folder you need to use a login that has access to the folder.  If it is a mapped drive you might have to use a UNC path or run net use within the task since the mapped drive is associated per login session.

I can't offer any specific advice with WinSCP script but the following Robo-FTP script will do the job described above:
SET local_source = "C:\FTPRoot\FolderA\"
SET local_archive = "C:\data\archive\A2\"
SET remote_address = "serverb.address.com" ;; or use IP address
SET remote_folder = "/FolderB"
SET remote_username = "UserID"
SET remote_password = "Secr3t!"
SET remote_server_type = "SFTP" 

:loop_start
WORKINGDIR local_source
ARCHIVEDIR local_archive
FTPLOGON remote_address /user=remote_username /pw=remote_password /servertype=remote_server_type /trust=all
FTPCD remote_folder
SENDFILE "*" /archive 
FTPLOGOFF 
:loop_done
CRON "@hourly" ;; sleep until the top of the next hour 
GOTO loop_start

Open in new window

It is a loop that runs once every hour.  The idea is that you'd install it as a Windows service so that it is just running all the time and wakes up once per hour.  If you really want to use a scheduled task instead just remove the bottom two lines. This page shows how to run a Robo-FTP script as a Windows service: http://www.robo-ftp.com/help/3.9/service_installer.htm

EDIT: You would need to modify the first 6 rows to match your environment
Avatar of Beny9

ASKER

@jeffld - I added the full file path to the script and logscript sections of the code and it seemed to work yesterday evening before leaving work. I then decided to leave it overnight and check the logfile and the remote server to see if the files had been sent over successfully.

Hurrah! The scheduled task has run as it should have, and has run each line of the script exactly as needed. I should've known to check the file path, but it was something that I assumed wasn't necessary as the script is located in the same folder as the batch file. How strange that it would generate the logscript.txt file perfectly fine, yet wouldn't run the script itself. Many thanks for opening my eyes up to such a simple issue!

@AlexPace - I had been looking at RoboFTP the past couple of days and was tempted to start working on that instead. If I hadn't found a solution to this by the end of the week, I would have switched over to this method.
Avatar of Beny9

ASKER

Spot on! Can't believe I hadn't tried that yet. Much appreciated!