Computing.Net > Forums > Programming > batch rename files consecutive n°

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch rename files consecutive n°

Reply to Message Icon

Name: maxbre
Date: June 20, 2007 at 00:24:50 Pacific
OS: WIN NT
CPU/Ram: not relevant
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 20, 2007 at 01:29:56 Pacific
Reply:

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


0

Response Number 2
Name: maxbre
Date: June 20, 2007 at 02:18:13 Pacific
Reply:

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



0

Response Number 3
Name: IVO
Date: June 20, 2007 at 02:49:57 Pacific
Reply:

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 :EOF

By the way, I am italian too, from Milano.


0

Response Number 4
Name: maxbre
Date: June 20, 2007 at 03:24:40 Pacific
Reply:

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

by the way I'm IT from Padua



0

Response Number 5
Name: IVO
Date: June 20, 2007 at 03:56:37 Pacific
Reply:

Well Max,

I'll answer your questions later as now I've an incoming hot call to service.

Ciao
Ivo


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: June 20, 2007 at 04:22:55 Pacific
Reply:

::== seq.bat
@echo off
setLocal EnableDelayedExpansion

for /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



0

Response Number 7
Name: maxbre
Date: June 20, 2007 at 05:07:19 Pacific
Reply:

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


0

Response Number 8
Name: Mechanix2Go
Date: June 20, 2007 at 05:30:26 Pacific
Reply:

If it's NT5 It's built in.

echo %CD%



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 9
Name: maxbre
Date: June 20, 2007 at 06:16:00 Pacific
Reply:

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


0

Response Number 10
Name: IVO
Date: June 20, 2007 at 06:17:38 Pacific
Reply:

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.



0

Response Number 11
Name: maxbre
Date: June 20, 2007 at 06:36:54 Pacific
Reply:

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%


0

Response Number 12
Name: IVO
Date: June 20, 2007 at 07:03:31 Pacific
Reply:

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.


0

Response Number 13
Name: djones
Date: June 23, 2007 at 17:21:05 Pacific
Reply:

maxbre i have the same confusion with the whole ~0,-2 syntax

Dominic Adair-Jones
IT Specialist
FDIC Federal Credit Union
www.fdicfcu.org


0

Sponsored Link
Ads by Google
Reply to Message Icon

A batch file for rename ftp error



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch rename files consecutive n°

bat: Rename files w/double ext? www.computing.net/answers/programming/bat-rename-files-wdouble-ext/16440.html

batch rename file extension? www.computing.net/answers/programming/batch-rename-file-extension/17669.html

Batch Rename files in a folder www.computing.net/answers/programming/batch-rename-files-in-a-folder/15096.html