For Loop: Iteratively add strings to a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 12
Joined: 19 Apr 2019 07:53

For Loop: Iteratively add strings to a variable

#1 Post by TMC » 01 May 2019 12:30

Dear Forum,

as I stated in my other post : viewtopic.php?f=3&t=9116 I'm trying to process files in many sub folders.
I came up with this short script in the last few hours which does almost everything I want.

Code: Select all

@echo off

rem define original path (e.g. C:\original\*) - must exist
  set "originalPath=C:\original\*"

rem define output path (e.g c:\output\) must exist
  set "outputPath=C:\output\"

define CloudCompare path with first parameters -O = open and export format
  set "programPath="C:\Program Files\CloudCompare\cloudcompare.exe"-SILENT -C_EXPORT_FMT BIN "

rem first loop to process all sub folders in original folder
for /d %%A in (%originalPath%) do (
	set subFolderPath=%%A\*
	  echo Processing Files In Folder ... "%%~nxA"
rem second loop to loop through all files in sub folder  
	for %%F in (!subFolderPath!) do (
		set fileName=%%~nxA%.bin
		set "filesToMerge=-O %%F "
	rem set "parameter=-SAVE_CLOUDS ALL_AT_ONCE FILE: !outputPath!!fileName!"
	set "commandLine=%programPath%!filesToMerge!!parameter!"
	echo !commandLine!

The only thing I'm not able to do on my own right now is concatenating strings in the second for loop.
Let me try to explain what I want to achieve there:
In that loop I iterate over the files in a given sub folder (subFolderPath). In the end I want to store the path of every file into one single variable called "filesToMerge".
If there were two files in the sub folder the echo of the variable should be for example "-O C:\originals\1\Scan_01.bin -O C:\originals\1\Scan_03.bin"

Right now obviously only the last file path is stored in that variable. How can I concatenate every path of every file in the same sub folder into that or a different named variable?

In the end I want the batch to concatenate and then execute the following variables: commandLine = programPath + filesToMerge + parameter. This would be the first sub folder, and then the batch moves on to the next sub folder.

I hope this is not to confusing and one of you is able to help me with this!

Best Tobias

p.s.: if this topic needs to be moved to my original post, feel free to do so
p.p.s: I guess I can move the line "set fileName=%%~nxA%.bin" into the first loop right?

Posts: 12
Joined: 19 Apr 2019 07:53

Re: For Loop: Iteratively add strings to a variable

#2 Post by TMC » 02 May 2019 07:31

Solution is:

First Loop to empty "filesToMerge" variable for every folder

Code: Select all

set "filesToMerge="
second loop add the new filepath to the variable "filesToMerge" without overwriting existing content

Code: Select all

set "filesToMerge=!filesToMerge! -O %%F"
Best Tobias

Posts: 3639
Joined: 22 Jan 2010 18:01
Location: Germany

Re: For Loop: Iteratively add strings to a variable

#3 Post by aGerman » 02 May 2019 07:54


You have to be careful with your solution. Delayed expansion is enabled in order to concatenate your strings. Keep in mind that you'll get trouble if file names in %%F contain exclamation points. Another fact is that strings are limited to a length of 8191 characters. I don't have any workaround for the latter. So, it's just a heads up.


Post Reply