You are not logged in.

#1 21 Jan 2007 11:00

omnikron
Member
Registered: 21 Jan 2007
Posts: 4

Fastest most efficient Command sequence to rename a set of files

This has to be a very common scenario for all computer users/admins.

We have a bunch of files that are related in some way due to content or their intended use.
What is the best way to rename all in some way where some part of the name will be some kind of enumeration:
either some number, prefixed by zeros so that they are listed in the correct order by windows, e.g. 0001, 0002, 0003 ..  or 01,02,03 ..
or pehaps the same but with HEX enumeration 01 .. 09, 0A .. 0F, 10
or just alphabetical enumeration: aa, ab, ac .. az, ba, bb, bc .. bz, ca, cb, cc .. cz, da, ..

The thing to bear in mind is that in the general case the user can decide where they enumeration is to be in the filename.  Not necessarily at the start or the end of the filename.


The war inside the machine is as real as any war between men.
-omnikron

Offline

#2 21 Jan 2007 15:06

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Fastest most efficient Command sequence to rename a set of files

Something like this

@echo off
setlocal
set _num=0
for %%G in ("c:\folder\*.txt") do call :sub "%%G"
goto :eof

:sub
set /a _num+=1

::add leading zero's
set _padding=0000000%_num%

::trim to 7 digits
set _padding=%_padding:~-7%

:: rename the file 
ren %1 "%_padding%.txt"
goto :eof

Offline

#3 21 Jan 2007 17:09

omnikron
Member
Registered: 21 Jan 2007
Posts: 4

Re: Fastest most efficient Command sequence to rename a set of files

Thanks great Simon.
Also thank you for the reply to my more syntax intensive problem in the previous thread I posted.

I have not tested either yet so have not ironed out any exceptions etc.

I don't think I grasp the significance of the '%' operator. I have an issue with this most times but primarily.
if the '%' symbol is found in the following places then:

%_thisisavariable
%_thisisastring%
%%thisisaninteger

I don't know.  I can't think straight tbh.

Been up all night too many nights in a row hmm

Hell.
Also the task of naming a file to a substring in the output of another command resulted in some very convoluted syntax.
Where did you learn this really arcane esoteric stuff? Please can you think back & give me the honest answer?

Also due to impending Vista & Microsoft's growing 'customer blindness' I am considering ditching all that is Windows.


I believe that the unix-like commands & tools on Linux are far more thoroughly documented & that I could find many many words & examples on how to use, & configure them.

Am I right?

Last edited by omnikron (21 Jan 2007 17:12)


The war inside the machine is as real as any war between men.
-omnikron

Offline

#4 21 Jan 2007 19:31

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Fastest most efficient Command sequence to rename a set of files

The two places you'll use the % -

%thisisavariable% - a string variable
%%G  - a FOR parameter

To figure out the script above, take a look at the FOR command page
and also under Syntax - the 'Evaluating expressions' pages

I first learned my way around the command-line by reading a Compaq MS-Dos manual, but things have moved on quite a bit since then!
I'm pretty agnostic when it comes to OS choices - they all have pros and cons, just use whatever gets the job done.

Offline

#5 22 Jan 2007 15:35

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Fastest most efficient Command sequence to rename a set of files

Don't forget:

%1 -- a command line parameter passed to the script (or to a subroutine via the "call" command)

And:

!thisisalsoavariable! -- digressing a little, for delayed expansion of string variables (when enabled).


cmd | *sh | ruby | chef

Offline

Board footer

Powered by