Bat script for SFTP upload of specific extensions only - batch-file

Say I have a folder containing jpg files, mp4 files and some other extensions.
I would like to write a bat script that uploads to SFTP the jpg and mp4 files only and ignores the others.
The bat file is also located inside this folder along with the jpg's and mp4's.
Suppose these are the sftp credentials:
Username:root.
Password: password.
Hostname: sftp://12.345.67.890.
Is there a way to do that?

Any command-line SFTP client can do that.
For example with WinSCP:
winscp.com /command "open sftp://root:password#12.345.67.890" "put *.jpg *.mp4 /target/path/" "exit"
See https://winscp.net/eng/docs/scripting
You can also use psftp.
(From the reference to a .bat file, I assume you are on Windows)
(I'm the author of WinSCP)

Related

WinSCP - Download file from SFTP server to network location, then delete

I need to write a WinSCP script or batch file that will download the contents of a remote SFTP folder, delete contents and then upload to a mapped network folder. I know how to write the login part of the script. But I am confused as to what the specific command line should look like. From what I've read the command script to download and delete the file is something like this:
get -delete -transfer=binary *
The part that I'm not getting is the upload to the network folder command. I understand put is the WinSCP upload command operator. But how do I use it in conjunction with the network folder location files should be uploaded to? Also, can this be scripted out in a batch file or do the batch file and script need to be written separately? Thanks!
An "upload" to a mapped network folder is not a job for WinSCP. A mapped network folder behaves as a local drive. A plain Windows copy command will do. So first do your WinSCP download and then copy (it's not really called an upload) the files to the network drive.
winscp /ini=nul /command ^
"open sftp://user:password#example.com/" ^
"get -delete /sftp/path/* c:\local\drive\path\" ^
"exit"
copy c:\local\drive\path\* n:\mapped\network\drive\path\
Though, if you do not need the files locally, you can have WinSCP download the files from the SFTP folder directly to the mapped network drive:
winscp /ini=nul /command ^
"open sftp://user:password#example.com/" ^
"get -delete /sftp/path/* n:\mapped\network\drive\path\" ^
"exit"

Put only latest file into SFTP server in batch file

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

Downloading new files from FTP server to Windows in a batch file

I am writing a batch file that downloads files from an FTP server.
Now, I want these files to be checked against the existing files, so that they are not being downloaded again. There are multiple files being downloaded and I do not know the name of the files that may be present in the FTP server.
Thanks
Just use any Window command-line FTP client that supports synchronization.
For example with WinSCP FTP client, you can use a batch file like:
#echo off
winscp.com /ini=nul /log=synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"synchronize local C:\local\path /remote/path" ^
"exit"
References:
Guide to automating synchronization from FTP server;
Scripting command synchronize.
You can have WinSCP GUI generate a batch file template for you.
(I'm the author of WinSCP)

Zip File containing couple .xml files and batch file

I want to be able to zip up a couple WLAN.xml files and a batch file.
Batch file will contain the commands to import those WLAN.xml's.
Can this be done directly from the zip file? Or is there a command to copy them from the zip file to a specific location (ie. c:)?
I currently have the WLAN.xml files in C:\ and my batch files specifies the location of it.
NOTE: To do this you would have to download the tool 72a.exe from 7zip, Although most computers nowadays already have it pre-installed
You can just use the following syntax:
7z a -tzip archive.zip -r src*.cpp src*.h
Apply your respective file path to the syntax above, and it should work.

PSFTP rename file extension

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/

Resources