3

I have file named with A.txt, Using batch command to copy the file to SFTP Server. After copying the A.txt, to copy the A.txt again to SFTP on same path.There is no possible same name with two files on same path, I want to rename the second copied file like A2.txt.

How to use the 'New name' command in WinSCP command line?

I copying the multiple files with file extension (*.txt), only I want to rename the file which already exist with same name in SFTP Server.

1 Answer 1

3

The "New name" command in WinSCP GUI cannot automatically rename uploaded file not to conflict with existing files.

Neither there's such a function in WinSCP scripting.


Though you can code this using WinSCP .NET assembly, for example using PowerShell script:

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "example.com"
    $sessionOptions.UserName = "username"
    $sessionOptions.Password = "password"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="

    $localPath = "c:\local\path\*.txt"
    $remotePath = "/remote/path/"

    try
    {
        # Connect
        Write-Host "Connecting..."
        $session = New-Object WinSCP.Session
        $session.SessionLogPath = "session.log"
        $session.Open($sessionOptions)

        # Upload files
        Write-Host "Uploading..."

        $localFiles = Get-ChildItem -Path $localPath

        foreach ($localFile in $localFiles)
        {
            $name = $localFile.Name;
            Write-Host "Found file $name"

            $index = 0;
            while ($session.FileExists($remotePath + $name))
            {
                Write-Host "$name exists already in remote directory"
                $index++;
                $name = $localFile.Name + " ($index)"
            }

            Write-Host "Uploading as $name ..."
            $session.PutFiles($localFile, $remotePath + $name).Check()
        }

        Write-Host "Done"
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}
7
  • please my review question again, i want to rename the file while transferring to SFTP server Commented Dec 15, 2015 at 7:55
  • That's what I answered. Read my answer again. The first put uploads the local file file.txt to a remote file file_one.txt. While the second put uploads it as file_two.txt. Commented Dec 15, 2015 at 7:55
  • I transferring the multiple files, during the time i using file extension like '*.txt', And i don't want to rename all files, I want to rename only which are files as same name already exist in the SFTP server Commented Dec 15, 2015 at 7:58
  • Your question down not mention multiple files not any extension. You need to elaborate on how you want to rename the files automatically, if you are uploading multiple files. That's something the "New name" command in WinSCP GUI cannot do. Commented Dec 15, 2015 at 8:00
  • @VigneshRajendran See my edited answer for a solution using PowerShell. Commented Dec 15, 2015 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .