批处理文件,带有dir命令的字符串manuplutation

时间:2017-12-12 16:30:37

标签: batch-file

试图让它发挥作用。

从dir获取此行:3 File(s) 7,332,731,128 bytes并尝试获取要用于添加的文件数。

@Echo ON

dir %1 /a:h | find "File(s)" > hidden.txt
dir %1 | find "File(s)" > reg.txt

set /p hidden=<hidden.txt
echo %hidden%
IF /I "%hidden%"=="File Not Found" (
    Set hidden = 0
    ) Else (
Set hidden=%hidden~-29%)

echo %hidden%
Set /p reg=<reg.txt
IF /I "%hidden%"=="File Not Found" (
    set reg = 0
    ) ELSE (
    Set reg=%reg~-29%)
set /a total = %reg% + %hidden%

Echo The total number of files in the %1 directory is: %total%. This includes hidden files.
Echo.
Echo The total number of non-hidden files in the %1 directory is: %reg%.
Echo.
Echo The total number of hidden files in the %1 directory is: %hidden%

3 个答案:

答案 0 :(得分:1)

您不需要只会降低脚本速度的临时文件:

如果你想计算隐藏文件,试试这个

@echo off
set counter=0
for /f %%# in ('dir /a:h-d "%~1"') do (
  set /a counter=counter+1
)
echo hidden files=%counter%

计算所有文件

@echo off
set counter=0
for /f %%# in ('dir /a:-d "%~1"') do (
  set /a counter=counter+1
)
echo all files=%counter%

答案 1 :(得分:1)

这只是另一个For循环选项:

@For /F %%A In ('Dir/AH-D-L "%~1" 2^>Nul') Do @If Not "%%A"=="0" Set "fileCount=%%A"
@Echo %fileCount%
@Pause

删除-L,如果您不希望从文件计数中排除联结点。

答案 2 :(得分:0)

只有两个FOR循环,我得到了相同的结果。使用括号内的单引号之间的句子,我们可以执行命令并存储结果。

我留下了相同的结果文字,只是为了让你比较一下,看看这是不是你想要的:

<强> CODE

@echo off
setlocal

for /f %%A in ('dir "%~1" /a-h-d /b ^| find /V /C ""') do set "visCount=%%A"
for /f %%A in ('dir "%~1" /ah-d /b ^| find /V /C ""') do set "hidCount=%%A"

set /a totalCount = visCount + hidCount

echo.
echo The total number of files in the %1 directory is: %totalCount%. This includes hidden files.
echo.
echo The total number of non-hidden files in the %1 directory is: %visCount%.
echo.
echo The total number of hidden files in the %1 directory is: %hidCount%

使用DIR命令的/ B参数我每个文件只获得一行而不再有文本,而使用/ A我可以选择隐藏或不存在文件而没有目录。

然后SET / A命令让我们使用算术运算。