Upload only the latest file with WinSCP
Solution 1
You won't be able to do that using WinScp by itself. It's scripts aren't that complex. You'll have to use another scripting language to get the latest file.
For example this batch file:
@echo off
for /f "delims=" %%i in ('dir /b /od c:\files\Completed\*') do set LastFile=%%i
winscp.exe /console /command "option batch on" "open ftp://user:pass@remoteip:port -explicitssl -passive" "put c:\files\Completed\%LastFile% /Test" "exit" /log=c:\winscp_log.txt
Solution 2
WinSCP can actually do it, contrary to the other answer.
The put
command has the the -latest
switch:
put -latest c:\files\Completed\* /Test/
For more details, see WinSCP article Uploading the most recent file.
Related videos on Youtube
molecule
Updated on September 17, 2022Comments
-
molecule over 1 year
I am pretty new at WinSCP. I would like to know if this application will allow me to send only the latest modified file in a local directory to a remote directory. Please note that contents in both folders do not match at any given time - thus, synching is not an option.
I have managed to establish connectivity and "put" a file onto the server. However, I am not able to figure out how I can achieve what I want to achieve.
Here are my commands...
winscp.exe /console /command "option batch on" "open ftp://user:pass@remoteip:port -explicitssl -passive" "put c:\files\Completed /Test" "exit" /log=c:\winscp_log.txt
Any help is greatly appreciated...
-
Martin Prikryl almost 9 yearsSee WinSCP sample Uploading the most recent file.
-
-
molecule almost 14 years@shf301 - hi, this is a very useful tip to know. At the moment, I believe it is doing exactly what it is meant to do. However, the LastFile variable does not seem to capture the entire filename if it contains spaces? Excuse my ignorance but how do i modify it to capture the spaces in the filename? The winscp log error states "The system cannot find the file specified "c:\files\Completed\Copy" but the actual filename is "c:\files\Completed\Copy of text.txt" Appreciate it if you could show me how to manipulate the "for" line
-
shf301 almost 14 yearsI've updated my answer. By default for will split on spaces, the setting the delimiters to nothing ("delims=") it will not break the file name up.
-
molecule almost 14 years@shf301 - thanks so much. This worked exactly as how I wanted it set up. Appreciate your help. If it is not too much to ask, I have another scripting problem I would like your assistance on if possible. I don't want to be rude and paste it here before I asked if you were willing to take a look :) Please let me know. Thanks!
-
molecule almost 14 years@shf301 - excuse my ignorance but what does the %%i do? when i remove @echo off, I actually see that it is passing %i. Reason i'm asking, when i try the script on a different machine, the for loop doesn't keep the filename as how it worked on my PC. the "for" still looks like it splits the spaces and can't recognise them.
-
shf301 almost 14 yearsThe %%i is the variable. Variables in batch files always start with a %. The batch process strips one off, so the for loop only sees a single %. I'm not sure why it isn't working though.
-
molecule almost 14 years@shf301 - looks like when I put the same command on the other machine - i see LastFile=dir /b /od c:\files\Completed* rather than LastFile=filename.ext