批处理:如何在FOR循环中追加字符串

时间:2017-09-18 12:52:21

标签: batch-file for-loop append concatenation

我遇到在FOR循环中附加字符串的问题。

我想为存储在数组中的每个名称创建子文件夹。 子文件夹属于具有用户名称的文件夹。

例如,如果我的数组中有以下名称:   - 文件夹1   - 文件夹2   - 文件夹3

我想要文件夹:

C:\ MyFolder \ Folder 1  C:\ MyFolder \ Folder 2  C:\ MyFolder \ Folder 3

如前所述,“MyFolder”名称由用户在命令行中输入。

以下是代码:

@echo off
@break off
@title Generate Billing Subfolders
@color 0a
@cls

setlocal EnableDelayedExpansion

SET "batch_path=%~dp0"
SET "first_folder=01. Folder1"
SET "second_folder=02. Folder2"
SET "third_folder=03. Folder3"

:: Create the new Working Data folder
SET /p new_folder_name= Enter Directory Name: 
SET "full_path=%batch_path%%new_folder_name%"

ECHO Working...

IF NOT EXIST ("%full_path%") (
  MKDIR %new_folder_name%
  IF "!errorlevel!" EQU "0" (
    ECHO Folder created successfully.
  ) ELSE (
    ECHO Error while creating folder.
  )
) ELSE (
  ECHO Folder already exists.
)

SET "folders_list="%first_folder%" "%second_folder%" "%third_folder%""


FOR %%f in (%folders_list%) DO (
    :: Displays the folder name in array correctly
    ECHO %%f 
    :: Displays ECHO is off. Why?
    CALL SET "updated_full_path=%full_path%\%%f"
    ECHO %updated_full_path%
    PAUSE


)
PAUSE
EXIT

1 个答案:

答案 0 :(得分:2)

由于您已经拥有enabled delayed expansion

setlocal EnableDelayedExpansion

....


FOR %%f in (%folders_list%) DO (
    :: Displays the folder name in array correctly
    ECHO %%f 
    :: Displays ECHO is off. Why?
    SET "updated_full_path=!full_path!\%%f"
    ECHO !updated_full_path!
    PAUSE


)
PAUSE