PSFTP rename file extension - batch-file

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/

Related

How to rename a downloaded file from within the WinSCP script?

I have WinSCP script that downloads a file from the SFTP server to the local machine. How can I rename the file from within the WinSCP script?
The script is being run from Windows machine.
The WinSCP script:
option batch off
option confirm off
open sftp://username:password#servername -hostkey=""
option transfer binary
get -latest "oldname.csv" c:\localfolder
mv c:\localfolder\oldname.csv latestname.csv
exit
I tried renaming using the commands:
rename InitialFileName FinalFileName
then tried using:
mv InitialFileName FinalFileName
But the script is throwing the error:
File not found, Language: en
Tried stuff from this blog
If you want to download the file to a different name, specify the new name directly in the get command:
get "oldname.csv" c:\localfolder\latestname.csv
(Note that when downloading one specific file, the -latest switch is pointless. The latest file out of one file[s] is that one file.)

Uploading files to an FTP server via a batch file [duplicate]

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.

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

How to download files from a txt file containing target list recursively?

I want to download files using ssh commands, there's a text file containing all urls of files I want to download, The problem is that I want these files to be downloaded each one in their respective folder.
For example, Here is some sample links in that txt file:
http://dl.example.com/2016/06/file1.zip
http://dl.example.com/2016/06/softwares/file2.zip
http://dl.example.com/2013/09/anotherfile.mp3
http://dl.example.com/movies/samplemovie.mkv
I want these files to be downloaded in this schema :
root/2016/06/file1.zip
root/2016/06/softwares/file2.zip
root/2013/09/anotherfile.mp3
root/movies/samplemovie.mkv
I mean I want this command to not only download the file, But also create the directory of that file exactly on my server.
How can I do that ?

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.

Resources