gpt4 book ai didi

windows - 用于计算子目录中文件类型的批处理文件

转载 作者:可可西里 更新时间:2023-11-01 09:43:02 25 4
gpt4 key购买 nike

我需要一个批处理文件来计算子目录中的某些文件类型。在这种情况下,它是 .txt 和 .xls 文件。在文件末尾,它会报告这一点。

@echo off
REM get script directory
set scriptdir=%~dp0
REM change directory to script directory
cd /d %scriptdir%
setlocal
set txtcount=0
set xlscount=0
for %%x in ('dir *.txt /s ) do set /a txtcount+=1
for %%x in ('dir *.xls /s ) do set /a xlscount+=1
echo %txtcount% text files
echo %xlscount% .xls files
endlocal
pause

我的批处理文件没有返回正确的文件数。我认为它可能会持续计数,但我已将计数变量设置为本地,所以我不确定发生了什么。

最佳答案

您的脚本存在三个问题:

  • 你想遍历命令输出,所以你需要 FOR/F
  • 您没有使用“裸”/B 格式,即 DIR 输出的不仅仅是文件名
  • 你错过了两次结束单引号

用这个替换你的循环:

FOR /F %%X IN ('DIR /S /B *.txt') DO SET /A "txtcount+=1"
FOR /F %%X IN ('DIR /S /B *.xls') DO SET /A "xlscount+=1"

关于windows - 用于计算子目录中文件类型的批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41633338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com