WinSCP script skipping files

Posted on 2014-07-16
Last Modified: 2015-11-12
I've made a script that will go out to a hosted FTP site and copy the files located there into a directory on my network. I close the session and then re-open it and move the files to an archive folder.

The issue I face is that from the time that the script is kicked off, until the time that it completes, new files could be added to this directory. When that does happen, they are not copied to my network location but are moved to the archive. Defeating the purpose of what the script is supposed to do.

In turn I need to do a treasure hunt to find the files that have been skipped.

What is the best way to handle that situation? Is there a way to temporary list the files somewhere that have been copied to the network then only move those when the second part of the script is engaged?

Here is the script:

# Make sure to have WinSCP downloaded first! 
# Set login name to diicorp 

# Automatically skips or ignores prompts 
option batch continue

# Allows for overwrites
option confirm off

# Opens session for diicorp
open diicorp

# Sets remote directory as root
cd /root

# Changes local working directory to X:\
lcd X:\

# Sets to ASCII
option transfer ascii 

# Gets multiple .txt files and copies to X:\
mget *.txt X:\

# Disconnects session

# Reopens session
open diicorp 

# Moves remote .txt files to /Archive folder  
mv *.txt /Archive/*.bak 

#Ends session

Open in new window

Question by:CPKGDevTeam
    LVL 1

    Expert Comment

    You could do a FOR loop.  I don't have the exact context handy, but basically for each file it finds, copy it, archive it, then move onto the next.  That way the file is being archived immediately after it is copied and you don't have to worry about any modifications in between the copy and archive processes.  If a file is created while the script is running, it will not get copied, but also not get archived.

    Author Comment

    Thanks for the reply. I will look for examples of using LOOP in WinSCP - but I've only found the examples of lopping the script itself and not files within the script.

    Any examples that you can supply would be most welcomed.

    Thank you.
    LVL 1

    Accepted Solution

    Take a look at this post.  Sounds like the exact same thing you're doing.

    Windows Task Scheduler running WinSCP Batch File

    Author Comment

    The author of that last article is attempting to do the same general process, but not using a loop within the script itself. His/her issue was more with the task scheduler not working. What I need is something that will work like this:

    Open session to hosted ftp site
    enter username/pw

    For each rcv_*.txt file
    Copy rcv_*.txt file using ASCII to network folder F:\netfolder
    Move rcv_*txt file to ftp hosted site subfolder 'archive'
    Next file

    Until no more files in folder

    close session

    Any guidance there? I am trying to look into running it in something other than WinSCP


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    This article will show, step by step, how to integrate R code into a R Sweave document
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now