用于从文本中检索文件名的批处理文件

时间:2015-06-11 18:13:23

标签: batch-file

我有一个文本文档,其中包含多个不同的文件扩展名。我想要一个批处理文件来提取所有文件名(.jpg,.txt)并创建一个列表。我之前从未使用批处理文件,但我认为这不应该太困难。

样品:

input.txt:

<pu2:commandButton id="btnPu2" 
                   onbegin="block(); console.log('begin')"                                                                    
                   oncomplete="unblock(); console.log('complete')"
                   onsuccess="console.log('success')"
                   execute="@form"
                   action="#{list.search()}"
                   render="@form :table-form :form-pagination"
                   value="Do it">
</pu2:commandButton>

output.txt:

Look at the image in image.jpg
Review the format.txt document before continuing
image2.jpg has some nice features

我在尝试:

image.jpg
format.txt
image2.jpg

但我得到了整个字符串,而不仅仅是我想要的字。

2 个答案:

答案 0 :(得分:0)

<start of string><some chars><dot><three chars><end of string>

为每一行(%% a)查看每个标记(%% b)。如果格式为SecItemCopyMatching(),则假定它是文件名。

答案 1 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

rem Define the target extensions *enclosed* by spaces (including one space at end)
set "extensions= .jpg .txt "

(
   rem Process just the lines that have an extension
   for /F "tokens=*" %%a in ('findstr "%extensions%" input.txt') do (
      set "line=%%a"
      rem Separate each word in this line, preserving special characters
      for %%b in ("!line: =" "!") do if %%b neq "" (
         set "word=%%~b"
         rem If the four last chars in the word is an extension...
         for /F %%c in ("!word:~-4!") do if "!extensions: %%c =!" neq "%extensions%" (
            rem report the word
            echo %%~b
         )
      )
   )
) > output.txt