批处理循环以从包含环境变量的列表创建文件夹

huangapple go评论52阅读模式
英文:

Batch loop to create folders from a list containing environment variables

问题

我有一个包含目录路径列表的文件。我想在这些位置创建目录。这些路径具有系统环境变量,使用系统属性->环境变量设置,如下所示:

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

我创建了一个小循环,应该按上面显示的方式创建目录:

@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

但这不会展开环境变量。我尝试关闭延迟扩展,但然后for循环中断,只创建一个目录。如何部分延迟扩展?

英文:

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?

答案1

得分: 1

你的文件中没有批处理文件"环境变量"...在继续之前,让我们假设变量的名称只是COMPLETE_DIR而不是$COMPLETE_DIR(这是在其他语言中访问变量值的方式)。

如果你将你的文件更改为以下内容:

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

...那么你可以使用完全相同的代码来使用你的变量。

这一行:

set "directory_name=%%~a"

...意味着在替换第一行读取的%%~a之后执行以下操作:

set "directory_name=!COMPLETE_DIR!/type1"

当然,在此之后,COMPLETE_DIR环境变量会像往常一样通过延迟扩展来展开!

英文:

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!

答案2

得分: 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

请尝试使用上面的脚本。

英文:
@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

huangapple
  • 本文由 发表于 2023年6月9日 07:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定