2

I'm having issues to append strings in FOR loop.

I want to create subfolders for each name stored in an array. The subfolders belong to a folder with a name given by user.

For example, If I have the following names in my array: - Folder 1 - Folder 2 - Folder 3

I want to have the folders:

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

As previous said, the "MyFolder" name is typed by user in the command line.

Here is the code:

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

1 Answer 1

4

Since you already have 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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was not understanding the purpose of EnableDelayedExpansion and using "!...!". I will update the code with the answer of my question following your indications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.