WinSCP script skipping files

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
CPKGDevTeamAuthor Commented:
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.
Take a look at this post.  Sounds like the exact same thing you're doing.

Windows Task Scheduler running WinSCP Batch File

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CPKGDevTeamAuthor Commented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.