Add datestamp to files or create folder with date stamp

Advertisement

jemacc
Donor
Joined:
Posts:
2
Location:
Florida

Add datestamp to files or create folder with date stamp

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

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

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,478
Location:
Prague, Czechia

Re: Add datestamp to files or create folder with date stamp

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:
$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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item
Last edited by martin on 2018-02-15; edited 1 time in total

Reply with quote

jemacc
Donor
Joined:
Posts:
2
Location:
Florida

Check Remote SFtP files

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

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
}

Reply with quote

Advertisement

You can post new topics in this forum