英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论