尝试使用 Bat 文件获取目录和子目录中所有文件的行数
Trying to Get A Line Count for all Files in a Directory and Sub-Directories using a Bat File
我正在尝试获取我正在处理的这个 bat 文件,以便为我提供目录(以及所有子目录)中的文档列表以及每个文档的行数。
我已经能够让下面的代码工作,但它只为我提供文件夹根目录中的文档,none 子目录。我搜索了很多不同的论坛,但一直无法找到可行的答案。
@echo off
:start
cls
echo Enter full path to directory that you want to count files for:
set /P FilePath=
echo ..
echo ...
if exist %FilePath%\CountLines.csv del %FilePath%\CountLines.csv /q
for /f "usebackq delims=" %%a in (`find /v /c "" "%FilePath%"\*.*`) do echo %%a >> "%FilePath%"\CountLines.csv
pause()
goto start
这是我得到的输出,这是正确的,但它没有返回子目录中的文档
MD5HASH APPEARS ONCE.csv 1174690
Split.txt 4258
COUNTLINESINFILES.txt 1
@echo off
setlocal
echo Enter full path to directory that you want to count files for:
rem Get path of the directory to search.
set /P "FilePath=" || (
>&2 echo Invalid reply.
exit /b 1
)
rem Check if the dir path exists.
if not exist "%FilePath%\" (
>&2 echo Invalid path.
exit /b 1
)
echo ..
echo ...
rem Get count of lines of files in the dir path.
(
for /r "%FilePath%" %%A in (*) do (
find /v /c "" "%%~A"
)
) > "%FilePath%\CountLines.csv"
echo Results:
type "%FilePath%\CountLines.csv"
pause
如果在脚本中设置变量,setlocal
可以帮助
将变量保留在脚本的本地。
setlocal
已添加。
考虑如果用户按下 return,没有
输入任何内容,那么这可能是无效路径,否则 a
可以解释以单个反斜杠开头的路径
就像从驱动器的根目录一样。我不认为用户可能
考虑到 ||
是一个失败检查,并执行
括号中的代码(如果失败)。
在继续之前检查回复的路径是否有效。
for /r
循环将递归迭代
*
的模式。 find
将读取每个文件和
标准输出被写入文件。与整个for /r
在括号内,允许写入所有标准输出
在关闭文件句柄之前归档。
最后使用type
显示结果。
请参阅 for /?
以帮助理解 for /r
以及所有其他类型的 for
循环。
我正在尝试获取我正在处理的这个 bat 文件,以便为我提供目录(以及所有子目录)中的文档列表以及每个文档的行数。
我已经能够让下面的代码工作,但它只为我提供文件夹根目录中的文档,none 子目录。我搜索了很多不同的论坛,但一直无法找到可行的答案。
@echo off
:start
cls
echo Enter full path to directory that you want to count files for:
set /P FilePath=
echo ..
echo ...
if exist %FilePath%\CountLines.csv del %FilePath%\CountLines.csv /q
for /f "usebackq delims=" %%a in (`find /v /c "" "%FilePath%"\*.*`) do echo %%a >> "%FilePath%"\CountLines.csv
pause()
goto start
这是我得到的输出,这是正确的,但它没有返回子目录中的文档
MD5HASH APPEARS ONCE.csv 1174690
Split.txt 4258
COUNTLINESINFILES.txt 1
@echo off
setlocal
echo Enter full path to directory that you want to count files for:
rem Get path of the directory to search.
set /P "FilePath=" || (
>&2 echo Invalid reply.
exit /b 1
)
rem Check if the dir path exists.
if not exist "%FilePath%\" (
>&2 echo Invalid path.
exit /b 1
)
echo ..
echo ...
rem Get count of lines of files in the dir path.
(
for /r "%FilePath%" %%A in (*) do (
find /v /c "" "%%~A"
)
) > "%FilePath%\CountLines.csv"
echo Results:
type "%FilePath%\CountLines.csv"
pause
如果在脚本中设置变量,setlocal
可以帮助
将变量保留在脚本的本地。
setlocal
已添加。
考虑如果用户按下 return,没有
输入任何内容,那么这可能是无效路径,否则 a
可以解释以单个反斜杠开头的路径
就像从驱动器的根目录一样。我不认为用户可能
考虑到 ||
是一个失败检查,并执行
括号中的代码(如果失败)。
在继续之前检查回复的路径是否有效。
for /r
循环将递归迭代
*
的模式。 find
将读取每个文件和
标准输出被写入文件。与整个for /r
在括号内,允许写入所有标准输出
在关闭文件句柄之前归档。
最后使用type
显示结果。
请参阅 for /?
以帮助理解 for /r
以及所有其他类型的 for
循环。