locked
Tracking if file renames work or not RRS feed

  • Question

  • Brand new to Powershell...  :)

    I'm working on automating some steps that we do that involve renaming a whole bunch of files.  I want to code it in so that it can just go through and rename them all, but I need to know if each rename worked or not.

    Ideally I'd want to just output something like

    Folder   Result
    ------   ------
    Folder1   OK
    Folder2   Error

    I don't see that the Rename command actually can return a True/False to say if it worked or not so I figured I can put each Rename-Item into its own Try/Catch and the Catch statement could put in Error and if it didn't have an error I could put in OK somehow.

    Just looking for some ideas for how I can make this work and look decent. :)

    Thanks!

    Friday, June 21, 2013 6:36 PM

Answers

  • I ended up using this:

    try {
    	$rename = 'OK'
    	rename-item -path \\server\FOLDER_NAME\file.txt -newname file2.txt -ea stop
    } catch {
    	$rename = 'Fail'
    }
    	
    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name Folder -Value 'FOLDER_NAME'
    $obj | Add-Member -MemberType NoteProperty -Name Result -Value ($rename)
    write-output $obj

    Just sets $rename to OK to star twith and resets it to Fail if the rename errors out.

    Thanks.

    • Marked as answer by Yan Li_ Monday, June 24, 2013 9:52 AM
    Friday, June 21, 2013 7:44 PM
  • mixing write-host and write-output is generally problematic because they do quite different things.

    I would suggest that you change the code that emits the data to something like this:

    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name Server -Value $server
    $obj | Add-Member -MemberType NoteProperty -Name Folder -Value 'FOLDER_NAME'
    $obj | Add-Member -MemberType NoteProperty -Name Result -Value ($rename)
    write-output $obj

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by Yan Li_ Monday, June 24, 2013 9:52 AM
    Friday, June 21, 2013 9:00 PM

All replies

  • Try something like this:

    $result = @()
    $i = 1
    $files = Get-ChildItem F:\Test -Recurse | Where-Object {$_ -is [System.IO.FileInfo]} 
    ForEach ($file in $files)
    {
    	$obj = New-Object Object
    	$obj | Add-Member NoteProperty OldName $file.FullName
    	$obj | Add-Member NoteProperty NewName "Test$i"
    	
    	Rename-Item -Path $file.FullName -NewName "Test$i" -ErrorAction SilentlyContinue -ErrorVariable myError
    	
    	If ($myError)
    	{
    		$obj | Add-Member NoteProperty Status "Error"
    	}
    	Else
    	{
    		$obj | Add-Member NoteProperty Status "Success"
    	}
    	$result += $obj
    	$i++
    }
    $result | FT -Auto
    You will have to modify it to suite your needs, but this works how you want it.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.




    • Proposed as answer by Mike Laughlin Friday, June 21, 2013 7:40 PM
    • Edited by clayman2 Friday, June 21, 2013 7:54 PM typo
    Friday, June 21, 2013 7:37 PM
  • I ended up using this:

    try {
    	$rename = 'OK'
    	rename-item -path \\server\FOLDER_NAME\file.txt -newname file2.txt -ea stop
    } catch {
    	$rename = 'Fail'
    }
    	
    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name Folder -Value 'FOLDER_NAME'
    $obj | Add-Member -MemberType NoteProperty -Name Result -Value ($rename)
    write-output $obj

    Just sets $rename to OK to star twith and resets it to Fail if the rename errors out.

    Thanks.

    • Marked as answer by Yan Li_ Monday, June 24, 2013 9:52 AM
    Friday, June 21, 2013 7:44 PM
  • I ended up using this:

    try {
    	$rename = 'OK'
    	rename-item -path \\server\FOLDER_NAME\file.txt -newname file2.txt -ea stop
    } catch {
    	$rename = 'Fail'
    }
    	
    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name Folder -Value 'FOLDER_NAME'
    $obj | Add-Member -MemberType NoteProperty -Name Result -Value ($rename)
    write-output $obj

    Just sets $rename to OK to star twith and resets it to Fail if the rename errors out.

    Thanks.

    YOu set ea to stop, so on first error your script will stop and not continue, also on your note property for Result you do not need to wrap $result in parenthethis

    I also noticed on my script it is set for dirs only, you want files, sorry about that, it's now fixed 

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.



    • Edited by clayman2 Friday, June 21, 2013 7:55 PM edit
    Friday, June 21, 2013 7:48 PM
  • Right.  I have a specific list of files in specific folders that need to be renamed.  The stop just breaks out the rename for the one file but then I loop around outside what I posted for the next one.

    Anyway, I did come up with oen other question so I'll see if it can be answered here instead of making a new thread...

    I use the new-object to make the output look nice in a table.  However, after doing a few renames I switch to a different server to do some more.  I want to do a write-host (or similar) to output the server name I'm currently on, and then start a new object for the renames from that server.

    Some like this:

    Server 1
    
    Folder     Result
    ------     ------
    test1     OK
    test2     Fail
    
    Server 2
    
    Folder     Result
    ------     ------
    test1     Fail
    test2     OK

    However, what I get is one big long list without the headers and a break in the middle.  Is there some way to close the object, write some normal text, and then start up a new object?

    Thanks!

    Friday, June 21, 2013 8:37 PM
  • mixing write-host and write-output is generally problematic because they do quite different things.

    I would suggest that you change the code that emits the data to something like this:

    $obj = New-Object -TypeName PSObject
    $obj | Add-Member -MemberType NoteProperty -Name Server -Value $server
    $obj | Add-Member -MemberType NoteProperty -Name Folder -Value 'FOLDER_NAME'
    $obj | Add-Member -MemberType NoteProperty -Name Result -Value ($rename)
    write-output $obj

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by Yan Li_ Monday, June 24, 2013 9:52 AM
    Friday, June 21, 2013 9:00 PM
  • Ah sure.  I just added an extra column so I have server as its own column.  That should work.

    Thanks.

    Friday, June 21, 2013 9:02 PM