尝试从批处理文件中的另一个字符串中提取字符串

时间:2014-02-24 18:52:46

标签: batch-file

我有一个for循环,循环遍历文本文件filelist.txt中包含的.pdf文件列表。从这些文件名中我提取了一系列令牌,这些令牌最终用于重命名某些输出文件。这个循环工作正常。但是我还需要将其中一些令牌变量子串到其他变量中,以便我可以构建一个“复制到”目录字符串。我已经尝试了以下但它不起作用,我怀疑因为后续“for”循环中的“in”源是一个令牌变量而不是一个文件。这是我目前的代码。

    @echo off

SET myPath=..\watch\ echo %myPath% cd ..\watch\ REM create a file containing all of the case pdfs to process dir /b /a-d *.pdf > filelist.txt REM for each filename in the file list for /f "tokens=1-5 delims=_." %%F in (filelist.txt) do ( echo Splitting and Encrypting %%F_%%G_%%H_%%I.%%J echo Cohort %%F echo Unit %%G echo Case %%H echo Case Name %%I echo Extension %%J REM Now determine the new location to copy entire output REM Extract Unit number REM for /f "tokens=1,2 delims=U" %%K in (%%G) do REM echo Original %%G REM echo Prefix %%K REM echo Unit Number %%L REM ) REM Extract Case number REM for /f "tokens=1,2 delims=C" %%O in ("%%G") do REM echo Prefix %%O REM echo Unit Number %%P REM ) ) cd ..\test

< \&预GT;

自从我完成任何批量编程以来已经有好几年了,所以我非常生疏。我怎样才能做到这一点。输入文件的格式为MS2014_U01_C2_John-Doe.pdf。我想从变量%%G中提取01,从变量%%H中提取2。

先谢谢你的帮助 - 约翰

2 个答案:

答案 0 :(得分:0)

您可以在其他内部for循环中使用不需要的字符作为分隔符来检索所需的数据

for /f "tokens=1-5 delims=_." %%F in (filelist.txt
) do for /f "delims=U" %%g in ("%%G"
) do for /f "delims=H" %%h in ("%%H"
) do (
   echo Splitting and Encrypting %%F_%%G_%%H_%%I.%%J
   echo Cohort %%F
   echo Unit %%G
   echo ... Unit Number %%g
   echo Case %%H
   echo ... Case Number %%h
   echo Case Name %%I
   echo Extension %%J
)

答案 1 :(得分:0)

REM Now determine the new location to copy entire output
REM Extract Unit number
rem
rem this must be executed in the context of the FOR...%%F...DO else %%G is out-of-context
rem note that %%G should be "quoted" else will try to read file with name %%G!
rem %%K will be set to the first group of characters following any initial "U"
rem and up to end or next "U" or sequence of "U"
rem %%L will be set to the second such group (if it exists)
rem note that " (" must follow "DO" to execute a block
rem
    for /f "tokens=1,2 delims=U" %%K in ("%%G") do (
       echo Original %%G
       echo Prefix %%K
       echo Unit Number %%L

REM Extract Case number
rem
rem this must be executed in the context of the FOR...%%F...DO else %%H is out-of-context
rem and also in the context of the FOR...%%K...DO else %%K,%%L are is out-of-context
rem note that %%H, not %%G contains the case
rem %%O will be set to the first group of characters following any initial "C"
rem and up to end or next "C" or sequence of "C"
rem %%P will be set to the second such group (if it exists)
rem note that " (" must follow "DO" to execute a block
rem
   for /f "tokens=1,2 delims=C" %%O in ("%%G") do (
    echo Splitting and Encrypting %%F_%%G_%%H_%%I.%%J
    echo Cohort %%F
    echo Unit %%G
    echo Case %%H
    echo Case Name %%I
    echo Extension %%J
    echo Prefix %%K
    echo Unit Number %%L
    echo Prefix %%O
    echo Unit Number %%P
rem this closes the FOR...%%O...DO (
    )
rem this closes the FOR...%%K...DO (
   )
rem this closes the FOR...%%F...DO (
)

更改评论内联rem个关键字小写。未经测试。