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.

2
  • What do you want to do when its not possible?
    – soandos
    Sep 2, 2014 at 8:23
  • @soandos Ignore the file or ideally report them Sep 2, 2014 at 8:44

1 Answer 1

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.

4
  • I will try it in a moment, thank you very much Sep 2, 2014 at 8: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 2, 2014 at 8:48
  • Worked like a charm and I got the chance to learn more about PowerShell, thank you. Sep 2, 2014 at 9: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 2, 2014 at 12:30

Not the answer you're looking for? Browse other questions tagged .