收集文件扩展名列表 - “命令行太长”解决方法 [英] Gathering list of file extensions - "The command line is too long" workaround
问题描述
我目前正在使用此批处理文件扫描Windows文件系统,并保存该系统中所有文件扩展名的.txt文档:
I am currently using this batch file to scan through a Windows file system and save a .txt document of all the file extensions in that system:
Cmd命令行:
NameOfBatchFile.bat >List.txt
BatchFile代码:
BatchFile Code:
@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 code works great on small folders but if I provide it a folder with too many subfolders, the command line will process then provide the following error:
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.
我无法编辑文件系统以减少子文件夹,有没有人知道另一种方式?
I can't edit the filesystem to decrease subfolders, does anyone know another way to get this to work?
推荐答案
代码中的问题是变量中的元素连接,可以生成一个长列表的扩展结束生成过长的命令行。
The problem in your code is the concatenation of elements inside a variable, that can generate a long list of extensions that will end generating a excesively long command line.
@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
这里使用环境通过为每个扩展设置一个变量来存储扩展的信息。如果变量没有设置,这是第一次找到扩展,并被回显到控制台。
This uses the environment to store the information of seen extensions by setting a variable for each one. If the variable is not set, this is the first time the extension is found and is echoed to console.
这篇关于收集文件扩展名列表 - “命令行太长”解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!