1

I'm writing a batch file to automate downloading a game called dwarf fortress with a bunch of other files and images. I have everything working, but now I want to provide the user with the ability to choose a color set from a downloaded folder of .txt files. The trouble is, I want to provide the available color set names based on the file names, so I need to iterate over the folder and get the names of the files (without the .txt extension), and add them to a single string with the format filename, filename, ... etc. I am able to iterate over the files, but I cannot figure out how to make that single string. Here's what I have so far (I know it doesn't work):


set /a colorFileNames=""
for %%f in (%CD%\colors\colors\*) do (
    set %temp%=%%f
    set colorFileNames="%%colorFileNames%%, %temp%:~0,-3"
)

I then will use the colorFileNames in the question for the user.

0

I want to provide the available color set names based on the file names

I need to iterate over the folder and get the names of the files (without the .txt extension), and add them to a single string with the format filename, filename, ... etc

Use the following batch file (test.cmd):

@echo off
setlocal enabledelayedexpansion
set colorFileNames=
for %%f in (%CD%\colors\colors\*) do (
  set _file=%%~nf
  if [!colorFileNames!]==[] (
    set colorFileNames=!_file!
    ) else (
    set colorFileNames=!colorFileNames!, !_file!
    )
)
echo !colorFileNames!
endlocal

Example:

F:\test>dir /b colors\colors
red.txt
green.txt
blue.txt
sky blue pink.txt

F:\test>test
red, green, blue, sky blue pink
F:\test>

Further Reading

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.