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
Less file name
Moderator: DosItHelp
Re: Less file name
There was a request similar to your's, here
Squashman provided a solution, This is his code but modified to work for your problem
This should work for you
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
Re: Less file name
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 problemCode: 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.
Re: Less file name
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.Running the posted code will attempt the followingand fail on renames #2, #3, and #5 with "duplicate file name exists".
Liviu
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
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"
Liviu
Re: Less file name
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
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
Re: Less file name
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.
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 . . .
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 . . .