Search code examples
bashpowershellbatch-filecmdputty

Script automation upload files from PC (Windows) to Linux server then rename them


I would like to automate following manual tasks:

A list of files: a.csv, b.csv, c.csv in a folder named files

  1. Rename these files to a_1.csv, b_1.csv, c_1.csv
  2. Upload these files to a Linux server (may connect via PuTTY with username and password)
  3. After finishing upload, rename them back to a.csv, b.csv, c.csv.

What did I try so far:

  1. Using batch file to rename and then using PuTTY to upload file. But I'm stuck at renaming files back to the originals. I have no ideas how to store the original names.
  2. Using PowerShell but I have no ideas when the upload process finishes.

Could you please give me some ideas to get these tasks done? I did try research a lot but no success.


Solution

  • This is trivial to do, if you use a client that supports masks to modify file name as the file is uploaded.

    For example with WinSCP, you can do:

    put C:\local\path\*.* /remote/path/*_1.csv
    

    It will upload all files, storing them with _1 suffix on the server. And you do not even need to rename the local files.

    For a start, see guide to automating file transfer with WinSCP.

    (I'm the author of WinSCP)