Search code examples
batch-filesftpwinscp

Files are not present in SFTP directory after upload using WinSCP


I need a script that will copy files to an SFTP server using WinSCP.

  • Connect
  • Copy files
  • If copy ok then delete local files
  • Disconnect

My txt file so far :

# Automatically answer all prompts negatively not to stall
# the script on errors
option batch on

# Automatically answer all prompts negatively not to stall
# the script on errors
option batch on

# Disable overwrite confirmations that conflict with the previous
option confirm off

# Connect using a password
# open user:[email protected]
# Connect
open sftp://***:***@***.fr/ -hostkey=*

# Force binary mode transfer
option transfer binary

# Interface 1
cd /tracks
lcd "Y:\"

#Copie des données en local
get *.txt

#Envoie de données sur le serveur
put *.*

#Effacement des données
put -delete "Y:\*.txt"

# Interface 2
cd /trackm
lcd "Y:\"

#Copie des données en local
get *.tar-gz*

#Envoie de données sur le serveur
put *.*

#Effacement des données
put -delete "Y:\*.tar-gz*"

#Disconnect
#close

#Exit WinSCP
#exit

My bat file so far :

@echo off
"D:\WinSCP\WinSCP.com" /log="D:\logfile.log" /ini=nul /script="D:\script_test.txt"

So far it doesn't upload the files, but it deletes them.


Solution

  • Your script makes a little sense.

    If you want a simple script that moves all Y:\*.txt files to /tracks and all Y:\*.tar-gz* files to /tracksm, replace all your script after the open command with:

    put -delete Y:\*.txt /tracks/
    put -delete Y:\*.tar-gz* /trackm/
    exit
    

    See documentation of put command.


    Though it seems that although the original script was quite ugly and inefficient, it probably did its job.

    The root problem is that your server probably does some processing with the uploaded files and deletes or moves away the files after they are processed.

    That's quite common behavior with servers that processes file (as opposite to storing files).
    See WinSCP FAQ Why is uploaded file not showing in a remote directory or showing with a different name?