Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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...
thank you
max

Here the most elegant way to perform your task under Win NT/4.0 or higher
Just replace in PushD "\Temp" with your folder path
@Echo Off
PushD \Temp
Set Count=1
For %%A in (*.txt) Do Call :NUM %%A
Set Count=
PopD
GoTo :EOF:NUM
Ren "%*" %Count%.txt
Set /A Count+=1
GoTo :EOFBy the way, I am italian too, from Milano.

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")
thanksby the way I'm IT from Padua

::== seq.bat
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir /s/b/a-d c:\temp\*.txt') do (
set /a N+=1
echo ren %%a !N!.txt
)
::==
=====================================
If at first you don't succeed, you're about average.M2

dear mechanix2go
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?
thanks
ps I'm learning a lot, thank you all

If it's NT5 It's built in.
echo %CD%
=====================================
If at first you don't succeed, you're about average.M2

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...?

Hi Max,
I'm back and here what you asked.
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.

hi ivo
thank you a lot, it's a great bat!
can you give me some references on where study the proper use of such stange things like:
~-1,1
~0,-1%

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.

maxbre i have the same confusion with the whole ~0,-2 syntax
Dominic Adair-Jones
IT Specialist
FDIC Federal Credit Union
www.fdicfcu.org

![]() |
A batch file for rename
|
ftp error
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |