簡體   English   中英

批處理文件以從文本中檢索文件名

[英]Batch file to retrieve filenames from a text

我有一個文本文件,其中有幾個重復使用的不同文件擴展名。 我想要一個批處理文件來提取所有文件名(.jpg,.txt)並創建一個列表。 我以前從未處理過批處理文件,但我認為這應該不會太困難。

樣品:

input.txt:

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

output.txt:

image.jpg
format.txt
image2.jpg

我在嘗試 :

@echo off
FINDSTR ".jpg" input.txt >> output.txt
FINDSTR ".txt" input.txt >> output.txt

但是我得到了整個字符串,而不僅僅是我想要的單詞。

@echo off
(
  for /f "tokens=*" %%a in (input.txt) do (
    for %%b in (%%a) do (
      echo %%b|findstr "\<.*[.]...\>" >nul && echo    %%b
    ) 
  )
)>output.txt

每一行(%% a)都要查看每個標記(%% b)。 如果它的格式為<start of string><some chars><dot><three chars><end of string> ,則假定為文件名。

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM