批量SUBSTRING文件名

时间:2015-07-22 07:21:39

标签: batch-file for-loop substring filenames

我需要帮助来制作批处理代码(如果可能的话)从filename获取子字符串。 我的文件名可以是(文件名长度正在改变):

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"

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

纯批次。简单的字符串操作与标记化混合。无需额外的实用程序。

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

答案 1 :(得分:1)

既然你说过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 "$1" -annotate "$2" -annotate2 "$3" -annotate3 "-$4"'}

根据您在问题中提供的文件名

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文件,您可以根据需要执行convert命令。

以下是该正则表达式的细分:

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

答案 2 :(得分:1)

@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

    )