Using windows batch file, I am trying to echo a list of file extensions in a folder. That will then be set as %var% and output to .txt file
@setlocal & @(for %%I in (*.*) do @set /a ext[%%~xI] = 1) & set ext[
from this post Windows command to get list of file extensions
My end game is I would like it to output like so
folder contents:
sample1.jpg
sample2.jpg
sample3.jpg
sample4.mp4
sample5.mp4
sample6.png
sample7.zip
sample8.zip
result:
.jpg, .mp4, .png, .zip
Any help is greatly appreciated, I hope I explained it clear enough
CodePudding user response:
You already have defined a variable for each extension. Just use another for /f
loop to concatenate the relevant part (the actual extension) of them:
@echo off
setlocal enabledelayedexpansion
for %%I in (*) do @set /a ext[%%~xI] = 1
for /f "tokens=2 delims=[]" %%a in ('set ext[') do set "var=%%a, !var!"
echo %var:~0,-2%
CodePudding user response:
@setlocal ENABLEDELAYEDEXPANSION&@(for %%I in (*.*) do @set /a ext[%%~xI] = 1&IF DEFINED ext[all] (IF "!ext[all]!" equ "!ext[all]: %%~xI=!" SET "ext[all]=!ext[all]!, %%~xI") ELSE (SET "ext[all]= %%~xI"))&SET "ext[all]=!ext[all]:~1!"&set ext[
Why you want this all on one line, I've no idea.
Since you need to access the changed value of a variable within a loop, you need delayedexpansion
. Note that Space.ext is appended each time a new extension is found, so there will be an extra space at the start of the list variable - hence the requirement to remove the first character before displaying the result.