I want to know how I could add a condition in a batch file to not overwrite always all files every time I transfer the files from one FTP server to another. Here is my code:
open rs68.lyon.fr.sopra
dbaq60
sopra*
cd "path file to transfer"
prompt
bin
mget *
disconnect
quit
So all I want to do is to test:
If a file exists already in target directory, then don't copy it .
If file does not exist in target directory, then copy it.
Related
This question already has answers here:
Batch file to upload all files in directory to FTP
(2 answers)
Closed 5 years ago.
I need to upload a set of .txt files to an FTP server using a .bat file. So far, I've managed to connect to the FTP server, including the correct directory that I need to put the file into and then disconnect. However, it isn't uploading the files.
In my .bat file, I've got this line to start the process
ftp -s:ftp.txt
Then, in ftp.txt, I've got
open my.ip.address
myUserName
myPassword
binary
cd myDir
cd myDir
put C:\MyFolder\*
quit
It goes to the correct directory when I run the batch file, the output being
OK. Current directory is /myDir/MyFolder
ftp> put C:\MyFolder*
Error opening local file C:\MyFolder..
ftp> quit
Goodbye. You uploaded and downloaded 0 kbytes.
Why is it erroring when trying to upload all files from C:\MyFolder\? Is there another way to upload all of the files from a folder?
put is used for a single file. To upload multiple files, use mput instead.
mput C:\MyFolder\*
You may also want to put a prompt on the line before the mput line so that you aren't prompted to press Y for each file in the folder.
I have an ETL (SSIS) job which creates a file in a folder named as TEST_20170505.csv before it sends the file to an SFTP server.
There would be multiple file in the folder such as, TEST_20170504.csv, TEST_20170503.csv. Currently I am using following sftp script file (File.txt) in a batch file.
lcd E:\localpath\
cd \sftpserverpath\
ascii
put *.csv
bye
This is my .bat file.
sftp -oIdentityFile=E:\sftp\filepath\ssh.ppk -B E:\sftp\filepath\File.txt username#ipaddress
But this will upload all the files in the local path to SFTP server instead of taking the latest/last modified/current date file.
OpenSSH sftp cannot do this on its own. But you can use some fancy batch file construct to select the latest file and then generate an ad-hoc sftp upload script.
Some references:
How do I write a Windows batch script to copy the newest file from a directory?
Windows batch file - Upload only latest file to FTP
Or use some more advanced Windows command-line SFTP client.
For example with WinSCP scripting, it's as easy as using -latest switch in the put command:
open sftp://username#example.com/ -privatekey=ssh.ppk
lcd E:\localpath\
cd \sftpserverpath\
ascii
put -transfer=ascii -latest *.csv
exit
Run the script (upload.txt) like:
winscp.com /script=upload.txt /ini=nul /log=upload.log
You can even have WinSCP generate the script/batch file for you (you just need to add the -latest switch manually).
References:
Uploading the most recent file with WinSCP
Converting OpenSSH SFTP script to WinSCP script
Note that WinSCP uses .ppk format of private key files. While OpenSSH sftp uses PEM format. Even though your key file is named .ppk, it cannot be real .ppk file, otherwise the sftp would reject it. You have probably converted original .ppk file to PEM, but incorrectly kept an original extension. You have to use the original .ppk file with WinSCP (if you do not have it, you can convert the PEM back to .ppk using PuTTYgen).
Also note that WinSCP actually supports the text/ascii mode (-transfer=ascii), while sftp does not (there's no ascii command).
(I'm the author of WinSCP)
The solution is instead of "put *.csv" in your script file, you specify the actual filename you want to upload
I'm using a PSFTP script to upload files to a remote server for processing and there are specific steps that I need to take with file in order for the file to be processed correctly by their system. Specifically, I have to upload the file with a ".u01" in the extension, then rename it to a ".r01". after it has been uploaded.
Is there a way to replace just the extension of the uploaded file? The rest of the filename can't be modified because the filename is corresponds with the contents of the file.
And no, I can't just upload it with a ".r" extension. Tried that already :P
The script is simple. It uploads all files from the "to_upload" directory into "ul" on the remote server:
cd ul
put -r to_upload/
I am trying to set up an automatic download of the files from a FTP directory on a scheduled basis. The solution I have come up with is to create a batch file that will be ran by windows task scheduler. The batch file is as follows (so far)
prompt
open ftp://xx.xx.xx.xx
myuser
mypassword
lcd C:\localdir
cd /remotedir/
mget *
quit
The problem is that this does not get everything in the specified directory. Only the loose files in the directory such as a .zip or .txt file. I have searched the questions here to no avail. So how can I get mget to not only download loose files in the root directory but also download the sub-directories and containing files (keeping the structure intact)?
Just answering this in case anyone else has a similar issue. The ftp.exe was a bust but I found out it is fairly easy using WinSCP just create a scheduled task in task scheduler and use the options section for command line parameters
Instructions for creating download script with WinSCP:
http://winscp.net/eng/docs/guide_automation
Instructions for scheduling WinSCP transfer task:
http://winscp.net/eng/docs/guide_schedule
WinSCP downloads directories recursively by default.
How can I write a MS dos ftp batch file to:
download files from the server to my local pc
remove these files from the server after download
Edit:
So far I have...
Batch file:
ftp.exe -s:ftp.txt
FTP.txt:
open domain.com
usernamehere
passwordhere
cd /httpdocs/store/files
need get, list and delete commands here??
quit
The ftp.exe program can take a sort of script file as input (that example uploads a file, but I guess to get the idea), so you should probably be able to create a script for the commands that you need to carry out, and then have a batch file launch ftp.exe with the appropriate input.
From one of my cuestions in other post.
user-name
user-password
lcd c:\localfolder-where-download
cd remote-folder
mget .
mdelete \\remote-folder\ .
quit
1 & 2 line , your credentials
3 - line local folder where the ftp can download the content
4- line - remote folder if needed
5- line - get al content to your local folder
6- line - delete all content from your remote folder
7- line quit!
you can download ncftpput/ncftpget. With ncftpget, there's option to remove remote files after downloading.