0

I have a windows file system where a hiccup in our backup software renamed hundreds of files.

The change looks like this:
"ABC.PDF" -> "ABC.PDF.BAK"

This happened to some, but not all files in a directory.

I would like to rename such files to their old name, but the script should output or ignore cases where this is not possible, because a "ABC.PDF" already exists.

I am not familiar at all with batch scripts, but powershell is available too.

Hennes
  • 65,142

1 Answers1

1

You can do something like this in powershell:

$files = dir *.BAK
foreach($file in $files) {
    Rename-Item $file $file.BaseName -ErrorAction Ignore
}

There will only be an error if those files no longer exist, or if you are trying to overwrite a file.

If you want to see what it will do first, add the -WhatIf flag.

soandos
  • 24,404
  • 28
  • 103
  • 134
  • I will try it in a moment, thank you very much – Christian Sauer Sep 02 '14 at 08:45
  • 1
    @ChristianSauer just as an aside, in your title you have .BAK, but in you question you have .BAL. Can you make it consistant? – soandos Sep 02 '14 at 08:48
  • Worked like a charm and I got the chance to learn more about PowerShell, thank you. – Christian Sauer Sep 02 '14 at 09:43
  • @ChristianSauer No problem. As a rule, these types of "simple" operations on files are very easy in powershell, and if you are doing something that is going to be hard to undo, just add the -WhatIf flag to the end of any command to see what would happen (as opposed to actually doing it). Use -ErrorAction Continue to report them (forgot that part until now, sorry) – soandos Sep 02 '14 at 12:30