PSFTP rename file after transfer completed - windows

I am transferring files through PSFTP to 3rd party server using Batch files. While transferring files, due to buffering issues, files are being broken/not transferred fully.
As a remedy, 3rd party requested us to name each file with '.new' before starting file transfer and remove '.new' once file is transferred fully/successfully.
Please let me know Batch script commands to implement above. Please let me know if you need additional info.

To rename a file, use mv command (or it's ren alias):
put c:\local\path\file /remote/path/file.new
mv /remote/path/file.new /remote/path/file
Though if you are transferring multiple files using a wildcard, this won't help you.
A relatively simple solution for multiple files is using a temporary upload folder. After the upload finishes, you can move all files at once to the target folder:
mput c:\local\path\* /temp/path
mv /temp/path/* /remote/path
For a similar discussion, see also SFTP file lock mechanism.
If you need to use the solution with extensions, you can use WinSCP, as it allows you to automatically use a temporary file name for upload. Though it uses .filepart, not .new extension.
put -resumesupport=on c:\local\path\* /remote/path/
See WinSCP article on Uploading to temporary file name for more details.
The article also shows (a way more complicated) solution using WinSCP .NET assembly that allows you to use even the .new extension.
If you choose to switch to WinSCP, there's a guide for converting psftp script to WinSCP.
(I'm the author of WinSCP)

Related

FTP - automatically rename/move file on FTP server when it's downloaded

I need to make a script for an FTP server that will detect when a file is downloaded and then either rename or move or delete that file to prevent it from being re-downloaded. Is there a way to do this by storing a file on the FTP server that will always be looking for when a file is downloaded, and then executing that process? I assume it could be done with a bash script, but I dont know enough about them to know if it can be constantly running/checking for if a file is downloaded.
Thanks!

How to create a batch file in Mac?

I need to find a solution at work to backup specific folders daily, hopefully to a RAR or ZIP file.
If it was on PC, I would have done it already. But I don't have any idea to how to approach it on a Mac.
What I basically want to achieve is an automated task, that can be run with an executable, that does:
compress a specific directory (/Volumes/Audio/Shoko) to a rar or zip file.
(in the zip file exclude all *.wav files in all sub Directories and a directory names "Videos").
move It to a network share (/Volumes/Post Shared/Backup From Sound).
(or compress directly to this folder).
automate the file name of the Zip file with dynamic date and time (so no duplicate file names).
Shutdown Mac when finished.
I want to say again, I don't usually use Mac, so things like what kind of file to open for the script, and stuff like that is not trivial for me, yet.
I have tried to put Mark's bash lines (from the first answer, below) in a txt file and executed it, but it had errors and didn't work.
I also tried to use Automator, but it's too plain, no advanced options.
How can I accomplish this?
I would love a working example :)
Thank You,
Dave
You can just make a bash script that does the backup and then you can either double-click it or run it on a schedule. I don't know your paths and/or tools of choice, but some thing along these lines:
#!/bin/bash
FILENAME=`date +"/Volumes/path/to/network/share/Backup/%Y-%m-%d.tgz"`
cd /directory/to/backup || exit 1
tar -cvz "$FILENAME" .
You can save that on your Desktop as backup and then go in Terminal and type:
chmod +x ~/Desktop/backup
to make it executable. Then you can just double click on it - obviously after changing the paths to reflect what you want to backup and where to.
Also, you may prefer to use some other tools - such as rsync but the method is the same.

Creating an executable file to download a file, then upload the file to new location

I'm having trouble finding the correct method to accomplish a relatively simple task
I'm trying to make a simple executable that I can run/schedule to run.
That
1. Downloads a file from an intranet location (192.168.100.112/file.txt)
2. Uploads the new version file to web (fpt.website.com/docs/file.txt)
There are 5 pdf files that auto generate on an intranet and I would like to keep the web versions updated. Ideally create one executable that does all 5 files at once and have the ability to do each one individually.
thanks
Use the windows ftp command. Is has a -s option for providing ftp "scripts". Basically just add all the commands you need to accomplish your task to something.txt for example:
open 192.168.100.112
get file.txt
close
open fpt.website.com
cd docs
put file.txt
close
bye
then do:
ftp -s:something.txt
You could make ftp scripts, one for each upload. Then put all five commands in a batch file

Use system command to download XML file

I am writting expansion programs to a CAD program called 12d Model. The language I write these expansions in is simply called Macro language and it has a very limited API. So it doesn't have a way to find a file on Windows, list all files in a directory or download a file.
To overcome this I use simple ShellExecute and system calls. For example to list all files in a directory I use the system call "dir C:\ /B > C:\MyCurrentFolder\outfile.txt". One of my needs is to download and parse an XML file but the API has no download function.
Is there a system call to download a file from a URL? Is there some native way to do this? Maybe there is a Windows Application like regedit.exe or something that I can use to download a file?
If not, do you think it would be possible to do it through a batch script?
Windows does have built in FTP support using ftp.exe. So if you could find a FTP mirror of the file, or upload it to one yourself, that might work.
Yes there is a native way to do this. Use the msxsl.exe parser to download the xml file and save it to whatever location you would like. You can do this from the command prompt or from a batch file. Note the one caveat to this is you will need to use an xsl file that does not alter the xml file. The command would look something like this...
c:\msxsl.exe c:\myXML.xml c:\myXSLT.xsl > c:\myXML.xml
Note here the file located at c:\myXSLT.xsl cannot change the source file c:\myXML.xml

Change file extension after FTP transfer

I'm transferring a small text file over FTP and trying to change the file extension after the transfer's been completed so the destination server can process the contents, how do I do this, the commands on winscp.net aren't particularly helpful but then again, I may be searching like an idiot.
rename oldname newname
If your client complains, try:
quote site rename oldname newname
This is old, but many FTP sessions won't allow you to change the file AFTER the transfer, as you have no permissions to do anything but dump the files into the FTP directory. Why can't they be changed prior to transfer?
If you want to rename file manually after transfer use Rename command:
https://winscp.net/eng/docs/task_rename
You can also configure WinSCP to transfer all files to temporary name and automatically rename them to final name after transfer finishes:
https://winscp.net/eng/docs/ui_pref_resume
In Python I got this code:
import ftplib
ftp_connect = ftplib.FTP(str_host)
ftp_connect.rename(str_oldName, str_newName)

Resources