Rename file with error handling?

Hi,
I want to rename files remotely and error handling.

Can anyone help me with the script, this is what I got so far

$files = Get-ChildItem -Path C:\Temp
foreach ($file in $files)
{
$newFileName=$file.Name.Replace(“c11”,“c12”)
Rename-Item $file $newFileName
}

Store the file paths in an array

$files = “C:\Temp\c11.file”, “C:\Temp\c12.file”"

Run the Test-Path cmdlet against each file path to return an array of True/False

values. Then, using the -notcontains operator, return True if all boolean values

returned via Test-Path are true.

$filecheck = ($files | Test-Path) -notcontains $false

If $fileCheck is a boolean true (all files exist)

if($filecheck -eq $True) {
Write-Host “All files were found, hooray!”
} else { ## If $fileCheck is a boolean false (at least one file does not exist)
Write-Host “Files are missing.”
}

Before we proceed - could you please go back, edit your question and format your code as code? This way it is hard to distinguish between plain text and code.

You should try to make it easy for people willing to help you. :wink:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

$files = Get-ChildItem -Path C:\Temp
foreach ($file in $files)
{
$newFileName=$file.Name.Replace(“c11”,“c12”)
Rename-Item $file $newFileName
}

How can I add the error handling after that script would tell me if the result successful or not? I try exist $LASTEXISTCODE end of the script but didn’t provide any result. Thank you

What you’re asking for is not error handling - it is confirmation. :wink:

Error handling in PowerShell is done with a try catch block. Here you can read more about:

You’d basicall trust the command to do its job until you catch an error. :wink:

If you want to have a confirmation you can use Test-Path after you renamed the files and collect its output the way you want. Here you can read more about:

try {
   if(Get-ChildItem -Path "C:\Users\vdinh\Desktop\New folder" | Rename-Item -NewName {$_.Name -replace '.txt','.txt.old'} -ErrorAction Stop) {
      Write-Output "Rename"
   }
}catch {
   $_.Exception
}

the above script doesn’t provide any errors as well.

So your code seems to work. :+1:t4: :wink:

Please keep in mind we cannot see your screen and we cannot read your mind.

Why would you expect errors to happen for this simple code snippet you shared?

It didn’t provide any error message, because there’s no file “.txt” in the folder.

And why should there be an error then? If there are no “.txt” files Rename-Item has nothing to do. So there will be no error.

Try to rename an Office file while you have it opened in Office. Then you will very likely see an error. But I suspect you will not be satisfied with it anyway. :wink:

And please do not post images of code or error messages or console output. Instead post the plain text and format it as code.
Thanks.

1 Like