WinSCP - Doing post-processing in a BAT file - batch-file

Here's what I want to do using WinSCP called from a BAT file:
Execute a WinSCP script that uploads files to an FTP site.
Test the success of the upload.
If successful, move the files on the local machine to another folder.
If unsuccessful, do nothing.
I have tried the example batch file shown at the WinSCP site, but after the WinSCP command is executing, the remainder of the bat file is ignored.
See:
https://winscp.net/eng/docs/script_local_move_after_successful_upload

The official example was missing exit command at the end of the WinSCP script.
It should be:
# Connect
open mysession
# Upload the files
put *.*
# Exit WinSCP
exit
Then the batch file will correctly process the other commands:
winscp.com /script=example.txt
if %ERRORLEVEL% neq 0 goto error
echo Upload succeeded, moving local files
move *.* c:\backup\
exit 0
:error
echo Upload failed, keeping local files
exit 1
I have corrected the example.

Related

Batch file to start new javaw process not working as expected

I'm new to writing Windows batch file. I have the following batch file to start a new javaw process and redirect the output of the commands in the batch file to a log file.
#echo OFF
call :sub >start-demo.jar.cmd.log
exit /b
:sub
echo starting demo.jar ..
start "" javaw -jar demo.jar
echo done ..
It's working fine and I can see that the output is redirected correctly to the log file and the javaw process is also started with a new process id when I execute the batch file.
Here is the output redirected to the log file after the batch file is executed.
starting demo.jar ..
done ..
However, when I try to edit or delete the log file after the batch file is executed, it says that the action can't be completed because the file is open in Java(TM) Platform SE binary.
Only after killing the javaw process I'm able to edit or delete the log file.
Not sure why this is happening and how to fix this. My expectation is that once the javaw process is started successfully, I should be able to edit or delete the log file. Is this correct?
My OS is Windows 10 Enterprise 64 bit.

Call WinSCP to check if files on a host exist or not using a batch file - to run everyday

I am trying to automate a call to a remote server using WinSCP which is a redundant everyday task. I want the batch file to call remote machine - provide username and password, and later check for if files exist.
So far I am able to start a WinSCP server but still the batch file does not consume username and password - it is still asking for them and/or providing an error regarding too many arguments.
chdir /d D:\Program Files\WinSCP\
winscp.com 172.18.186.39 username password
Your syntax does not even remotely resemble WinSCP command-line syntax for scripting.
See WinSCP article on Checking file existence using scripting:
#echo off
set REMOTE_PATH=/home/user/test.txt
winscp.com /command ^
"open ftp://username:password#ftp.example.com/" ^
"stat %REMOTE_PATH%" ^
"exit"
if %ERRORLEVEL% neq 0 goto error
echo File %REMOTE_PATH% exists
rem Do something
exit /b 0
:error
echo Error or file %REMOTE_PATH% not exists
exit /b 1
The above example is for FTP protocol. You didn't tell use what protocol are you using. If not FTP, you have to change the open command accordingly.
You can have WinSCP GUI generate the open command for you.
Once you have the batch file ready, use Windows Scheduler to call it regularly.

Check for psftp file upload error in batch file

I am trying to upload a file from Windows server to third party server (I do not know OS etc. of this) through windows batch script. I am using PSFTP to upload files. It was working since a long time but since yesterday while uploading files, I am getting 'Network error: connection timed out' and batch script file control is doing further steps after file uploading step.
My requirement is whenever there is a failure to upload a file through psftp command through batch script, system should not proceed further. It should stop executing further steps.
Please let me know how to do this in Windows batch scripting?
psftp returns exit code 1 on error. So you just check the exit code and act accordingly.
To records errors, just redirect all psftp.exe output to a file.
psftp.exe -b script.txt > script.log 2>&1
if errorlevel 1 (
echo Error
) else (
echo Success
rem Other commands to perform on success
)

SQL Server agent for scheduling SFTP using WinSCP under SSIS

I have a batch script which generates a WinSCP upload script to upload a file to an SFTP location. Now when I run the batch file via command prompt - it runs successfully and loads it. I called the same thru SSIS Execute process task - it runs successfully and loads it. Now when I put the same on SQL Agent - I tried the following two options:
Using Operating System (CmdExec) - cmd.exe /c "\.bat"
Added the SSIS package to SSISDB and added it as a job step.
With both the above options the job showed a successful run. However the file is not uploaded! Any ideas on what is happening?
Here's my batch script:
echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\\<staging path>\
SET scriptPath=\\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
#REM ftpFlags: -explicit
echo deleting uploadScript if it already exists
IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1
echo Generating WINSCP Upload Script
>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%#%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit
echo Launching WINSCP upload
start /wait %winscp% /console /script=%scriptPath%
As you start the WinSCP via the start (why?), the exit code is not propagated to the SSIS. So, you never learn, if the script fails. And it most probably fails.
You also should enable logging, so that you can see what's wrong.
You should use this code to propagate the WinSCP exit code to SSIS and to enable logging:
%winscp% /log=\\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%
(Note that the winscp.com does not have the /console parameter)
Anyway, one clear problem is that you do not specify an expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?
When testing the script, add the /ini=nul parameter to isolate the script from your configuration.
For this and other hints, when debugging WinSCP running under SSIS, see My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
And finally, see WinSCP SFTP Task for SSIS.
Your variable seems set incorrectly. To manage with a space in the path and into the variable you have to put in quotes the whole path or the whole variable.
i.e.
set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
or
set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
Another point, you have to check this file: UploadScript.txt because your script adds new lines rather than remake the file.
change this line to >%scriptPath% echo option batch abort instead of >>%...
Ah, I did not pay attention to the IF EXIST.

batch file stops after cvs checkout

I am just writing a small batch file that should checkout a module and build it using maven. But the batch file exits/stops after running the cvs checkout command. Below is the batch file contents.
call rmdir /S /Q C:\temp\project_folder
call cvs -q -d %CVSROOT% checkout -d C:\temp\project_folder module\workspace\project_folder
call cd C:\temp\project_folder
call mvn clean install
Any idea what I am doing wrong here?
The use of call is for generating a callback to the currently running batch file. If you were to call another batch file without the call command it will transfer control to the new batch file and will not record where it came from. With the call command it will remember what line of the batch file was executed and when the called batch file exits it will continue execution at the next line of the parent file.

Resources