Topic "Add datestamp to files or create folder with date stamp"

Author Message
jemacc
[View user's profile]
Donor
Joined: 2014-10-04
Posts: 2
Location: Florida
Hi the a standard script retrieved from this forum wich work as design. I will like to make some modification but have failed in making it work.
Steps
I connect to and sftp site and upload all files from directory "C:\temp\send\", then I move the files to backup location "C:\temp\Backup\"

This scripts is scheduled to run every day during a specific time or it can also be run manually. What I will like to do is for each day that the scripts run to to create subfolder under "C:\temp\Backup\" with the current day "C:\temp\Backup\20141004" and the files that have uploaded to the sftp site.

option 2
when moving the files from "C:\temp\send\" to "C:\temp\Backup\"
is to add time stamp to all the files, for example, the filenames gytcxs.txt= 20141004_gytcxs.txt
tsystt.trn=20141004_tsystt.trn
yhftdt.bat=20141004_yhftdt.bat

This is the powershell script

Code:

try
{
    # Load WinSCP .NET assembly
    [Reflection.Assembly]::LoadFrom("\\C:\Program Files\WinSCP\WinSCPnet.dll") | Out-Null
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "192.20.xxx.xx"
    $sessionOptions.PortNumber = 22   
    $sessionOptions.UserName = "sample"
    $sessionOptions.Password = "Password$1@"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $localPath = "C:\temp\send\*.*"
        $remotePath = "/home/DEV/"
        $backupPath = "C:\temp\Backup\"
 
        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)
 
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                Write-Host ("Upload of {0} succeeded, moving to backup location" -f
                 $transfer.FileName)
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host ("Upload of {0} failed: {1}" -f
                    $transfer.FileName, $transfer.Error.Message)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}


Any help will be greatly appreciated
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24555
Location: Prague, Czechia
If I understand your question right, all you are missing is appending the timestamp to a $backupPath, and creating that directory, correct?
Well, it's more of PowerShell question than WinSCP.

Anyway, use this:
Code:
$backupPath = "C:\temp\Backup\" + $(Get-Date -f "yyyyMMddHHmmss")
New-Item -ItemType directory -Path $backupPath


References:
https://winscp.net/eng/docs/script_download_timestamped_filename
https://technet.microsoft.com/en-us/library/ee176914.aspx
jemacc
[View user's profile]
Donor
Joined: 2014-10-04
Posts: 2
Location: Florida
Thank for your assistance. The addition work
I have also made some changes but could not get it to Work correctly. I wanted to check the remote files, if the files exit and not upload the files from $localpath and display message to check the remote location but if the file does not exist upload the files and then move the files from the $localpath to the $backupath

Code:
try
{
   # Load WinSCP .NET assembly
    [Reflection.Assembly]::LoadFrom("\\C:\Program Files\WinSCP\WinSCPnet.dll") | Out-Null
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "192.20.xxx.xx"
    $sessionOptions.PortNumber = 22   
    $sessionOptions.UserName = "SAMPLE"
    $sessionOptions.Password = "Password$1@"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:x:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
     
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
       
        $localPath = "C:\temp\SEND\*.*"
        $remotePath = "/home/DEV/"
        $backupPath = "C:\temp\Backup\"+$(Get-Date -f "yyyyMMddHHmmss")
        New-Item -ItemType directory -Path $backupPath
 
        if ($session.FileExists($remotePath))
        {
            Write-Host ("File {0} File exists, Check why the file has not move from remote locatiom" -f $remotePath)
           
            exit 0
        }
        else
        {
            Write-Host ("File {0} does not exist" -f $remotePath)
            $transferResult = $session.PutFiles($localPath, $remotePath)
            Move-Item $transfer.FileName $backupPath
           
            exit 1
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 2
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24555
Location: Prague, Czechia
The Session.FileExists can test for an existence of a single file only. If you need to test existence of any file in a directory, use Session.ListDirectory.
http://winscp.net/eng/docs/library_session_listdirectory
Advertisements

You can post new topics in this forum






Search Site

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