Less file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Less file name

#1 Post by mohdfraz » 14 Aug 2012 03:30

Hi,

I have a file 431.txt I need a batch file code to make it 1 number less like 430.txt

This file number could be any 322.txt or 1.txt or 32.txt

Many thanks for your help

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Less file name

#2 Post by abc0502 » 14 Aug 2012 06:20

There was a request similar to your's, here

Squashman provided a solution, This is his code but modified to work for your problem

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%G in (*.txt) do (
   set num=
   set base=%%~nG
   set ext=%%~xG
   set /a num=base - 1
   rename "%%G" "!num!!ext!"
)


This should work for you

mohdfraz
Posts: 69
Joined: 29 Jun 2011 11:16

Re: Less file name

#3 Post by mohdfraz » 15 Aug 2012 07:22

abc0502 wrote:There was a request similar to your's, here

Squashman provided a solution, This is his code but modified to work for your problem

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%G in (*.txt) do (
   set num=
   set base=%%~nG
   set ext=%%~xG
   set /a num=base - 1
   rename "%%G" "!num!!ext!"
)


This should work for you



abc0502, Many thanks, It worked like a charm. Did not know it was solved before. Thank you.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Less file name

#4 Post by Liviu » 16 Aug 2012 15:43

I do believe the given answer was good as to put the OP on the right track. The given solution does handle the case of files having all-digit names of the same length, not starting with a 0. Whether this completely resolves the OP's problem is hard to tell since there are always assumptions left untold. Whether it is a general solution applicable in all cases, no.

There are possible issues due to numbers starting with a 0 being taken as octal, and filenames being enumerated in lexicographical (not numerical) order, where "10.txt" may come before "9.txt". For example consider a directory with the following files in it.

Code: Select all

C:\tmp>dir /b *.txt
009.txt
012.txt
10.txt
9.txt
abc.txt
Running the posted code will attempt the following

Code: Select all

rename "009.txt" "-1.txt"
rename "012.txt" "9.txt"
rename "10.txt" "9.txt"
rename "9.txt" "8.txt"
rename "abc.txt" "-1.txt"
and fail on renames #2, #3, and #5 with "duplicate file name exists".

Liviu

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Less file name

#5 Post by abc0502 » 16 Aug 2012 16:26

thanks for the notes, Squashman provided a solution here for the files that start with 0 or 1,2,..etc and it works great.
and all what i did is just removing un-wanted features.

but if there is files with letters only that is a new one :)
thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Less file name

#6 Post by foxidrive » 16 Aug 2012 18:29

There is also another issue with the plain for in do command where it will process files twice, or more times.

This works here given the limitations stated: but the filenames can only be as large as batch math can handle within the set /a command.

Filename collisions are not handled either.

Code: Select all

@echo off
rem.>009.txt
rem.>012.txt
rem.>10.txt
rem.>9.txt
rem.>abc.txt
dir *.txt /b
echo =================

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir *.txt /b') do (
for /f "delims=0123456789" %%b in ("%%~na:") do (
if "%%b"==":" (   
   set "base=1%%~na"
   set /a base=base-1
   rename "%%a" "!base:~1!%%~xa"
)
)
)

dir *.txt /b
pause
del *.txt



009.txt
012.txt
10.txt
9.txt
abc.txt

=================
008.txt
011.txt
09.txt
8.txt
abc.txt

Press any key to continue . . .

Post Reply