Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

batch rename files consecutive n°

Original Message
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


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: June 20, 2007 at 01:29:56 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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...


Report Offensive Follow Up For Removal

Response Number 2
Name: maxbre
Date: June 20, 2007 at 02:18:13 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: June 20, 2007 at 02:49:57 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 4
Name: maxbre
Date: June 20, 2007 at 03:24:40 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 5
Name: IVO
Date: June 20, 2007 at 03:56:37 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
Well Max,

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

Ciao
Ivo


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: June 20, 2007 at 04:22:55 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
::== 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



Report Offensive Follow Up For Removal

Response Number 7
Name: maxbre
Date: June 20, 2007 at 05:07:19 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: June 20, 2007 at 05:30:26 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
If it's NT5 It's built in.

echo %CD%



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

M2



Report Offensive Follow Up For Removal

Response Number 9
Name: maxbre
Date: June 20, 2007 at 06:16:00 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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...?

Report Offensive Follow Up For Removal

Response Number 10
Name: IVO
Date: June 20, 2007 at 06:17:38 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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.



Report Offensive Follow Up For Removal

Response Number 11
Name: maxbre
Date: June 20, 2007 at 06:36:54 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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%


Report Offensive Follow Up For Removal

Response Number 12
Name: IVO
Date: June 20, 2007 at 07:03:31 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 13
Name: djones
Date: June 23, 2007 at 17:21:05 Pacific
Subject: batch rename files consecutive n°
Reply: (edit)
maxbre i have the same confusion with the whole ~0,-2 syntax

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


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: batch rename files consecutive n°

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


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

All content ©1996-2007 Computing.Net, LLC