【发布时间】:2019-02-28 10:53:56
【问题描述】:
我有几个 txt 文件,其中包含具有不同参数的同一组变量。我创建了一个批处理来读取文件夹中的所有 txt 文件并创建一个选择列表。到目前为止,这工作正常。
现在我想从选择列表中排除某些 txt 文件,因为它们不包含变量(即 readme.txt、information.txt)。
这是我目前得到的代码:
@echo off
setlocal enabledelayedexpansion
set count=0
:: Read in files
for %%x in (*.txt) do (
set /a count=count+1
set choice[!count!]=%%x
)
echo.
echo Select one:
echo.
:: Print list of files
for /l %%x in (1,1,!count!) do (
echo %%x] !choice[%%x]!
)
echo.
:: Retrieve User input
set /p select=?
echo.
:: Print out selected filename
echo You chose !choice[%select%]!
for /f "delims=" %%A in ("!choice[%select%]!") do endlocal & set "VAR=%%~A"
:: Read variables from configuration file.
for /f "delims== tokens=1,2" %%G in (%VAR%) do set %%G=%%H
有人可以帮我排除上面提到的文件 readme.txt 等,这些文件位于带有参数文件(即 param1.txt、param2.txt)的文件夹中吗?
【问题讨论】:
-
欢迎来到 StackOverflow。你试过什么了?请提供Minimal, Complete and Verifiable Example of your problem
-
使用
If语句,例如If /I Not "%%~nx"=="information" If /I Not "%%~nx"=="readme" (Set /A...或使用FindStr语句,例如For /F Delims^=^ EOL^= %%x in ('Dir/B/A-D-L *.txt 2^>Nul^|FindStr/BIV "information\. readme\."')Do ( -
@compo 不应该是
/ive并包含扩展名吗? -
我本可以这样做@LotPings,但是,我提供的是想法,而不是答案。即使这样也不够健壮,因为如您所知,
Dir *.txt不只是拾取.txt文件,因此您的代码将计数并包含readme.txtz,更不用说忽略pleasereadme.txt和@ 987654332@. -
感谢@Compo 您的意见。我去找了 LotPings 提供的代码。
标签: list batch-file select