FTP 'rename' command to move remote files matching wildcard - windows

I'm writing a *.bat file (Windows) in which I use FTP commands to get remote files back on a local machine. The remote directory includes a archive subdirectory in which I want to move the files once they are downloaded on the local machine.
My script in the *.bat file:
ftp -v -i -s:GET_FILES_FTP.txt
My script in GET_FILES_FTP.txt:
open example.com
username
password
lcd S:\
lcd repository/files
mget *.txt
rename *.txt archive/
disconnect
bye
Note that hostname, username and password are not those I use for real!
The TXT files are downloaded properly on the local machine.
The problem is that rename *.txt archive/ is not interpreted and the files do not move to the archive file. I the command window, I get an error message:
directory not found.
I can't find extra information better than this doc.
Any idea on how to move the files?

The rename command of the Windows ftp.exe does not support wildcards.
You would have to dynamically generate a script file based on a list of downloaded files with a separate rename command for each file.
Or use a different command-line FTP client that supports wildcards when renaming/moving.
For example with WinSCP scripting the batch file would be like:
winscp.com /log=winscp.log /command ^
"open ftp://username:password#example.com" ^
"lcd S:\" ^
"lcd repository\files" ^
"get *.txt" ^
"mv *.txt archive/" ^
"exit"
For details see:
mv command
Converting Windows FTP script to WinSCP FTP script
(I'm the author of WinSCP)

Related

WinSCP recursively find all files with certain extension and upload it to FTP server

I need to recursively find *.log files at C:\ and send them to my server using WinSCP. I've experimented with put but it can only send files from a given directory. After that I've tried using cmd's dir to get the list of required files and then send them using WinSCP, but I can't both open connection AND send files: cmd prompt changes to winscp> after I open connection from cmd.
I'd appreciate any help.
Use -filemask switch in put command to upload only files matching a mask:
put -filemask=*.log C:\ /remote/path/
If you want to avoid "uploading" folders that contain no *.log files:
put -filemask=*.log -rawtransfersettings ExcludeEmptyDirectories=1 C:\ /remote/path/

How can I take file names from a text document, find them in windows explorer, then transfer the specific files to another folder?

I have a .txt with 900+ file names of pictures with full directories (ex: 2017/conference/tsd-60545).
My windows explorer is pulling from an ftp for my company, and has over 160K photos in it.
I only need the 900 images in my .txt file.
Is there a way to automate this? Manual is bringing me to a slow death.
This is untested as I do not have an ftp server to test this with now, but this should work.
Create a batch file with a .cmd or .bat extention, ensure your test file is in the same directory, or specify the full path to it in the batch file.
Basically, you echo the entire connection to a file. Run a for loop to read from your FTP file and do a get or put for each entry in the file. Once it is completed, it will run the ftp command and read from the file.
MyFTPscript.cmd
#echo off
echo user username password> ftpto.dat
for /F "tokens=*" %%A in (myfile.txt) do echo get %%A >> ftpto.dat
echo quit>> ftpto.dat
ftp -n -s:ftpto.dat ftp.imagesserver.com
You can test it first to make sure it is actually writing the .dat file without actually ftp'ing anything adding rem before the last command. like this:
rem ftp -n -s:ftpto.dat ftp.imagesserver.com
Once you are happy that the content of the dat file looks ok, you can remove the rem and run the script which will then do everything.
No need to delete the file as it will re-write it each time you run the command file.
This downloads all jpgs from ftp:://ftp.mydomain.com/2017/conference/tsd-60545
Create a file called "commands.ftp" based on the contents of your txt file using a tool like vim, awk, or sed. Then use the windows ftp command with option -s to process it.
commands.ftp
open ftp.mydomain.com
user myusername mypassword
cd mysubdirectory
prompt
mget 2017/conference/tsd-60545/*.jpg
close
quit
batch file
ftp -s:commands.ftp -n

Upload files to SFTP server and delete or archive the source files afterwards

I can't figure out a way to automatically sync files with an SFTP server that will do the following.
Syncs files from local PC to the SFTP
After upload deletes the file on the local PC and keeps SFTP file or
After upload moves the file that has been uploaded to another file for review.
I've tried using Ftpbox but it doesn't have the options.
WinSCP but couldn't find a script that would work.
Remote-Sync but didn't have the options.
If just want to move local files to a remote folder, use the WinSCP put command with the -delete switch.
A full Windows batch file would be like:
winscp.net /log=upload.log /command ^
"open sftp://username:password#example.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^
"put -delete C:\local\path\* /remote/path/" ^
"exit"
Have WinSCP generate the open command or even a complete batch file for you.
If you want to move/archive the local files to another local folder after the upload, it is more complicated.
See the official WinSCP example Moving local files to different location after successful upload.

Windows FTP batch file returns "200 type set to A" on mget command

I am trying to automate a FTP job using a Windows batch file. When the mget *.zip line in the FTP batch file is executed this message is displayed:
200 type set to A
and no files are transferred. How can I resolve this?
The file was working previously before I changed something.
The batch file command is
ftp -n -i -s:FtpCommands.txt not.a.real.ftphost
The commands in the FtpCommand.txt file are
USER UserName
Password
cd /FtpFolder
lcd C:\WorkstationFolder
ascii
mget *.zip
After doing a ls I get the below output
You are trying to download .zip files from a directory, where there are no .zip files. So, nothing is downloaded.
Maybe you wanted to download the .gz files:
mget *.gz
USER UserName Password cd /FtpFolder lcd C:\WorkstationFolder ascii mget *.zip
try to put a 'ENTER' between command, eg:
USER UserName Password
cd /FtpFolder
lcd C:\WorkstationFolder
ascii
mget *.zip

How to write command in windows batch file to copy a zip file from an FTP path to a local folder in my computer

I want to copy a zip file from an FTP path to a local folder in my computer.
My client previously used coreftp.exe for that purpose. But now he ask us to use ftp.exe [default in windows machine, available at C:\Windows\System32\ftp.exe] for that purpose. The ftp is in the format :
ftp://username#ftpserver.address.com
And want to download it to d:\sample\docs folder on my machine.
I want that in a batch file so that I can schedule it through windows task manager.
So could you please help me to write that command on the batch file.
Thanks a lot in advance.
FTP.EXE is capable of executing script files. So you could just put this into your bat file:
#ECHO OFF
CD d:\sample\docs
FTP -v -i -s:C:\some\path\ftpscript.txt
And something like this into your ftpscript.txt:
open ftp://ftpserver.address.com
username
password
cd myfolder
get some_zip_file.zip
disconnect
bye
This will download some_zip_file.zip into the current directory (d:\sample\docs).

Resources