Solved Add a prefix to file name using a batch file

September 22, 2011 at 12:25:55
Specs: Windows XP
I have files named MIMA0512xxxxxxxxxxxx where xxxxxxxxxxxx are variable characters. I want to add BT512PDF1O. to the beginning of each file so that it will look like this: BT512PDF1O.MIMA0512xxxxxxxxxx. When I use this command:
ren MIMA0512?????????? BT512PDF1O.MIMA0512?????????? it drops off the 10 variable characters. Any ideas?

See More: Add a prefix to file name using a batch file

Report •


✔ Best Answer
September 22, 2011 at 13:07:01
If you're going to script it, the syntax changes. Also, I can only go with what you give me.
for %%a in (MIMA0512??????????.*) do ren "%%a" "BT512PDF1O.%%a"

How To Ask Questions The Smart Way



#1
September 22, 2011 at 12:31:50
for %a in (MIMA0512??????????) do ren "%a" "BT512PDF1O.%a"

Obtuse, ain't it?

How To Ask Questions The Smart Way


Report •

#2
September 22, 2011 at 12:42:30
Hey Razor2.3,

Would dewey need to dir the filenames into a file first? Something like this:

@echo off

REM creating a file containing all of the files that match MIMA0512*
dir /b MIMA0512* > file_list.tmp
for /f "tokens=*" %a in (file_list.tmp) do ren %a BT512PDF1O.%a
REM deleting temp file
del /q /f file_list.tmp


Report •

#3
September 22, 2011 at 12:55:32
That is very clever. I see what you are doing but it didn't work. I forgot one part. The original files actually look like this. MIMA0512??????????.txt or .zip. I changed your solution to 14 ?s and then to 10? plus .???. Finally I tried 2 lines, one with .txt and the other with .zip. It still did not work. I think the . is messing up the solution. Any other ideas?

Report •

Related Solutions

#4
September 22, 2011 at 13:02:20
dewey,

Try just entering and executing this line at a command prompt and tell me if it lists the correct files that you want to rename:

dir /b MIMA0512*

If that command creates an accurate list, we'll go on from there.


Report •

#5
September 22, 2011 at 13:07:01
✔ Best Answer
If you're going to script it, the syntax changes. Also, I can only go with what you give me.
for %%a in (MIMA0512??????????.*) do ren "%%a" "BT512PDF1O.%%a"

How To Ask Questions The Smart Way


Report •

#6
September 22, 2011 at 13:07:36
Jowah,

Yes, it created an accurate list.


Report •

#7
September 22, 2011 at 13:09:08
Or try this... I put the BT512PDF1O into a variable and changed the ren command to ren %a %b%.%a

@echo off
REM creating a file containing all of the files that match MIMA0512*
dir /b MIMA0512* > file_list.tmp
REM loading BT512PDF1O into a variable
set b=BT512PDF1O
for /f "tokens=*" %a in (file_list.tmp) do ren %a %b%.%a
REM deleting temp file
del /q /f file_list.tmp


Report •

#8
September 22, 2011 at 13:13:00
Thanks for your help. When I gave Razor3 the right info his solution worked.

Report •

#9
September 22, 2011 at 13:14:38
Sorry about that. I hate it when people leave out part of the specs and that is exactly what I did to you. It's been a long day but you just helped make it shorter.

dewey


Report •

Ask Question