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

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

Batch loop to create folders from a list containing environment variables

问题

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

  1. $COMPLETE_DIR/type1
  2. $COMPLETE_DIR/type1
  3. $COMPLETE_DIR/type1
  4. $INCOMPLETE_DIR/type2
  5. $INCOMPLETE_DIR/type3
  6. $INCOMPLETE_DIR/type4

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

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "input_file=FolderList.txt"
  4. for /F "usebackq delims=" %%a in ("%input_file%") do (
  5. set "directory_name=%%~a"
  6. echo Creating directory: !directory_name!
  7. if exist "!directory_name!" (
  8. echo Directory already exists: !directory_name!
  9. ) else (
  10. mkdir "!directory_name!" || (
  11. echo Failed to create directory: !directory_name!
  12. )
  13. )
  14. )
  15. echo All directories created or checked successfully.
  16. 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:

  1. $COMPLETE_DIR/type1
  2. $COMPLETE_DIR/type1
  3. $COMPLETE_DIR/type1
  4. $INCOMPLETE_DIR/type2
  5. $INCOMPLETE_DIR/type3
  6. $INCOMPLETE_DIR/type4

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

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "input_file=FolderList.txt"
  4. for /F "usebackq delims=" %%a in ("%input_file%") do (
  5. set "directory_name=%%~a"
  6. echo Creating directory: !directory_name!
  7. if exist "!directory_name!" (
  8. echo Directory already exists: !directory_name!
  9. ) else (
  10. mkdir "!directory_name!" || (
  11. echo Failed to create directory: !directory_name!
  12. )
  13. )
  14. )
  15. echo All directories created or checked successfully.
  16. 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(这是在其他语言中访问变量值的方式)。

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

  1. !COMPLETE_DIR!/type1
  2. !COMPLETE_DIR!/type2
  3. !COMPLETE_DIR!/type3
  4. !INCOMPLETE_DIR!/type2
  5. !INCOMPLETE_DIR!/type3
  6. !INCOMPLETE_DIR!/type4

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

这一行:

  1. set "directory_name=%%~a"

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

  1. 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:

  1. !COMPLETE_DIR!/type1
  2. !COMPLETE_DIR!/type2
  3. !COMPLETE_DIR!/type3
  4. !INCOMPLETE_DIR!/type2
  5. !INCOMPLETE_DIR!/type3
  6. !INCOMPLETE_DIR!/type4

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

This line:

  1. set "directory_name=%%~a"

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

  1. set "directory_name=!COMPLETE_DIR!/type1"

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

答案2

得分: 0

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "input_file=FolderList.txt"
  4. for /F "usebackq delims=" %%a in ("%input_file%") do (
  5. set "directory_name=%%~a"
  6. rem Expand environment variables using regular expansion
  7. call set "expanded_directory_name=!directory_name!"
  8. echo Creating directory: !expanded_directory_name!
  9. if exist "!expanded_directory_name!" (
  10. echo Directory already exists: !expanded_directory_name!
  11. ) else (
  12. mkdir "!expanded_directory_name!" || (
  13. echo Failed to create directory: !expanded_directory_name!
  14. )
  15. )
  16. )
  17. echo All directories created or checked successfully.
  18. pause

请尝试使用上面的脚本。

英文:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "input_file=FolderList.txt"
  4. for /F "usebackq delims=" %%a in ("%input_file%") do (
  5. set "directory_name=%%~a"
  6. rem Expand environment variables using regular expansion
  7. call set "expanded_directory_name=!directory_name!"
  8. echo Creating directory: !expanded_directory_name!
  9. if exist "!expanded_directory_name!" (
  10. echo Directory already exists: !expanded_directory_name!
  11. ) else (
  12. mkdir "!expanded_directory_name!" || (
  13. echo Failed to create directory: !expanded_directory_name!
  14. )
  15. )
  16. )
  17. echo All directories created or checked successfully.
  18. 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:

确定