3

I have about 300 .pst files that I have exported from Exchange 2010. Eventually, I'm going to import them into another Exchange/AD environment. In the original environment, the user names are as follows: FirstinitialMiddleInitialLastName i.e. Bart P. Smith would be bpsmith.

In the new environment, the user name are first initial last name. So, bsmith.

Instead of manually renaming each pst file for import does anyone know a way to batch rename each file in the directory, removing the second letter in the filename?

Thanks in advance.

Tyler Miranda
  • 73
  • 2
  • 9
  • The quick-and-dirty way to do this would be to put your .pst files in one directory and then dir > filename.txt, then use Notepad++ column-edit mode to craft a batch file of 300 rename commands that would look like this: ren foo.pst bar.pst A more elegant way to do it would be to use the powershell rename-item cmdlet and use regex to remove the 2nd character. – smithian Oct 25 '12 at 18:44
  • What about users that don't have a middle name? Bart Smith would become `Bmith.pst` – Bryan Oct 25 '12 at 19:01
  • There are 300 files. Either pre-sort into "modify" and "Don't modify" folders, or handle exceptions while you are editing. – smithian Oct 25 '12 at 19:12

2 Answers2

4

Open a command prompt in the directory your .pst files are located in, and place the following file in that directory (save it as renamepst.cmd)

@echo off
for %%f in (*.pst) do call :renfile "%%f"
goto :eof

:renfile
set file=%1
echo rename %file% %file:~0,2%%file:~3,999%

This batch file has an echo command in place so that it doesn't actually perform any actions. If you are happy with the output after running renamepst.cmd in your command prompt window, then remove the echo from the last line, leaving rename %file% %file:~0,2%%file:~3,999% in place.

Please test before using this though!

Bryan
  • 7,628
  • 15
  • 69
  • 94
  • Won't work. You need delayed expansion on the %file%. Basic idea is sound though, at least if you can be certain EVERY name has a middle initial to be removed. – Tonny Oct 25 '12 at 19:26
  • Corrected, tested, and working fine now. – Bryan Oct 25 '12 at 19:27
  • You shall not use GoTo anymoar ! – Jonathan Rioux Oct 25 '12 at 19:50
  • @Bryan That's the way to do it. And it works regardless off the delayed expansion and whether or not command extensions are enabled. Nice ! – Tonny Oct 25 '12 at 21:11
  • @JonathanRioux Believe it or not, but that is the official Microsoft recommended way of skipping the :renfile sub-routine. Of course an "exit" command would have had the same effect. – Tonny Oct 25 '12 at 21:15
  • @Bryan Oops..... Indices are wrong. You are removing the 3rd char in stead of the 2nd. – Tonny Oct 25 '12 at 21:27
  • @Tonny It's including a quote as the first character, so although I'm removing the 3rd character, it's the second character in the filename that is being removed. e.g. `"filename.pst"` becomes `"flename.pst"`. - At least it did here on my laptop. – Bryan Oct 25 '12 at 22:23
  • @Brain In that case you are right of course. As far as I know the quotes only appear if the filenames contain spaces or if they really have quotes in the name. – Tonny Oct 26 '12 at 14:55
0

Save this content in a .bat file and there you go:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set file=
cd C:\test
for %%f in (*.pst) do (
    set file=%%f
    move !file! !file:~0,1!!file:~2,999!
)
Jonathan Rioux
  • 1,938
  • 6
  • 33
  • 57