How can I send files from a specific folder to FTP [duplicate] - ftp

I want to do an automatic file transfer from Windows server to my FTP.
Problem is that file is generated with timestamp in its name (the name is not fixed). So I need to upload always only the last version (newest) of file (based on the actual file timestamp, not timestamp in the name). Is there any way how to do that?
Running under Windows Server 2003. Thank you.

To select the newest file in a Windows batch file, see
How do I write a Windows batch script to copy the newest file from a directory?
Based on that you can create an upload batch file like:
#echo off
FOR /F %%I IN ('DIR C:\source\path\*.* /B /O:D') DO SET NEWEST_FILE=%%I
echo Uploading %NEWEST_FILE%
(
echo open ftp.example.com
echo username
echo password
echo put C:\source\path\%NEWEST_FILE% /target/path/%NEWEST_FILE%
echo bye
) > ftp.txt
ftp.exe -s:ftp.txt
For an easier and more reliable approach, use some more powerful 3rd party FTP client.
For example with WinSCP FTP client, you can use the -latest switch of its put command.
An example batch file (.bat):
winscp.com /ini=nul /command ^
"open ftp://username:password#ftp.example.com/" ^
"put -latest C:\source\path\* /target/path/" ^
"exit"
You can even have WinSCP generate the script/batch file for you (you just need to add the -latest switch manually).
See WinSCP article on Uploading the most recent file.
(I'm the author of WinSCP)

Related

WinSCP script to download files from a list (txt or csv)

I would need help to create a (manual) script to download a bunch of selected files from a designated folder.
All files are in the same folder on the FTP server. The folder holds appr. 80.000 files and I would need to download 1200 of them as set out in a list (e.g. images.txt or images.csv).
I know that it works via get command but don't know which command(s) are needed to call the items in the list, e.g.
001DD1B2-1D73-4C10-A514-BF4EA50A2103.jpg
009F64B8-1458-4238-8B84-D829912D7925.jpg
00B128EF-D6EA-4535-AE36-7AA71AD2E945.jpg
00C30DC0-10FB-4B12-B877-6C6A2E2F9194.jpg
and so on.
The script would be executed manually (no batch needed).
Based on WinSCP article Uploading a list of files, an equivalent batch file for downloading files listed in a .txt file is:
#echo off
(
echo open ftp://user:password#example.com/
echo cd /remote/source/path
echo lcd C:\local\target\path
rem Generate "get" command for each line in list.txt file
for /F %%i in (list.txt) do echo get "%%i"
echo exit
) > script.tmp
winscp.com /ini=nul /log=script.log /script=script.tmp

Upload files generated in last one hour to FTP

I'm trying to upload latest files generated in one hour to a remote server. But the script I wrote is intelligent enough to gather only the latest file in directory and upload it to FTP. Any chance of set the variable type as array and upload the files in the array?
My FTP batch file:
FOR /F %%I IN ('DIR "abcdef*.bac" /B /O:D') DO SET latest_file=%%I
echo user domain/username> ftp.txt
echo password>> ftp.txt
echo cd remotepath>> ftp.txt
echo put %latest_file%>>ftp.txt
echo quit>> ftp.txt
ftp -n -s ftp.txt Servername>ftp_logs.txt
del ftp.txt
It would be difficult to write this in a pure batch file code.
You better use some more powerful tool.
For example with WinSCP FTP client, it's trivial:
winscp.com /ini=nul /log=upload.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"put -filemask=>1H C:\local\path\abcdef*.bac /remote/path/" ^
"exit"
References:
Automate file transfers to FTP server with WinSCP;
Converting Windows FTP script to WinSCP script;
File masks with time constraints.
(I'm the author of WinSCP)

Rename a file on the remote FTP host to the output of a command (Windows)

I'm connecting to an FTP server of my website using cmd.exe (on Windows 7). However, when I want to rename a file on the remote host, I'm able to only rename it to a static text (first word, if there's multiple words separated by space)
What I want to do is renaming the file to the output of a command (dynamic). For example the output of the command date is
The current date is: Sat 06/18/2016
I want the name of the file to be the result of the date command.
Thanks in advance!
Start by collecting the command output to a variable.
Then, use the variable to generate the ftp.exe script on the fly.
FOR /F "tokens=* USEBACKQ" %%F IN (`date /t`) DO (
SET OUTPUT=%%F
)
echo open ftp.example.com>ftp.txt
echo user>>ftp.txt
echo password>>ftp.txt
echo rename current_name %OUTPUT%>>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt

trying to download specific files from ftp recursively and run a command on them

i'm trying to download every file with a certain file name from an ftp location, run a command on it then delete it.
ex: download every file that looks like "*_qwerty.jpeg" from "\some\random\ftp\location" then run a command on it. once command is finished, delete it. i need the script to ignore all other.jpeg files too.
i'm trying to do this in windows CMD or Powershell
does anyone have any suggestions?
The simplest way is to use Ftp.exe and drive it with a parameter file. Use mget to download files with wildcards. Iterate the files with foreach loop and do stuff. Like so,
<commandfile>
lcd <path-to-local-dir>
open <ftp-site>
<username>
<password>
bin
cd <path-to-remote-dir>
prompt
mget <filename-with-wildcards>
close
bye
Using the command file in Powershell is simple enough:
Ftp -s:<commandfile>
foreach($i in gci <path-to-images>) {
<do-something> $i
}
You could do the same with batch scripting as well (remember to use double-% in script, single % in command line).
Ftp -s:<commandfile>
for /f "tokens=*" %j in ('dir /b <path-to-images') do <do-something> %j

To search for a file at FTP

I want to find if a file exists at FTP using if-exist filename -else statement using FTP batch script which is as follows:
ftp.txt open ftp.mysite.com
ftp.txt username
ftp.txt password
ftp.txt if exist filename (echo file exists) else (echo file doesn't exist)
ftp.txt quit
ftp -s:ftp.txt
the if-exist line above does not work.
Is there any other way to search?
Don't do the logic in the FTP script.
Call the ftp.txt script from a batch file. Within your ftp.txt script, just do a GET on your file. If the file is there, it'll be downloaded to the local directory. Otherwise, it won't. After calling the FTP script, check the file's existence in your local directory using standard DOS batch commands, i.e.:
#echo off
:FETCHFILE
ftp -s:ftp.txt
IF EXIST filetocheckfor.txt (
REM the file was there, so do something
) ELSE
echo Trying again...
REM ping "sleep" would go here
GOTO FETCHFILE
)
If you want to build a delay into your retries, perform a "sleep" by pinging a bogus IP address, as described in this post:
http://www.dullsharpness.com/2010/06/14/elapsed-timer-using-pure-ms-dos/

Resources