Link to home
Start Free TrialLog in
Avatar of sirasonian
sirasonian

asked on

Rename files and remove prefix through batch file

Dear All:

I am trying to rename files in a directory (c:\temp) with a batch file that will take filenames:

U000123.dat
U000947.dat
U001658.dat
U006791.dat

and rename them to

123.dat
947.dat
1658.dat
6791.dat

The rule is to basically strip the characters U and the 2~3 zeros.

TIA!
Avatar of Pui_Yun
Pui_Yun
Flag of Canada image

This isn't precisely a batch file, but ....  Here's a quick vb script that should do the trick, just create a file with a vbs extension (create a text file and rename it to a .vbs) copy the code below and run it.

One last thing, this will only work if the only files in the C:\Temp directory are the ones that you have mentioned and will not recursive do sub folders.  So make a backup of your C:\Temp directory and delete all other files but the type you mentioned.  Hope this helps:

Dim fso, fold, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder("C:\Temp")
For Each f In fold.Files
    FileName = Right(f.Name, Len(f.Name) - 1)
    extension = Right(f.Name, Len(f.Name) - InStrRev(f.Name, "."))
    FileName = Left(FileName, InStrRev(FileName, ".") - 1)    
    f.Move f.ParentFolder & "\" & Int(FileName) & "." & extension
Next

Let me know if you need any help
Avatar of sirasonian
sirasonian

ASKER

Thanks for your input. However, sorry, should've clarified - I am looking for a DOS batch file.

Thanks,
Graham
Put it in the directory you want to rename files in.
Note: It removes all occurences of U and 0 from the file name
E.g. U002330.dat  => 233.dat

------------- Batch File starts here ------------

@Echo Off

SETLOCAL


For /F "tokens=*" %%I in ('Dir /B %~f1.\*.dat') Do Call :RENAMEFILES %~n1 %%I
GoTo :EOF

:RENAMEFILES

Set FName=%~n1

REM Replace U with <blank>
Set FName=%FName:U=%

REM Replace 0 with <blank>
Set FName=%FName:0=%

Echo %~n1%~x1 Changed To  %Fname%%~x1
Rename %~n1%~x1  %FName%%~x1
GoTo :EOF
avi247,

Thanks for the great script. Could I request one more change before I accept your answer? Because some files will be named:

U000130.dat
U006480.dat
U007120.dat
so on...

trailing zeros should not be removed because U000130.dat is going to be changed to 13.dat however it's really 130.dat and will conflict with U000013.dat.

TIA!
ASKER CERTIFIED SOLUTION
Avatar of avi247
avi247

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
avi247,

Worked beautifully! Actually, the commands did not strip the leading "U" however your two answers provided me with enough information to get the job done.

Graham
Glad it helped :)
This is just an afterthought. I tried something else and it didnt work as great.
Here's the script:

@echo off
:start
For /F "tokens=*" %%I in ('Dir /B U00*.*') Do Call :Ren1 %%I

:Ren1
set name=%%I:~4%
ren %I% %name%

For some reason it only renamed one of the files then it stopped.