简体   繁体   English

将唯一的文件扩展名扫描到变量中

[英]Scanning unique file extensions into a variable

I have a windows directory containing tens of thousands of files.我有一个包含数万个文件的 windows 目录。 I need a list of all the unique file extensions to be put in a variable.我需要将所有唯一文件扩展名的列表放入变量中。

In a java program, I would like to scan through that directory (recursively, including all sub-directories) and retrieve a list of unique file extensions that can be put into a variable.在 Java 程序中,我想扫描该目录(递归地,包括所有子目录)并检索可以放入变量的唯一文件扩展名列表。

Ex: Dir contains: File1.txt File2.doc File3.doc File4.doc File5.ppt I would like to retrieve "txt,doc,ppt" and put that into a string variable (They do not have to be separated in an array of any type - although that would work. I only need to end up with a string of them, like the one above).例如:Dir 包含:File1.txt File2.doc File3.doc File4.doc File5.ppt 我想检索“txt,doc,ppt”并将其放入字符串变量中(它们不必在数组中分开任何类型的 - 尽管这会起作用。我只需要以一串字符串结束,就像上面的那样)。

Is there anyway I can do this?无论如何我可以做到这一点吗? Possibly by accessing the command line or using regex?可能通过访问命令行或使用正则表达式?

Here is a Java 8 example as String:这是一个 Java 8 示例作为字符串:

        final String extensions = Files.walk(Paths.get(""))
                .map(Path::toString)
                .filter(pathString -> pathString.contains("."))
                .map(pathString -> pathString.substring(pathString.lastIndexOf('.') + 1, pathString.length()))
                .distinct()
                .collect(Collectors.joining(","));

        System.out.println(extensions);

As an array:作为数组:


     final String[] extensions = Files.walk(Paths.get(""))
                .filter(Files::isRegularFile)
                .map(Path::toString)
                .filter(pathString -> pathString.contains("."))
                .map(pathString -> pathString.substring(pathString.lastIndexOf('.') + 1, pathString.length()))
                .distinct()
                .toArray(String[]::new);

        System.out.println(Arrays.toString(extensions));

here is a batch-solution:这是一个批处理解决方案:

del %temp%\x.x 2>nul
for /f "tokens=*" %%i in ('dir /s /b /a-d *') do (find "%%~xi" %temp%\x.x ||<nul set/p .= %~xi>>x.x)
set /p ext=<%temp%\x.x
set ext=%ext:.=,%
set ext=%ext:~1%
echo %ext%

For a cmd (batch file) solution对于cmd (批处理文件)解决方案

@echo off

    setlocal enableextensions
    for /r "%cd%" %%a in (*) do if not defined "\%%~xa\" (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.如果未设置该变量,则这是第一次找到扩展名并将其回显到控制台。

edited to adapt to comments and to OP that i have misread.编辑以适应我误读的评论和OP。 The output needs to be in only one line输出只需要在一行

@echo off

    setlocal enableextensions
    for /r "%cd%" %%a in (*.*) do if not defined "\%%~xa\" (
        set ""\%%~xa\"=1" 
        if not defined "\" (set ""\"=1" ) else (<nul set /p ".=,")
        <nul set /p ".=%%~xa"
    )
    endlocal

Same working that the previous code, but in this case the output is keept in one line with commas added when needed to separate the elements in the extensions list与前面的代码相同的工作,但在这种情况下,输出保持在一行中,并在需要时添加逗号以分隔扩展列表中的元素

edited to properly format the output: remove the dots from extension and store the data in a variable编辑以正确格式化输出:从扩展中删除点并将数据存储在变量中

@echo off

    setlocal enableextensions disabledelayedexpansion 

    for /f "delims=" %%z in ('cmd /e:on /v:off /q /c "for /r "%cd%" %%a in (*.*) do if not defined "\%%~xa\" (set ""\%%~xa\"^=1" & if not defined "\" (set ""\"^=1" ) else (<nul set /p ".^=^,") & <nul set /p ".^=%%~xa" )"') do set "extensionList=%%z"
    set "extensionList=%extensionList:.=%"
    echo(%extensionList%

    endlocal

Still the same code, but to get the data inside a variable, all the previous logic has been moved inside a for command, so the list from previous version can be assigned to a variable.仍然是相同的代码,但是为了获取变量中的数据,所有以前的逻辑都已移到for命令中,因此可以将以前版本的列表分配给变量。 Then the dots are removed from that variable to get the required output.然后从该变量中删除点以获得所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM