rename all files in a folder

  • Thread starter Thread starter JeffO
  • Start date Start date
J

JeffO

I have a user who has to rename tens of thousands of
files. The filenames were generated by a machine. They
follow this format:
70 1xxx xxxxxxxxx .tif
Strange enough? They start with a blank space, then 70,
then another blank space, 1 and three other digits, etc.
I need a script that will rename all the files in a folder
that follow this criteria to this format:
70-1xxx xxxxxxxxx .tif
So I only need the first space erased and the second
changed to a dash. The rest of the name MUST be retained.

Is this possible with a batch script, or must it be vbs?
 
JeffO said:
I have a user who has to rename tens of thousands of
files. The filenames were generated by a machine. They
follow this format:
70 1xxx xxxxxxxxx .tif
Strange enough? They start with a blank space, then 70,
then another blank space, 1 and three other digits, etc.
I need a script that will rename all the files in a folder
that follow this criteria to this format:
70-1xxx xxxxxxxxx .tif
So I only need the first space erased and the second
changed to a dash. The rest of the name MUST be retained.

Is this possible with a batch script, or must it be vbs?

- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>dir /b d:\junkdir\*.tif
70 1aaa aaaaaaaaa .tif
70 1xxx xxxxxxxxx .tif
70 1zzz zzzzzzzzz .tif

<Win2000> c:\cmd>demo\rename345
ren "d:\junkdir\ 70 1aaa aaaaaaaaa .tif" "70-1aaa aaaaaaaaa .tif"
ren "d:\junkdir\ 70 1xxx xxxxxxxxx .tif" "70-1xxx xxxxxxxxx .tif"
ren "d:\junkdir\ 70 1zzz zzzzzzzzz .tif" "70-1zzz zzzzzzzzz .tif"

<Win2000> c:\cmd>rlist demo\rename345.cmd
=====begin c:\cmd\demo\rename345.cmd ====================
1. @echo off
2. for /f "tokens=*" %%a in (
3. 'dir /b d:\junkdir\*.tif'
4. ) do call :rename "%%a"
5. goto :EOF
6. :rename
7. set oldname=%~1
8. set newname=%oldname:~0,2%-%oldname:~3%
9. echo ren "d:\junkdir\ %oldname%" "%newname%"
=====end c:\cmd\demo\rename345.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -

If the preceding meets your requirements, remove the word 'echo'
from line 9 to actually do the renaming.
 
Not a fixed number of spaces or length clear across. Only
the first part is the same.
And some files begin with:
C 480 xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx .tif
I don't want the script to alter those (I'll do a 2nd
section or script for them). I need a discriminatory
script that will only change the first part of " 70 1"
to "70-1" and retain the variable endings.

That's what makes this all so hard.
Thanks in advance.
 
Ahh... It needed quotes.
ren " 70?1*.tif" 70-1*.*

It only needs quotes if it has SPACEs (or other weird stuff perhaps.)

doing the following also works fine:

cd /d C:\Program*files
OR
cd /d C:\Program?files

(I hate spaces in Directory names especially. Bad Microsoft;
bad Microsoft. said:
I thought I tried this exact line earlier, with quotes,
but apparently there was something wrong earlier. This
latter line does it all beautifully.
Thanks a million!

Sure!!! (Cash is fine <grin>)
 
Oops! Spoke too soon. I was doing a demo to the user with
a copy of the folder she's working in. There's an odd
thing that I can't explain. It throws in an extra "1".

Example of original (leading with a space):
70 1234 LONGNAME N.C. 480 & 481 .tif
Result:
70-11234 LONGNAME N.C. 480 & 481 .tif

Command:
ren " 70?1*.tif" 70-1*.*
Also tried:
ren " 70 1*.tif" 70-1*.*
ren " 70?1*.tif" "70-1*.*"
ren " 70?1*.tif" 70-*.*
ren " 70?1*.tif" 70-1*.tif
And many more...
Either the name gets totally goofed up, or there's an
extra 1. I can't for the life of me figure out where the
extra is comming from.
 
Example of original (leading with a space):
70 1234 LONGNAME N.C. 480 & 481 .tif
Result:
70-11234 LONGNAME N.C. 480 & 481 .tif

Command:
ren " 70?1*.tif" 70-1*.*

The REN command cannot delete characters from the name. What you have
told it to do is this:

For all files matching
"SPACE SEVEN ZERO anyChar ONE anyString DOT TEE EYE EFF"
replace the first four characters with
"SEVEN ZERO DASH ONE", leaving the rest of the filename alone.

Since the original filename started with
"SPACE SEVEN ZERO SPACE ONE TWO THREE",
the filename ends up being
"SEVEN ZERO DASH ONE ONE TWO THREE" ...

There is no way at all to make the REN command replace five characters
with four. You will have to use another solution, like the rename345.cmd
offered by Phil Robyn, or this vbscript:

strPath = "C:\PATH\TO\DIR"
set fso = createobject("scripting.filesystemobject")
set oFolder = fso.GetFolder(strPath)

set oRegex = new Regexp
oRegex.Pattern = "^ 70.1.*\.tif$"

for each oFile in oFolder.Files
if oRegex.Test(oFile.name) then
strNewName = "70-1" & mid(oFile.name,5)
oFile.Rename strNewName
end if
next
 
That explains a lot!
But the VBS causes a runtime error on oFile.Rename
Line 11, Char 8, Code 800A01B6
I'm not familiar with vbs.
 
That explains a lot!
But the VBS causes a runtime error on oFile.Rename
Line 11, Char 8, Code 800A01B6
I'm not familiar with vbs.

Man, I am getting sloppy. There is no .Rename method for the File object.
Instead, make that line read

oFile.Name = strNewName

I apologize for the error.
 
That explains a lot!
But the VBS causes a runtime error on oFile.Rename
Line 11, Char 8, Code 800A01B6
I'm not familiar with vbs.

ARGH!!!!! Another mistake .... Here is the script as it should be:

strPath = "C:\PATH\TO\DIR"
set fso = createobject("scripting.filesystemobject")
set oFolder = fso.GetFolder(strPath)

set oRegex = new Regexp
oRegex.Pattern = "^ 70.1.*\.tif$"

for each oFile in oFolder.Files
if oRegex.Test(oFile.name) then
strNewName = "70-1" & mid(oFile.name,6)
oFile.Name = strNewName
end if
next
 
Back
Top