2017-09-18 4 views
1

FOR 루프에 문자열을 추가하는 데 문제가 있습니다.배치 : FOR 루프에 문자열을 추가하는 방법

배열에 저장된 각 이름의 하위 폴더를 만들려고합니다. 하위 폴더는 사용자가 지정한 이름의 폴더에 속합니다. 예를 들어

, 나는 다음 내 배열의 이름이있는 경우 : - 폴더를 1 - 폴더 2 - 폴더 나 폴더를 갖고 싶어 3

:

C : \ MyFolder에 폴더 \ 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 

답변

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 
+0

감사합니다 여기

코드입니다! EnableDelayedExpansion의 목적을 이해하지 못하고 "! ...!"를 사용했습니다. 나는 당신의 적응증에 따라 내 질문의 답을 가지고 코드를 업데이트 할 것이다. –