Windows Batch
--
Questions
--
Followers
Top Experts
I would like to know if there is a way to confirm a file has been successfully downloaded before deleting it on a remote server. I have a very basic batch script (running from console command/scheduled task)that goes out to a remote SSH server and downloads 2 files each night. Currently, I have everything working successfully, but I would like to add a "checks and balances" option to make sure that I have a successful download of the files before WinSCP deletes them via the "rm" command. Also, one of the files that I download isn't always there because it doesn't nee to be generated on the remote side (*-*-.reject.txt), so if it isn't there is there a way to tell WinSCP to ignore that command or is it ok to just leave it as it? Everything seems to work great except I would like to check the files download before deleting.
I know there is probably a better way to do this, so that is why I ask the experts. Here is what I use:
# Automatically answer all prompts negatively not to stall
# the script on errors
option batch on
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect using a password
# open user:password@example.com
# Connect
open xxxxxx@xxxxx.xxxxxxxx.com
# Change remote directory
cd /home/user
# Force binary mode transfer
option transfer binary
# Download file to the local directory d:\
get deer*.txt d:\
get *-*-reject.txt d:\
rm deer*.txt
rm *-*-reject.txt
# Disconnect
close
exit
Thanks all!
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
FC file1.txt file2.txt
if %errorlevel%==0 (
  REM files match ok, run winscp script to delete from server
) else (
  REM files don't match, don't delete from sever
)
1) If I have two scripts, then the first will download the files twice with one file going to one folder and the same file going to a second folder or I can just rename it on the download the second time. With this scenario, and a completed download, how can I code it so that that second file that was used to confirm the completed download ( via FC in DOS ) is deleted. I obviously only need one copy. I havea programmer who processes this file and then archives it. I do not want a second copy of the file sitting in limbo.
2) If and only if the completed download completes I would want to delete both the downloaded second file and the file from the remote server.
I am an infrastructure guy, not a programmer, so please understand I am able to keep up with you most of the time but created code from scratch is something I have to research. If I have asked the questions in a silly fashion, then that is my only defense :)
Thanks for the help.
rm deer*.txt
rm *-*-reject.txt
# Disconnect
close
exit
FC file1.txt file2.txt
if %errorlevel%==0 (
  del file2.txt
  REM files match ok, run winscp script to delete from server
) else (
  REM files don't match, don't delete from sever
)






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I believe you only took into account downloading just one file; whereas, we will possibly have two file to deal with. We will always have one file to download no matter what, in this example "deer*.txt". The second file will only needed to be downloaded if it is actually present. So, here is what I can see needs to happen, but I could be way off base here.
1) Run first script which downloads the files twice. I can successfully do that now and have it renaming on the second download. Here is the script for that:
==========================
# Automatically answer all prompts negatively not to stall the script on errors
option batch on
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect
open gasftp@datatxfr.xpresschex
# Change remote directory
cd /home/user
# Force binary mode transfer
option transfer binary
# Download file to the local directory d:\
get deer*.txt d:\
get *-*-reject.txt d:\
get deer*.txt d:\fileDeer.txt
get *-*-reject.txt d:\fileReject.txt
# Disconnect
close
exit
==========================
My confusion now sets in because with the FC DOS code you have me it us only dealing with on .txt file and comparing that file, so my assumption is that I simply just need to run the set of code twice: once for the "deer*.txt" and once for the "*-.-.reject.txt". So, I would have something like this:
# Confirms if download was successful, if so deletes file downloaded twice
FC d:\deer*.txt d:\fileDeer.txt
if %errorlevel%==0 (
 del fileDeer.txt
 REM files match ok, run winscp script to delete from server
) else (
 REM files don't match, don't delete from sever
)
# Confirms if download was successful, if so deletes file downloaded twice
FC d:\*-*-reject.txt d:\fileReject.txt
if %errorlevel%==0 (
 del fileReject.txt
 REM files match ok, run winscp script to delete from server
) else (
 REM files don't match, don't delete from sever
)
However, when I run this in the first script it doesn't work. I am not sure with the winscp.exe command line understands FC, but I could be wrong. So, where do I place the "FC code" within the script or does that need to be a second script with the deletion script being the third script. Also, I am thinking that doubling the code like I have it probably isn't the most efficient, but I am not a programmer so I don't know. It appears like the code you provided could somehow call the next script (deletion script) if the files actually compare.
Can you give me what I am doing wrong in the scenario as I have explained it? Thanks again.
Second, sorry, I guess I didn't specify where this code goes. Â The code I've been giving you is Windows Batch file ("DOS" / CMD ) code. Â It goes into a batch file with a .CMD or .BAT extension (it doesn't matter which). Â This batch file will call your WINSCP script where I have the REM lines (REM is short for remark - a comment). Â The batch file code (the FC command, if statement, etc) doesn't go in the winscp script, the winscp script gets called FROM the batch file. Â I'll illustrate what else you need to do here...
To test for both files, I'm going to change the logic a bit as well...
@ECHO OFF
REM Put this code into a text file with a .BAT or .CMD extension
 
START /WAIT WINSCP /console /script=DOWNLOADscriptfilename
 
FC d:\deer.txt d:\fileDeer.txt >nul
if %errorlevel%==1 GOTO :EOF(
  REM files don't match, don't delete from sever
) else (
  REM first file matches, now test second...
  FC d:\--reject.txt d:\fileReject.txt >nul
  if %errorlevel%==0 (
    REM files match ok, run winscp script to delete from server
    del d:\fileDeer.txt & del d:\fileReject.txt
    WINSCP /console /script=DELETEscriptfilename
  )
)

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Windows Batch
--
Questions
--
Followers
Top Experts
Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.