我正在使用这个batch file扫描Windows文件系统,并保存该系统中所有文件扩展名的.txt文件:
命令行命令:
NameOfBatchFile.bat >List.txt
批文件代码:
@echo off set target=%1 if "%target%"=="" set target=%cd% setlocal EnableDelayedExpansion set LF=^ rem Previous two lines deliberately left blank for LF to work. for /f "tokens=*" %%i in ('dir /b /s /a:-d "\\PathOfMyWindowsDirectory"') do ( set ext=%%~xi if "!ext!"=="" set ext=FileWithNoExtension echo !extlist! | find "!ext!" > nul if not !ERRORLEVEL! == 0 set extlist=!extlist!!ext!: ) echo %extlist::=!LF!% endlocal
该代码在小文件夹上效果很好,但是如果我提供的文件夹中包含太多的子文件夹,命令行将进行处理,然后提供以下错误:
The command line is too long. The command line is too long. The command line is too long. The command line is too long. The command line is too long. The command line is too long. The command line is too long. The input line is too long.
我不能编辑文件系统来减less子文件夹,有没有人知道另一种方式来得到这个工作?
代码中的问题是变量内部元素的连接,这可能会生成一长串扩展名,这些扩展名将会结束生成一个极长的命令行。
@echo off setlocal enableextensions disabledelayedexpansion set "target=%~1" if "%target%"=="" set "target=%cd%" for /r "%target%" %%a in (*) do if not defined "\%%~xa\" ( if "%%~xa"=="" (echo(FileWithNoExtension) else (echo(%%~xa) set ""\%%~xa\"=1" ) endlocal
这使用环境来存储可见扩展的信息,通过为每个扩展设置一个变量。 如果变量没有被设置,这是第一次找到扩展名并被回显给控制台。
我认为这是解决这个问题的最快方法。
@echo off setlocal set target=%1 if "%target%"=="" set target=%cd% for /R "%target%" %%a in (*.*) do set ext[%%~Xa]=1 for /F "tokens=2 delims=[]" %%a in ('set ext[') do ( if "%%a" equ "=1" ( echo FileWithNoExtension ) else ( echo %%a ) )
以前的方法可以很容易地修改,以获得具有每个扩展名的文件的数量 ; 只需通过set /A ext[%%~Xa]+=1
修改set /A ext[%%~Xa]+=1
并相应地修改for /F
的标记。
这给你一个非常快速的所有扩展的排序列表。
@echo off set "target=%~1" if "%target%"=="" set target=%cd% dir "%target%" /b /s /ad |repl ".*(\..*)" "$1" |repl ".*\\.*" "FileWithNoExtension"|sort|uniq >file.txt
这使用一个称为repl.bat
(由dbenham)的帮助器批处理文件 – 下载: https ://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
这使用一个称为uniq.bat
的助手批处理文件(通过aacini) – 下载: https ://www.dropbox.com/s/71o38a4ljnqgqjh/uniq.bat
将repl.bat
和uniq.bat
放在与批处理文件相同的文件夹中或路径中的文件夹中。