这是我的第一篇文章,因为我一直都能从这里的先前主题中找到帮助。
我的问题听起来很简单,但是我很难解决,非常感谢您的帮助。
我正在尝试编写批处理,以简单地递增文件夹中所有图形文件.dwg
的最后一个字符。数字代表图纸的修订版本。修订顺序为A,0,1,2,3,4,...etc.
,所以我不确定将a
更改为0
以及递增数字是否更困难。
我正在使用Windows 7,并且所有文件都以这种方式命名:
SAJ9495FA2006PLCE020140601-A
需要更改为
SAJ9495FA2006PLCE020140601-0SAH7830FA2006PLCE020110101-0
需要更改为
SAH7830FA2006PLCE020110101-1SA8096FA2006PLCE020104001-5
需要更改为
SA8096FA2006PLCE020104001-6
感谢您或任何投入,并抽出宝贵的时间。
编辑:
下面的代码有效,但是它从文件名中删除了-
,我希望它保留下来。它还将文件复制到已处理的文件夹中,我希望它们也保留下来。不知道如何进一步编辑...
@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!
)
答案 0 :(得分:1)
保留/重新插入-
它使用一个简单的方法来迭代匹配的文件,并使用for /f
在-
处拆分名称
@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
)
示例输出:
Ren "SA8096FA2006PLCE020104001-5.dwg" "SA8096FA2006PLCE020104001-6.dwg"
Ren "SAJ9495FA2006PLCE020140601-A.dwg" "SAJ9495FA2006PLCE020140601-0.dwg"
Ren "SAH7830FA2006PLCE020110101-0.dwg" "SAH7830FA2006PLCE020110101-1.dwg"
如果输出正常,请删除Ren命令前面的Rem。
答案 1 :(得分:1)
此任务并非那么简单,因为您必须考虑一些问题:
-
); 这是我针对所有这些问题的处理方法(请参见所有说明性注释):
@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
删除ECHO
命令前面的大写ren
,以实际重命名文件。
答案 2 :(得分:-1)
我的解决方案:
@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!
)