Link to home
Start Free TrialLog in
Avatar of WilsonJ
WilsonJFlag for United States of America

asked on

Script or Batch to Rename Files

Hello Experts,

I need your help in creating a batch or script that will rename two files located in a shared folder. the two original files once generated by my program will always start with a fix letter A and B follow by the date.  The format is somewhat like this A00000000.XXX and B00000000.XXX
I need to rename these files to have the following format "NAME.NAME.name.A0000000.XXX and NAME.NAME.name.B0000000.XXX respectively. Basically the name of the original file adding the 3 words separated by periods to the beginning of the file name.
I will need to run this from a windows XP Pro workstation which is part of AD on a mix 2K and 2K3 domain my idea is to put this batch or script in the desktop and have the user double click on it to rename the file. i think it will actually be better to create one batch or script per file, since the orginals are created at different times of the day.
My knowledge on batch and scripting is very very limited therefore i'm posting this ? in hope for someone to create or guide me in the process of creating the script.
I thank you in advance.
WilsonJ
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this... paste into notepad and save as namefiles.cmd

@echo off
SET newname=NAME.NAME.name
pushd \directoryname
for /f "tokens=*" %%a in ('dir /b /a-d A_*.XXX') do rename "%%a" "%newname%.%%a" & echo Renaming %a to %newname%.%a
for /f "tokens=*" %%a in ('dir /b /a-d B_*.XXX') do rename "%%a" "%newname%.%%a" & echo Renaming %a to %newname%.%a
popd
pause

It change into the relevant directory and gets a directory listing of files matching A*.XXX and then renames them by adding NAME.NAME.name to the front then does the same for B*.XXX.

If there is nothing that matches that pattern nothing happens

Steve
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are your three words going to change with each file?  And how will you select which file to rename?  Will they always be placed in a specific folder?

Do you want the user prompted for the three words, or a folder to select?

Regards,

Rob.
Avatar of WilsonJ

ASKER

That was it, thank you very much dragon-it
No problem.

Steve