Solved

Rename files and remove prefix through batch file

Posted on 2004-10-13
8
1,094 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:sirasonian
8 Comments
 
LVL 7

Expert Comment

by:Pui_Yun
ID: 12303139
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
0
 

Author Comment

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

Thanks,
Graham
0
 
LVL 4

Expert Comment

by:avi247
ID: 12305320
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
0
 

Author Comment

by:sirasonian
ID: 12310192
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!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Accepted Solution

by:
avi247 earned 500 total points
ID: 12329323
Graham:
Sorry for the late reply. I am having problems trying to get it work the way you want using pure DOS commands.
Here's a bat file that dynamically uses vbscript to rename files the way you want.

--------batch file starts -----------

@echo off

:: setup variables for the batch
setlocal
set vbscript=%temp%\~%~n0.vbs
set vbreplace=cscript /NoLogo "%vbscript%"
echo %vbscript%

>"%vbscript%" echo  Dim fso, f, sf, intCount, strFile, pos  
>>"%vbscript%" echo  pos = 1
>>"%vbscript%" echo intCount = 0        
>>"%vbscript%" echo Set fso = CreateObject("Scripting.FileSystemObject")
>>"%vbscript%" echo Set f = fso.GetFolder("D:\Work\Source Code\Batch Files\Temp")
>>"%vbscript%" echo For Each sf In f.Files
>>"%vbscript%" echo pos = 1
>>"%vbscript%" echo strFile = sf.Name
>>"%vbscript%" echo If UCase(Left(strFile, 1)) = "U" Then strFile = Right(strFile, Len(strFile) - 1)
>>"%vbscript%" echo For intCount = 0 To Len(strFile)
>>"%vbscript%" echo  Do While Mid(strFile, pos, 1) = "0"
>>"%vbscript%" echo    pos = pos + 1
>>"%vbscript%" echo  Loop
>>"%vbscript%" echo  Exit For
>>"%vbscript%" echo Next
>>"%vbscript%" echo strNewName = Right(strFile, Len(strFile) - pos + 1)
>>"%vbscript%" echo fso.MoveFile sf, Replace(sf,strFile,strNewName)
>>"%vbscript%" echo Next

%vbreplace%

:: cleanup
del "%vbscript%"

--------batch file ends -----------
0
 

Author Comment

by:sirasonian
ID: 12341736
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
0
 
LVL 4

Expert Comment

by:avi247
ID: 12342689
Glad it helped :)
0
 

Expert Comment

by:No1Spec
ID: 12405093
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now