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.


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:

    # 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/"

        # Connect
        Write-Host "Connecting..."
        $session = New-Object WinSCP.Session
        $session.SessionLogPath = "session.log"

        # 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"
                $name = $localFile.Name + " ($index)"

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

        Write-Host "Done"
        # Disconnect, clean up

    exit 0
catch [Exception]
    Write-Host $_.Exception.Message
    exit 1
  • please my review question again, i want to rename the file while transferring to SFTP server Dec 15 '15 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. Dec 15 '15 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 Dec 15 '15 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. Dec 15 '15 at 8:00
  • @VigneshRajendran See my edited answer for a solution using PowerShell. Dec 15 '15 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.