0

I am creating a script file using windows BATCH and then calling it using PSFTP (using Putty from command line to call TransferScript.txt).

The requirement is to get a file from Unix box to windows and then delete it from Unix but after getting the return 0. My code looks like:

TransferScript.txt

lcd E:\Temp 
cd /sap/xx/yy 
get abc.dat 
IF %ERRORLEVEL% EQ 0
rm abc.dat
quit

This doesn't work, however file is getting copied but not getting deleted. Can anyone help?

1 Answers1

1

The command processor for the file is psftp, not cmd.exe. As such, IF is not a recognized command. You can, however, use the ! PSFTP command to pass commands to Windows. This is documented here. Here is a tested working version:

Command: psftp -b script.txt username@ftpserver.com -pw password

#script.txt
!echo Connected. Getting file...
lcd c:\temp 
cd sap/xx/yy
get abc.dat
!IF EXIST c:\temp\abc.dat echo Success
!IF NOT EXIST c:\temp\abc.dat echo Failure
quit

Simply replace echo Success with rm abc.dat or whatever you want to happen if the file exists on the local file system.

Another option would be to do all the logic in an external batch file. Write 2 PSFTP scripts like script_get.txt and script_rm.txt and call the second one only if the file exists (like above).

I know neither of these solutions technically answer your question in that they don't capture an error state from PSFTP but hopefully it's an acceptable workaround.