![](/img/trans.png)
[英]Batch file to write filenames from a directory into a text file with specific format
[英]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.