Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

2 Answers

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!

share|improve this answer
    
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
show 3 more comments

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!
)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.