Solved

WinSCP - How to confirm file download before deleting remote file

Posted on 2008-10-29
9
5,148 Views
Last Modified: 2012-05-05
Hi 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!
0
Comment
Question by:swcrook
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 2

Expert Comment

by:Dudge669
ID: 22832479
You will have to split your winscp script into two scripts.  Download your files, check them and then run another winscp script to do the deleting.  One way to check to see if it downloaded completely is to download the file twice, with different names (or into two different folders) and then compare them.  You can use DOS's FC command to compare two files.

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
)

Open in new window

0
 

Author Comment

by:swcrook
ID: 22832861
Ok, I believe I follow what you logic is but I am curious about a few things.

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.
0
 
LVL 2

Expert Comment

by:Dudge669
ID: 22833503
Your question makes sense, just add an extra "DEL" line to the if statement (shown below).  As for how to delete the file from the server on successful download, you have to make a second winscp script that skips the 'GET' lines and just does the 'RM' lines as you had at the bottom of the script you posted in your original question.  Also be sure to remove the 'RM' lines from your first winscp script that is only doing the file downloading now, not the removing.

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
)

Open in new window

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:swcrook
ID: 22833727
I will give it a shot tonight. Thanks for the help.
0
 

Author Comment

by:swcrook
ID: 22834574
Actually, here is where I am confused.

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.com:26
# 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.
0
 
LVL 2

Expert Comment

by:Dudge669
ID: 22834900
First, you can't have a file in DOS/Windows with an asterisk * in the filename.  What name does the file have when it is downloaded?  Is it just -.-.Reject.txt without the asterisks?  You can't have asterisks in filenames or on the FC command line.  It helps to know the exact name of the files you'll be dealing with.  If the filenames change, we will need to code some more logic into the batch file.

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
  )
)

Open in new window

0
 
LVL 2

Accepted Solution

by:
Dudge669 earned 500 total points
ID: 22834913
Oops, I changed the logic while typing that code segment and forgot to take out a section, disregard the code above, here is the correct code:
@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 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
)

Open in new window

0
 

Author Comment

by:swcrook
ID: 22834946
Thank you for your help. I have an acceptable solution that will work for me. I will give you the points because you helped me come to the solution I used, though it isn't exactly as you have stated. Thanks again for your time.
0
 

Author Closing Comment

by:swcrook
ID: 31511185
Thanks for help.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question