Name: maxbre Date: June 20, 2007 at 00:24:50 Pacific Subject: batch rename files consecutive n° OS: WIN NT CPU/Ram: not relevant Model/Manufacturer: not relavant
Comment:
Hi all
sorry for my really basic level of programming; I need to sort out this problem; I have a number of *.txt files and I want to rename all them with #.txt where # is a consecutive number; I want all this through the use of a batch file; since now I've managed to write what follows (e.g for 10 *.txt files):
for %%b in (*.txt) do for /L %%a in (1,1,10) do rename %%~nb.txt %%a.txt
which is doing the almost right the work BUT on the other hand I understand it is a really poor (and even not an efficient) way to deal with the problem; I hope in your valuable help, please in case of reply specify me all the sequence of commands (I'm a real newby); I searched through this forum but I did not find exactly what I need thank you
are you sure you have searched ? you most probably will not find EXACTLY what you want as most problems come from different domains, but you should probably find SIMILAR types of problems. Check these search results. http://computing.net/cgi-bin/AT-sea...
Yes, you are right (as usual) I've managed to find something and I've adapted it to my needs; and it works!!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @echo off set count=0 for %%i in (*.jpg) do ( set name=%%i call :countadd ) :countadd set /a count=count+1 ren %name% %count%.jpg goto :eof
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I just do not exactly understand this: ( set name=%%i call :countadd )
I did not know it's possible to insert a call there...
thanks ivo that's really great ! ...but I would like to generalize it so that the batch can be run in every dir it is stored (without having to specify the folder path); and here I have another question: how would you store in a variable the name of the dir where the batch is resident?(I mean a relative name: e.g. "c:\temp" would end up in a variable with content "temp") thanks
thank you for the feedback; I slightly modified your batch in order to be run in every dir (as I wanted);
::== seq.bat @echo off setLocal EnableDelayedExpansion for /f "tokens=* delims= " %%a in ('dir /s/b/a-d *.txt') do ( set /a N+=1 ren %%a !N!.txt ) ::==
by the way your original batch was just echoing the process (but certainly you know that and you did it because you want to grow up a little your affectionate pupils);
there is still the question about the storing in a variable of the the dir relative name where the bat is resident... any help for this?
well let me explain: I want to batch rename a file yyy.yyy which is resident in a given dir XXX with the new name XXX.yyy; I really do not know how to deal with this, I understand it might be very simple but I'm stuck with the variable assignement set a=%CD% ???? I just want the name and not the path.. is it something to deal with ~n...?
About the relative directory, the code below sets up in the RD variable the active directory stripping the full path (that is stored in the system variable CD to be not modified)
Set CF=%CD% Set RD= :LOOP If "%CF:~-1,1%"=="\" GoTo :DONE Set RD=%CF:~-1,1%%RD% Set CF=%CF:~0,-1% GoTo :LOOP
:DONE Echo. Echo %CD% %RD%
Pay attention that in case of CD "drive:\" RD is empty.
About the code posted by M2 be aware it doesn't work under NT/4.0 as the "delayed expansion" was introduced starting with Win 2000 (NT 5.0), so it is at your own risk.
The reference I suggest, althought highly unfriendly, is to read carefully the on line short help offered by typing Set /? at prompt.
Here you can learn a lot about substring manipulation, one of the most powerfull facility of NT batch language. To get full exposure to the tricks allowed mixing positive and negative lenght/offset requires a bit of experience and experiments.
As a general rule I suggest you inquire commands by the /? switch, even those you suppose well known: NT batch language is reach of amazing surprises.
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE