Topic "Upload one file with date stamp to SFTP"

Author Message
luisgarza2589
[View user's profile]

Joined: 2016-12-06
Posts: 4
Location: Earth
Hi everyone, I am new to WinSCP and I don't have a clue about scripting or programming. I have been reading how to automate a process I do each morning but I am not getting it. So each morning I have to upload this file that is a .csv into the SFTP but the file name has a date stamp of the date before file_name_20161205 but today is 12/6/2016. After that file has been uploaded it would get verified and the new version would be dropped into another folder in the same SFTP in which we get an email that means it is ready for pick up. I would like to have this script scheduled thru my windows pc unless anyone knows of another way.

I am having a hard time with the script... I am using WinSCP version 5.9.3

Now from what I understand I can use this code %TIMESTAMP-1D#yyyymmdd% and that will subtract one date out of the current date, how do I do it for powershell. However I have no clue how I can tell the script to put this file with the name file_name_20161205 to be on the sftp and how does it know where it is going to grab the file from.



Code:

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $stamp = Get-Date -Format "yyyyMMdd" #how do I do -1 date? "-1dyyyMMdd"?
        $fileName = "file_name_$stamp.csv"
        $remotePath = "/home/user/sysbatch/$fileName"
        $localPath = "T:\prb\$fileName"
 
        #I only need for this script to upload one file, how do I make sure it will only upload that one file and not all of them.
        # Upload files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
        $transferResult = $session.PutFiles("$localPath", "$remotePath", $False, $transferOptions)
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
Location: Prague, Czechia
The %TIMESTAMP% syntax is for a plain scripting. Actually, you can probably do with a plain scripting only for your task:

put T:\prb\file_name_%TIMESTAMP-1D#yyyymmdd%.csv /home/user/sysbatch/

https://winscp.net/eng/docs/guide_automation

If you really want to use PowerShell, use:

$yesterday = (Get-Date).AddDays(-1)
$stamp = $yesterday.ToString("yyyyMMdd")

See https://winscp.net/eng/docs/script_formatting_timestamp_batch_file
luisgarza2589
[View user's profile]

Joined: 2016-12-06
Posts: 4
Location: Earth
Hi Martin, after looking into the script and making some changes. For some reason I don't ever see my file on the ftp can you guide me, the log file says it is transfered...

I have a file called deploy.bat which I double click.
Winscp.com /script=loadfile.txt /log=winscplog.txt


This is my loadfile.txt
open sftp://user:password@example.com/ -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
cd /user/sysbatch/
put T:\prb\file_name_%TIMESTAMP-1D#yyyymmdd%.csv
close



This is my Log file.
. 2017-01-05 17:01:43.371 Using SFTP protocol.
. 2017-01-05 17:01:43.371 Doing startup conversation with host.
> 2017-01-05 17:01:43.371 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2017-01-05 17:01:43.449 Type: SSH_FXP_VERSION, Size: 33, Number: -1
. 2017-01-05 17:01:43.449 SFTP version 3 negotiated.
. 2017-01-05 17:01:43.449 Unknown server extension newline@vandyke.com="\n"
. 2017-01-05 17:01:43.449 We believe the server has signed timestamps bug
. 2017-01-05 17:01:43.449 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
. 2017-01-05 17:01:43.449 Getting current directory name.
. 2017-01-05 17:01:43.449 Getting real path for '.'
> 2017-01-05 17:01:43.449 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2017-01-05 17:01:43.515 Type: SSH_FXP_NAME, Size: 75, Number: 16
. 2017-01-05 17:01:43.515 Real path is '/home/user'
. 2017-01-05 17:01:43.515 Startup conversation with host finished.
< 2017-01-05 17:01:43.515 Script: Active session: [1] user@example.com
> 2017-01-05 17:01:43.515 Script: cd /user/sysbatch/
. 2017-01-05 17:01:43.515 Cached directory change via "/user/sysbatch/" to "/home/user/sysbatch".
. 2017-01-05 17:01:43.515 Getting current directory name.
< 2017-01-05 17:01:43.515 Script: /home/user/sysbatch
> 2017-01-05 17:01:43.515 Script: put T:\prb\file_name_20170104.csv
. 2017-01-05 17:01:43.515 Copying 1 files/directories to remote directory "/home/user/sysbatch"
. 2017-01-05 17:01:43.515 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask:
. 2017-01-05 17:01:43.515 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
. 2017-01-05 17:01:43.515 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2017-01-05 17:01:43.530 File: 'T:\prb\file_name_20170104.csv' [2017-01-05T05:00:15.785Z] [46434]
. 2017-01-05 17:01:43.546 Copying "T:\prb\file_name_20170104.csv" to remote directory started.
. 2017-01-05 17:01:43.546 Binary transfer mode selected.
. 2017-01-05 17:01:43.546 Opening remote file.
> 2017-01-05 17:01:43.546 Type: SSH_FXP_OPEN, Size: 87, Number: 259
< 2017-01-05 17:01:43.640 Type: SSH_FXP_HANDLE, Size: 31, Number: 259
> 2017-01-05 17:01:43.640 Type: SSH_FXP_WRITE, Size: 32764, Number: 774
. 2017-01-05 17:01:43.655 1 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-01-05 17:01:43.655 Type: SSH_FXP_CLOSE, Size: 31, Number: 1284
> 2017-01-05 17:01:43.655 Type: SSH_FXP_SETSTAT, Size: 83, Number: 521
< 2017-01-05 17:01:43.920 Type: SSH_FXP_STATUS, Size: 49, Number: 774
< 2017-01-05 17:01:44.201 Type: SSH_FXP_STATUS, Size: 49, Number: 1030
< 2017-01-05 17:01:44.201 Type: SSH_FXP_STATUS, Size: 40, Number: 1284
< 2017-01-05 17:01:44.201 Status code: 0
. 2017-01-05 17:01:44.201 Preserving timestamp [2017-01-05T05:00:15.000Z]
< 2017-01-05 17:01:44.201 Type: SSH_FXP_STATUS, Size: 40, Number: 521
< 2017-01-05 17:01:44.201 Status code: 0
. 2017-01-05 17:01:44.201 Transfer done: 'T:\prb\file_name_20170104.csv' [46434]
> 2017-01-05 17:01:44.201 Script: close
. 2017-01-05 17:01:44.201 Closing connection.
. 2017-01-05 17:01:44.201 Sending special code: 12
. 2017-01-05 17:01:44.201 Sent EOF message
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
Location: Prague, Czechia
What do you do to "see the file"? How do you check? Show us some proof, e.g. a screenshot or a log file.
luisgarza2589
[View user's profile]

Joined: 2016-12-06
Posts: 4
Location: Earth
Attached you will find the log file.

Thank you!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
Location: Prague, Czechia
You didn't answer my question. The log shows the upload. But it does not show that the file is not in the remote directory after the upload. For example add the ls command after the put.
luisgarza2589
[View user's profile]

Joined: 2016-12-06
Posts: 4
Location: Earth
Martin,

I found out by the owner of the SFTP that any file that gets upload gets hidden automatically. After putting the ls on the script I was able to see that it was in the correct location.

Thank you so much for all your help!
Advertisements

You can post new topics in this forum






Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License