Batch file 批处理文件,使用dir命令创建字符串

Batch file 批处理文件,使用dir命令创建字符串,batch-file,Batch File,试着让它工作 从dir获取此行:3个文件7332731128字节,并尝试仅获取用于添加的文件数 @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

试着让它工作

从dir获取此行:
3个文件7332731128字节
,并尝试仅获取用于添加的文件数

@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%
@Echo ON
目录%1/a:h |查找“文件”>hidden.txt
目录%1 |查找“文件”>reg.txt

set/p hidden=您不需要只会降低脚本速度的临时文件:

如果要计算隐藏文件的数量,请尝试以下操作

@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%

这只是
循环选项的另一个

@For/F%%A In('Dir/AH-D-L“%~1”2^>Nul')如果不是,则执行“%%A”==“0”设置“文件计数=%%A”
@回显%fileCount%
@停顿

如果不希望从文件计数中排除连接点,请删除
-L

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

我留下了相同的结果文本,只是为了让您进行比较,看看这是否是您想要的:

代码

@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,我可以选择隐藏或不隐藏文件,没有目录


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

您可以使用
FOR/F
命令来读取您创建的输出文件,或者解析
DIR
FIND
命令的输出。默认情况下,输出将按第一个空格分割。因此,您可以将
FOR
变量分配给环境变量,以便稍后在脚本中使用。更好的选择是使用
FIND
命令的
/C
选项来计算文件数<代码>目录“%1”/a:h-d/b | find/c/v”“