I have to show the filenames using given template. I’ve written the following code:
if "%2" == "" (
echo "Missing second argument!"
set /p FileName="Input file name template ('*', '?' are allowed): "
set /p FileType="Input file type ('text', 'bat', 'all' only): "
if FileType == "all" (set FileType = "*")
) else (
set FileType="%2"
)
echo %DirSearch%\%FileName%.%FileType%
for %%i in (%DirSearch%\%FileName%.%FileType%) do (echo "Thats it: %%i")
If the second argument is empty, I ask user about filename template, extension (if its equal to ‘all’ I rewrite it’s value as ‘*’.
Now the first trouble is that it isn’t rewritten. When I put ‘all’ the ‘FileType’ is still ‘all’ after setting it to ‘*’. Why?
And echo shows up:
"C:\Folder"\test.all
"Thats it: "C:\Folder"\test.all"
How to interpretate it as single value and use in for?
New code:
if "%2" == "" (
...
if "%FileType%" == "all" (set FileType=*)
) else (
...
)
set result=%DirSearch%\%FileName%.%FileType%
echo %result%
for %%i in (%result%) do (echo "Thats it: %%i")
// echo %result%:
"C:\Data\test"\test.all
// in for cycle
"Thats it: "C:\Data\test"\test.all"
The right string should be: “C:\Data\test\test.all”
You are not testing the value of
FileTypein the correct manner. Also, you are not setting the new value in the correct manner. The code should readOtherwise, you are just comparing the strings “FileType” and “all”, which of course never succeeds.
Aside: You also seem to have some error in the code that sets
DirSearch; there’s an extra trailing double quote there that shouldn’t be.