【问题标题】:Trying to extract string from another string in a batch file尝试从批处理文件中的另一个字符串中提取字符串
【发布时间】:2014-02-24 18:52:45
【问题描述】:

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

 @echo 关闭

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

我已经好几年没有做任何批处理编程了,所以我很生疏。我怎样才能使这项工作。输入文件的格式为MS2014_U01_C2_John-Doe.pdf。我想从变量%%G中提取01,从变量%%H中提取2。

提前感谢您的帮助 - 约翰

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    您可以在额外的内部 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
    )
    

    【讨论】:

      【解决方案2】:
      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 关键字的内联更改为小写。未经测试。

      【讨论】:

        猜你喜欢
        • 2012-12-04
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        相关资源
        最近更新 更多