【问题标题】:Batch: Exclude certain files from select list批处理:从选择列表中排除某些文件
【发布时间】: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


【解决方案1】:

解析由 findstr 过滤的 dir cmd 的输出以排除文件名

:: Read in files
for /f "delims=" %%x in ('
    Dir /B /A-D *.txt ^|findstr /ive "readme\.txt information\.txt"
') do (
  set /a count+=1
  set choice[!count!]=%%x
)

【讨论】:

  • 谢谢@LotPings。此代码完美运行,仅添加了更多文本。
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 2021-07-16
  • 2021-07-30
相关资源
最近更新 更多