如果文件名小于18位,我有一个在文件名中添加零的脚本。
例如,如果文件名是1234567
,则应将其重命名为000000000001234567
,如果是123456789
,则会将其000000000123456789
。我的代码已经在执行此功能
现在我的要求是,如果文件名是1234567-1
,那么它应该是000000000001234567_Type_1
,如果名称是1234567-a
,那么它应该将其重命名为000000000001234567_Type_a
这意味着它应该继续执行我的脚本所做的事情,但另外它应该在文件名中加上_Type_<last character>
。文件名可以是1234567
或123456789
或1234567-1
或12345678-b
或其他任何内容,但在连字符( - )之前总是有数字而不是字符。文件夹中可以有多个文件。连字符( - )应替换为下划线(_),后跟Type_
,并且文件名中的最后一个字符也是如此。
这是我现有的代码:
@echo off
setlocal enableextensions enabledelayedexpansion
rem iterate over tif files:
for %%f in (C:\Task\Drop_Files\*.tif) do (
rem store file name without extension
set FileName=%%~nf
rem Add leading zeroes:
set FileName=00000000000000000!FileName!
set FileName=!FileName:~-18!
set FileName=!FileName!%%~xf
rem Rename the file
rename "%%f" "!FileName!"
)
答案 0 :(得分:1)
以下脚本应该根据需要处理带有连字符后缀(如1234567-1
)和不带(如1234567
)的文件:
@echo off
setlocal enableextensions enabledelayedexpansion
rem Iterate over TIF files:
for %%f in ("%~dp0*.tif") do (
rem Store file name without extension:
set "FileName=%%~nf"
rem Split off hyphen and suffix:
set "Suffix=!FileName:*-=!"
if "!Suffix!"=="!FileName!" set "Suffix="
call set "FileName=%%FileName:-!Suffix!=%%"
if defined Suffix set "Suffix=_Type_!Suffix!"
rem Add leading zeroes:
set "FileName=00000000000000000!FileName!"
rem Build final file name:
set "FileName=!FileName:~-18!!Suffix!"
rem Rename the file:
rename "%%~ff" "!FileName!%%~xf"
)
我做了什么:
Suffix
,它将在连字符后面保留字符(例如1
,当文件名为1234567-1
时);包含连字符(如-1
)的后缀将从文件名中删除(如果-1
在文件名中出现多次,则会失败;)set
语法; 答案 1 :(得分:1)
简单。在原始代码中,就在“重命名”行之前,只需添加以下行:
set FileName=!FileName:-=_Type_!
就是这样!
答案 2 :(得分:0)
我认为以下略有修改的代码应该能够满足您的需求。基本上,您需要测试倒数第二个字符是否为短划线( - ),在这种情况下,您将其替换为 _Type _ 。 为此,您提取FileName的最后2个字符(不带扩展名),然后查看这两个提取的字符的第一个字符。如果它是破折号,则从原始文件名中删除最后两个字符,因为它们最终将被新的_Type_ [char]构造替换。
@echo off
setlocal enableextensions enabledelayedexpansion
rem iterate over tif files:
for %%f in (C:\tests\output*.txt) do (
rem store file name without extension
set FileName=%%~nf
set IsDash=!FileName:~-2!
set IsDash=!IsDash:~0,1!
IF "!IsDash!" == "-" (
SET FileType=_Type_!FileName:~-1!
SET FileName=!FileName:~0,-2!
) ELSE (
SET FileType=
)
rem Add leading zeroes:
set FileName=00000000000000000!FileName!
set FileName=!FileName:~-18!!FileType!
set FileName=!FileName!%%~xf
rem Rename the file
rename "%%f" "!FileName!"
)
答案 3 :(得分:0)
@echo off
setlocal enableextensions enabledelayedexpansion
rem Change current directory
cd /D C:\Task\Drop_Files
rem Iterate over tif files:
for /F "tokens=1-3 delims=-." %%f in ('dir /B *.tif') do (
rem Store file name without extension and leading zeroes:
set "FileName=00000000000000000%%f"
rem Get the right type and input name:
rem - If input name is "1234567.tif" then "%%h" is empty, so there is no Type part
rem - If input name is "1234567-1.tif" then "%%h" is "tif", so the Type is "_Type_%%g"
if "%%h" equ "" (
set "input=%%f.%%g"
set "type="
) else (
set "input=%%f-%%g.%%h"
set "type=_Type_%%g"
)
rem Rename the file
rename "!input!" "!FileName:~-18!!type!.tif"
)