How to use windows command line to put a file to an SFTP location using Putty?
I have used hybrid approach to solve this problem.

Getting started:

  1. Download : (Putty)
  2. Basic knowledge of command line in Windows (Not must )
  3. You know the server you want to connect to

Step by Step:

  1. Download and install Putty. You can navigate to the path of Putty Installation, mostly it would be C:\Program Files\PuTTY .
  2. Start Command Prompt in windows, short cut (Windows + R), type CMD and hit enter .
  3. Try going to your Putty installation directory :

    cd C:\Program Files\PuTTY

  4. Now since we have validated our installation let’s get to the main task now. Create text file a paste below to it and save the file as .bat , I am saving it as Toolboxupload.bat

    Content of the File  Toolboxupload.bat:
    @ECHO OFF
    ECHO *******************Hello World ************
    cd C:\Program Files\PuTTY\
    psftp -bc @
    PAUSE
    CLS
    EXIT
  5. Double click on the file see the output, it will say as shown below:Enter the password Now

    Now you know you were able to connect to it by entering your password. Lets Put this in this script .

  6. Update the toolbox.bat file as below. Here I am putting the password in display area so that I can copy and paste while starting my activity .
    • Code below :
      @ECHO OFF
      ECHO *******************Hello World ************
      ECHO Copy PASSWORD:
      cd C:\Program Files\PuTTY\
      psftp -bc @
      ECHO.
      PAUSE
      CLS
      EXIT

  7. Output is now :
  8. Now let’s add our action that we want to do on to the SFTP server.
  9. I want to navigate to directory /upload and then upload a file there.
  10. Create another text file and say it myscript.scr (not .txt at the end)
  11. Now let put below code in this file myscript.scr :
    • Code below:
      ls
      cd uploads
  12. Now let’s change the code in your toolbox.bat to :
  13. Code as below :
    @ECHO OFF
    ECHO *******************Hello World ************
    ECHO Copy PASSWORD: ADD YOUR PASSOWRD HERE
    cd C:\Program Files\PuTTY\
    psftp -bc @ -b myscript.scr
    ECHO.
    PAUSE
    CLS
    EXIT
  14. Output you will see after you enter your password
  15. Now you are done add to upload a file to SFTP server add following line in myscript.scr file

    put “
    put “text.csv”

  16. Now run your toolbox.bat , enter your password . all the command from myscript.scr will get executed after you enter you password. Enjoy your put command is now uploading the file to server.

Give some Likes to Authors

whatsq

Support whatsq.com, help community and write on social network.