For a cmd
(batch file) solution
@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. 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. Then the dots are removed from that variable to get the required output.
String.endsWith()
method. – Braj