0

I have a file containing a list of directory paths. I want to create directories at those locations. The paths have system environment variables, set with System Properties ->Environment Variables. Like so:

$COMPLETE_DIR/type1
$COMPLETE_DIR/type1
$COMPLETE_DIR/type1
$INCOMPLETE_DIR/type2
$INCOMPLETE_DIR/type3
$INCOMPLETE_DIR/type4

I made a little loop that is supposed to create the directories as shown above:

@echo off
setlocal enabledelayedexpansion

set "input_file=FolderList.txt"

for /F "usebackq delims=" %%a in ("%input_file%") do (
    set "directory_name=%%~a"
    echo Creating directory: !directory_name!
    if exist "!directory_name!" (
        echo Directory already exists: !directory_name!
    ) else (
        mkdir "!directory_name!" || (
            echo Failed to create directory: !directory_name!
        )
    )
)

echo All directories created or checked successfully.
pause

But this doesn't expand the environment variables. I tried turning off the delayed expansion, but then the for loop breaks, and only one directory is created. How do I partially delay expansion?

Adam
  • 840
  • 6
  • 24
  • 1
    `$COMPLETE_DIR` and `$INCOMPLETE_DIR` are not batch file/cmd.exe variables, they're just strings, and therefore will not expand. Are you sure you're not mixing, things up with PowerShell, which uses that style of variable? – Compo Jun 09 '23 at 00:13
  • They are system environment variables, set with System Properties ->Environment Variables – Adam Jun 09 '23 at 01:11
  • Well if you have variables pre-defined, expandable as `%$COMPLETE_DIR%`, `%$INCOMPLETE_DIR%` and/or `!$COMPLETE_DIR!`, `!$INCOMPLETE_DIR!`, you would still need to parse each line and split off the leaf in order to isolate each. Or change your text file to use the appropriate variable syntax. You also need to [Edit] your question to include the information you have commented, before deleting that comment. You should also be aware that Windows uses backward slashes as path seperators, not forward slashes. – Compo Jun 09 '23 at 01:19

2 Answers2

1

Your file have not Batch file "environment variables"... Before continue, lets assume that the name of the variable is just COMPLETE_DIR and NOT $COMPLETE_DIR (that is the way to access the variable value in other language).

If you change your file to this:

!COMPLETE_DIR!/type1
!COMPLETE_DIR!/type2
!COMPLETE_DIR!/type3
!INCOMPLETE_DIR!/type2
!INCOMPLETE_DIR!/type3
!INCOMPLETE_DIR!/type4

... then you can use your variables with your exact same code.

This line:

    set "directory_name=%%~a"

... imply to execute this (after replacing the %%~a by the first line read):

    set "directory_name=!COMPLETE_DIR!/type1"

Of course, after that the COMPLETE_DIR environment variable is expanded via Delayed Expansion, as usual!

Aacini
  • 65,180
  • 12
  • 72
  • 108
0
@echo off
setlocal enabledelayedexpansion

set "input_file=FolderList.txt"

for /F "usebackq delims=" %%a in ("%input_file%") do (
    set "directory_name=%%~a"
    
    rem Expand environment variables using regular expansion
    call set "expanded_directory_name=!directory_name!"
    
    echo Creating directory: !expanded_directory_name!
    
    if exist "!expanded_directory_name!" (
        echo Directory already exists: !expanded_directory_name!
    ) else (
        mkdir "!expanded_directory_name!" || (
            echo Failed to create directory: !expanded_directory_name!
        )
    )
)

echo All directories created or checked successfully.
pause

Try using the above script

Naveed
  • 1
  • 3
  • Unfortunately, that didn't change anything – Adam Jun 09 '23 at 01:11
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 10 '23 at 00:56
  • In order for this to work, the lines in the file must be changed to something like this: `%COMPLETE_DIR%/type1` – Aacini Jun 14 '23 at 03:34