且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

收集文件扩展名列表 - “命令行太长”解决方法

更新时间:2022-05-23 15:57:19

代码中的问题是变量中的元素连接,可以生成一个长列表的扩展结束生成过长的命令行。

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.