Welcome › Forums › General PowerShell Q&A › [POWERSHELL] check rename directory
- This topic has 6 replies, 3 voices, and was last updated 8 months, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
May 12, 2020 at 2:49 pm #227545
Hello ,
I want to know one thing. I realize a script and I want to check if the directory is corretly rename.
For exemple I creat this one : $test | rename-item -NewName{$_.name -replace “exemple”,”training”}
However as soon as the directory is modified the first time, if I start the schrip there is a error message which write “the item is already rename ” something like that. To avoid the pollution of my display, I want to do include a conditionnal. But, I don’t know what I have to put in this “If” I want to do something like that :
PowerShell12345[crayon-600ecc2b284d2759372056 inline="true" ]If( $_.name -eq "training ") {Write-Host "The directory is already rename"}But, I know, this one can’t work. Do you have an idea ? I think I missed something easily understandable btu i don’t know what.
Thanks to you !
-
This topic was modified 8 months, 2 weeks ago by
Noyha56.
-
This topic was modified 8 months, 2 weeks ago by
-
May 12, 2020 at 3:21 pm #227554
aa, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.
When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.You may post your actual code or at least the relevant part of it and the complete error message you get.
-
May 12, 2020 at 3:40 pm #227557
Explain and show all of the code. What is $test? You should not need to see if something is renamed as you should be only finding folders named ‘exemple’ to do the rename.
-
May 12, 2020 at 6:18 pm #227578
Explain and show all of the code. What is $test? You should not need to see if something is renamed as you should be only finding folders named ‘exemple’ to do the rename.
The code is just about a directory which is in a place and I want to rename this directory. As soon as it’s rename I want to do a conditionnal “If “to verify if the directoy is correctly rename.
The variable $test is a random name for my example $test can be equal to “C:\User\Program” It’s just the place where we can find the directory that we want to rename
-
May 12, 2020 at 7:29 pm #227593
By default the cmdlet Rename-Item does not generate any output. But you can change this by using the parameter -PassThru. With this you can check the generated object if it’s the expected result. Please read the complete help including the wexamples to learn how to use it. You should read the help for about_if as well to get your task done. 😉
-
May 12, 2020 at 8:00 pm #227599
The error should be captured to determine success\fail, doing a Test-Path isn’t necessary:
PowerShell123456789$path ='C:\Scripts\Foo'try {$results = Rename-Item -Path $path -NewName Blah -Force -ErrorAction Stop -PassThru'Successfully renamed folder {0} to {1}' -f $path, $results.FullName}catch {'Problem renaming folder {0}. {1}' -f $path, $_}Output from success and then a re-run but the folder doesn’t exist, because it was renamed…
PowerShell1234567891011121314151617181920212223PS C:\Users\rasim>$path ='C:\Scripts\Foo'try {$results = Rename-Item -Path $path -NewName Blah -Force -ErrorAction Stop -PassThru'Successfully renamed folder {0} to {1}' -f $path, $results.FullName}catch {'Problem renaming folder {0}. {1}' -f $path, $_}Successfully renamed folder C:\Scripts\Foo to C:\Scripts\Blah\PS C:\Users\rasim>$path ='C:\Scripts\Foo'try {$results = Rename-Item -Path $path -NewName Blah -Force -ErrorAction Stop -PassThru'Successfully renamed folder {0} to {1}' -f $path, $results.FullName}catch {'Problem renaming folder {0}. {1}' -f $path, $_}Problem renaming folder C:\Scripts\Foo. Cannot rename because item at 'C:\Scripts\Foo' does not exist. -
May 12, 2020 at 8:17 pm #227602
Thanks for your help I’ll test this method tomorrow !
-
-
AuthorPosts
- The topic ‘[POWERSHELL] check rename directory’ is closed to new replies.