M. Coutinho
M. Coutinho

Reputation: 305

Batch: How to append string in FOR loop

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

Upvotes: 2

Views: 4445

Answers (1)

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 4

Related Questions