批量SUBSTRING一个文件名
SUBSTRING a filename in batch
我需要帮助来制作批处理代码(如果可能的话)以从文件名中获取子字符串。
我的文件名可以像(文件名长度正在改变):
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
文件编号 - 从左到第一 _
id1 - 从 1 到 n 的字符串,带 _ 分隔符;例如 C_C1_C2_C3_C4
id2 - 始终为 9 位数字; for example 011122558
日期 - 例如 2015-07-07
分机 .jpg
如何为文件夹中的所有文件名循环子字符串(文件号、id1、d2、日期)并将其放入我的代码
convert - "file number" -annotate "id1" -annotate2 "id2" -annotate "date"
例如:
convert - "01" -annotate "C_C1" -annotate2 "012345678" -annotate "2015-07-07"
感谢您的帮助。
纯批次。简单的字符串操作与标记化相结合。无需额外的实用程序。
(g.txt
包含您的示例文件名;可以替换为 'dir /b /a-d'
)
@echo off
for /f %%i in (g.txt) do call :process %%i
goto :eof
:process
set x=%1
set ext=%x:*.=%
for /f "delims=_" %%i in ("%x%") do set fileno=%%i
for /f "tokens=1,*delims=-" %%i in ("%x%") do (
set x1=%%i
set x2=%%j
)
for /f "tokens=1,* delims=." %%i in ("%x2%") do (
set dat=%%i
set ext=%%j
)
set id2=%x1:~-9%
for /f "tokens=1,* delims=_" %%i in ("%x1:~0,-10%") do set id1=%%j
echo filename %x%
echo ------------------------
echo Nr. %fileno%
echo ID1 %id1%
echo ID2 %id2%
echo Date %dat%
echo Ext. %ext%
echo ------------------------
echo convert - "%fileno%" -annotate "%id1%" -annotate2 "%id2% -annotate "%dat%"
echo(
echo(
goto :eof
既然你说 Windows 7,我知道你有可用的 Powershell。这是一个 Powershell 脚本:
$re = '^(\d+)_((?:(?:[a-zA-Z0-9]+)_?)+)_(\d{9})-(\d{4}-\d\d-\d\d)\.(\w+)$'
dir | ForEach-Object {$_ -replace $re, 'convert "" -annotate "" -annotate2 "" -annotate3 "-"'}
鉴于您在问题中提供的文件名
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
它将产生这个文本输出:
convert "100" -annotate "C_CCC1_C2_C3_C4" -annotate2 "055555555" -annotate4 "2015-07-07"
convert "10" -annotate "D_D1" -annotate2 "011122558" -annotate4 "2015-07-07"
convert "7" -annotate "D_D1" -annotate2 "012345678" -annotate4 "2015-07-07"
convert "8" -annotate "A" -annotate2 "087654321" -annotate4 "2015-07-07"
(文件名排在最前面,所以100开头的在前,8开头的在后)
通过将此文本输出重定向到 .cmd 文件,您可以根据需要执行转换命令。
下面是该正则表达式的分解:
Beginning of line or string
[1]: A numbered capture group. [\d+]
Any digit, one or more repetitions
_
[2]: A numbered capture group. [(?:(?:[a-zA-Z0-9]+)_?)+]
Match expression but don't capture it. [(?:[a-zA-Z0-9]+)_?], one or more repetitions
(?:[a-zA-Z0-9]+)_?
Match expression but don't capture it. [[a-zA-Z0-9]+]
Any character in this class: [a-zA-Z0-9], one or more repetitions
_, zero or one repetitions
_
[3]: A numbered capture group. [\d{9}]
Any digit, exactly 9 repetitions
-
[4]: A numbered capture group. [\d{4}-\d\d-\d\d]
\d{4}-\d\d-\d\d
Any digit, exactly 4 repetitions
@echo off
setlocal enableextensions disabledelayedexpansion
rem For each file
for /r "x:\starting\folder" %%z in (*.pdf) do (
rem Separate number part
for /f "tokens=1,* delims=_" %%a in ("%%~nz") do (
set "_number=%%~a"
set "_file=%%~fz"
rem Separate date and ids
for /f "tokens=1,* delims=-" %%c in ("%%~b") do (
set "_date=%%~d"
set "_ids=%%~c\."
)
)
rem Separate id1 from id2 handling the string as a path
rem This way id2 is the last element and the path to it
rem is id1
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("::!_ids:_=\!") do (
endlocal
set "_id2=%%~nxe"
set "_id1=%%~pe"
)
rem Correct id1 contents (it is a path) changing backslashes
rem to underscores. As there are initial and ending backslashes,
rem later we will remove the initial and ending underscores
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("!_id1:\=_!") do (
endlocal
set "_id1=%%~e"
)
rem Execute final command
setlocal enabledelayedexpansion
echo(
echo file[!_file!]
echo convert - "!_number!" -annotate "!_id1:~1,-1!" -annotate2 "!_id2!" -annotate "!_date!"
endlocal
)
我需要帮助来制作批处理代码(如果可能的话)以从文件名中获取子字符串。 我的文件名可以像(文件名长度正在改变):
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
文件编号 - 从左到第一 _
id1 - 从 1 到 n 的字符串,带 _ 分隔符;例如 C_C1_C2_C3_C4
id2 - 始终为 9 位数字; for example 011122558
日期 - 例如 2015-07-07
分机 .jpg
如何为文件夹中的所有文件名循环子字符串(文件号、id1、d2、日期)并将其放入我的代码
convert - "file number" -annotate "id1" -annotate2 "id2" -annotate "date"
例如:
convert - "01" -annotate "C_C1" -annotate2 "012345678" -annotate "2015-07-07"
感谢您的帮助。
纯批次。简单的字符串操作与标记化相结合。无需额外的实用程序。
(g.txt
包含您的示例文件名;可以替换为 'dir /b /a-d'
)
@echo off
for /f %%i in (g.txt) do call :process %%i
goto :eof
:process
set x=%1
set ext=%x:*.=%
for /f "delims=_" %%i in ("%x%") do set fileno=%%i
for /f "tokens=1,*delims=-" %%i in ("%x%") do (
set x1=%%i
set x2=%%j
)
for /f "tokens=1,* delims=." %%i in ("%x2%") do (
set dat=%%i
set ext=%%j
)
set id2=%x1:~-9%
for /f "tokens=1,* delims=_" %%i in ("%x1:~0,-10%") do set id1=%%j
echo filename %x%
echo ------------------------
echo Nr. %fileno%
echo ID1 %id1%
echo ID2 %id2%
echo Date %dat%
echo Ext. %ext%
echo ------------------------
echo convert - "%fileno%" -annotate "%id1%" -annotate2 "%id2% -annotate "%dat%"
echo(
echo(
goto :eof
既然你说 Windows 7,我知道你有可用的 Powershell。这是一个 Powershell 脚本:
$re = '^(\d+)_((?:(?:[a-zA-Z0-9]+)_?)+)_(\d{9})-(\d{4}-\d\d-\d\d)\.(\w+)$'
dir | ForEach-Object {$_ -replace $re, 'convert "" -annotate "" -annotate2 "" -annotate3 "-"'}
鉴于您在问题中提供的文件名
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
它将产生这个文本输出:
convert "100" -annotate "C_CCC1_C2_C3_C4" -annotate2 "055555555" -annotate4 "2015-07-07"
convert "10" -annotate "D_D1" -annotate2 "011122558" -annotate4 "2015-07-07"
convert "7" -annotate "D_D1" -annotate2 "012345678" -annotate4 "2015-07-07"
convert "8" -annotate "A" -annotate2 "087654321" -annotate4 "2015-07-07"
(文件名排在最前面,所以100开头的在前,8开头的在后)
通过将此文本输出重定向到 .cmd 文件,您可以根据需要执行转换命令。
下面是该正则表达式的分解:
Beginning of line or string
[1]: A numbered capture group. [\d+]
Any digit, one or more repetitions
_
[2]: A numbered capture group. [(?:(?:[a-zA-Z0-9]+)_?)+]
Match expression but don't capture it. [(?:[a-zA-Z0-9]+)_?], one or more repetitions
(?:[a-zA-Z0-9]+)_?
Match expression but don't capture it. [[a-zA-Z0-9]+]
Any character in this class: [a-zA-Z0-9], one or more repetitions
_, zero or one repetitions
_
[3]: A numbered capture group. [\d{9}]
Any digit, exactly 9 repetitions
-
[4]: A numbered capture group. [\d{4}-\d\d-\d\d]
\d{4}-\d\d-\d\d
Any digit, exactly 4 repetitions
@echo off
setlocal enableextensions disabledelayedexpansion
rem For each file
for /r "x:\starting\folder" %%z in (*.pdf) do (
rem Separate number part
for /f "tokens=1,* delims=_" %%a in ("%%~nz") do (
set "_number=%%~a"
set "_file=%%~fz"
rem Separate date and ids
for /f "tokens=1,* delims=-" %%c in ("%%~b") do (
set "_date=%%~d"
set "_ids=%%~c\."
)
)
rem Separate id1 from id2 handling the string as a path
rem This way id2 is the last element and the path to it
rem is id1
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("::!_ids:_=\!") do (
endlocal
set "_id2=%%~nxe"
set "_id1=%%~pe"
)
rem Correct id1 contents (it is a path) changing backslashes
rem to underscores. As there are initial and ending backslashes,
rem later we will remove the initial and ending underscores
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("!_id1:\=_!") do (
endlocal
set "_id1=%%~e"
)
rem Execute final command
setlocal enabledelayedexpansion
echo(
echo file[!_file!]
echo convert - "!_number!" -annotate "!_id1:~1,-1!" -annotate2 "!_id2!" -annotate "!_date!"
endlocal
)