Reputation: 1
This is my first post as I've always been able to find help from previous threads here.
My issue sounds simple but I am having a hard time with it and very much appreciate any help.
I am trying to write a batch to simply increment the last character of all drawing files, .dwg
, in a folder. The number represents the revision of the drawing. The Revision order is an A,0,1,2,3,4,...etc.
so I'm not sure if it is more difficult changing an a
to a 0
as well as incrementing numbers.
I am using Windows 7 and all files are named in this manner:
SAJ9495FA2006PLCE020140601-A
needs to change to
SAJ9495FA2006PLCE020140601-0SAH7830FA2006PLCE020110101-0
needs to change to
SAH7830FA2006PLCE020110101-1SA8096FA2006PLCE020104001-5
needs to change to
SA8096FA2006PLCE020104001-6
Thank you or any input and for your time.
Edit:
The code below works, however it removes the -
from filename and I would like it stay. Also it copies the files to a processed folder and I'd prefer they stay as well. Not sure how to edit it any further...
@Echo off
setlocal enabledelayedexpansion
mkdir processed
mkdir temp
set ext=dwg
for /f "tokens=*" %%A in ('dir /b *.!ext!') do (
set file=%%~nA
set name=!file:~0,-2!
set /a num=!file:~-1!+1
xcopy "%%A" "temp" > nul
ren "temp\%%A" "!name!!num!.!ext!" > nul
move "temp\!name!!num!.dwg" "processed" > nul
Echo %%A -^> !name!!num!.!ext!
)
Upvotes: 0
Views: 1021
Reputation: 34909
This task is not that trivial, because you have to consider some issues:
-
);So this is my approach, regarding all these things (see all the explanatory remarks):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~1" & rem // (provide path to root directory as command line argument)
set "_MASK=*-*.dwg" & rem // (mask to find files to process)
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (path to temporary file)
rem // Write list of files to rename to temporary file:
> "%_TMPF%" (
rem // Loop through all matching files:
for /F "delims= eol=|" %%F in ('dir /B /A:-D "%_ROOT%\%_MASK%"') do (
rem // Store original file name:
set "FILE=%%F"
setlocal EnableDelayedExpansion
rem // Extract last `-`-separated part from file name:
for %%I in ("!FILE:-=" "!") do set "LAST=%%~I"
rem // Extract file name with the last `-`-separated part removed:
set "REST=!FILE!|" & for %%J in ("!LAST!") do set "REST=!REST:-%%~J|=!"
rem // Remove any leading zeros from the past part:
for /F "tokens=* delims=0" %%E in ("!LAST!") do set "LAST=%%E"
if not defined LAST set /A "LAST=0"
rem // Increment last part:
if /I "!LAST!"=="A" (set /A "NEXT=0") else set /A "NEXT=LAST+1"
rem // Left-zero-pad incremented last part:
set "NPAD=0000000000!NEXT!" & set "NPAD=!NPAD:~-10!"
rem /* Return list with three items:
rem # file name without last part + `-` + zero-padded incremented last part;
rem # original file name;
rem # file name without last part + `-` + incremented last part (non-padded);
rem this is then needed for alphabetical sorting in descending order: */
echo(!REST!-!NPAD!^|!FILE!^|!REST!-!NEXT!
endlocal
)
)
rem // Loop through sorted temporary file:
for /F "tokens=2* delims=| eol=|" %%G in ('sort /R "%_TMPF%"') do (
rem // Actually rename a file:
ECHO ren "%_ROOT%\%%G" "%%H"
)
rem // Clean up temporary file:
del "%_TMPF%"
endlocal
exit /B
Remove the upper-case ECHO
in front of the ren
command to actually rename files.
Upvotes: 1
Reputation:
A solution preserving/reinserting the -
It uses a simple for iterating the matching files and a for /f
to split the name at the -
@Echo off
setlocal enabledelayedexpansion
set ext=dwg
set A=-1
for %%A in (*-?.%ext%) do for /f "tokens=1,2delims=-" %%B in ("%%~nA") do (
set /a num=%%C + 1
Echo Ren "%%A" "%%B-!num!%%~xA"
Rem Ren "%%A" "%%B-!num!%%~xA" > nul
)
Sample output:
Ren "SA8096FA2006PLCE020104001-5.dwg" "SA8096FA2006PLCE020104001-6.dwg"
Ren "SAJ9495FA2006PLCE020140601-A.dwg" "SAJ9495FA2006PLCE020140601-0.dwg"
Ren "SAH7830FA2006PLCE020110101-0.dwg" "SAH7830FA2006PLCE020110101-1.dwg"
If the output is OK remove the Rem in front of the Ren command.
Upvotes: 1
Reputation: 1
My Solution:
@Echo off
setlocal enabledelayedexpansion
set ext=dwg
for /f "tokens=*" %%A in ('dir /b *.!ext!') do (
set file=%%~nA
set name=!file:~0,-1!
set /a num=!file:~-1!+1
ren "%%A" "!name!!num!.!ext!" > nul
Echo %%A -^> !name!!num!.!ext!
)
Upvotes: -1