locked
Renaming and Moving Files RRS feed

  • Question

  • I am a complete newbie to PowerShell.  I am working on a script to upload files to an SFTP site and then rename them to .bak and move them to backup directory.

    I have them uploading just fine and I can get them to move to a different directory but I am having trouble with the renaming part.

    This is the main part of the script that is working.  I move each file individually after they upload and so I am wondering if I could just rename them before I move them to the backup directory.  My files are .dat and I would like to change them to .bak.

        try
        {
            # Connect
            $session.Open($sessionOptions)
     
            # 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" -f
                        $transfer.FileName)
                    # Upload succeeded, move source file to backup
                    
                    # Move-Item $transfer.FileName $backupPath 'original
                    Move-Item $filenew $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
    }

    My FileName is the whole filename with the extension so I was trying to do a GetFileNameWithoutExtension right before the Move-Item but I don't think I have the syntax correct.

    $filenew = $transfer.FileName.GetFileNameWithoutExtension($_.fullname)  #$transfer.FileName + ".bak"
                    Rename-Item $filenew

    Thank you,

    Stacy




    • Edited by tnswalker Tuesday, June 9, 2015 6:57 PM
    • Moved by Bill_Stewart Monday, July 27, 2015 8:25 PM Abandoned
    Tuesday, June 9, 2015 6:48 PM

Answers

  • $f_rename |Get-ChildItem -Filter "*.dat*" |Rename-Item -NewName {$_.name -replace 'dat’,'bak'}

    • Proposed as answer by K38hoa Wednesday, June 10, 2015 1:44 PM
    • Marked as answer by tnswalker Thursday, November 19, 2015 8:57 PM
    Wednesday, June 10, 2015 1:23 PM

All replies

  • Once the file has been uploaded to the remote directory try:

    $f_rename = "PATH_for_files_to_be_renamed"

    $f_rename | Get-ChildItem |Rename-Item -NewName {$_.Name + ".bak"}

    • Proposed as answer by K38hoa Tuesday, June 9, 2015 9:45 PM
    • Unproposed as answer by K38hoa Wednesday, June 10, 2015 1:44 PM
    Tuesday, June 9, 2015 9:44 PM
  • Thanks K_121 for the reply.

    I tried your code and it renames my file by adding .bak at the end of the file name so it looks like filename.dat.bak instead of filename.bak.

    Any ideas?

    Thanks,

    Stacy

    Wednesday, June 10, 2015 11:55 AM
  • Get-ChildItem -File | ForEach-Object {
        # Both will return file names without extension
        [System.IO.Path]::GetFileNameWithoutExtension($_.FullName)
        $_.BaseName
    }

    Wednesday, June 10, 2015 12:16 PM
  • $f_rename |Get-ChildItem -Filter "*.dat*" |Rename-Item -NewName {$_.name -replace 'dat’,'bak'}

    • Proposed as answer by K38hoa Wednesday, June 10, 2015 1:44 PM
    • Marked as answer by tnswalker Thursday, November 19, 2015 8:57 PM
    Wednesday, June 10, 2015 1:23 PM