Page 1 of 1

Creating seperate output-files [SOLVED]

Posted: 06 Aug 2021 14:37
by AlphaInc.
Hello everybody,

I'm working on a script to output the information given by mediainfo using a specific template.
I have a working script which merges every output together but now I want to customize it, so that for every mkv-file in my folder it creates a seperate output-file (for example if there are 7 mkvs I want 7 text-files with an increasing filename) but I've not managed to do so:

Code: Select all

@echo off
set /a fileCounter=0
	for %%A in (*video*.mkv) do (
		set /a fileCounter=fileCounter+1
		C:\System\Mediainfo\MediaInfo.exe --Inform=file://debug.txt %%A
		) >> Output_%fileCounter%.txt
exit

Re: Creating seperate output-files

Posted: 06 Aug 2021 15:18
by aGerman
Variables in a command line or in a parenthesized block of command lines (like the body of your loop) are expanded to their value only once before the line/block is even executed. To avoid this early variable expansion you need to specify that you want to delay it. Delayed expansion makes the exclamation point a special character which gets similar semantics as the percent sign. In order to avoid side effects with file names that may contain exclamation points you have to toggle disabled and anabled deleyed expansion in each iteration.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set /a "fileCounter=0"
for %%A in (*video*.mkv) do (
  set /a "fileCounter+=1"
  set "name=%%A"
  setlocal EnableDelayedExpansion
  echo "!name!" !fileCounter!
  endlocal
)
pause
Steffen

Re: Creating seperate output-files

Posted: 09 Aug 2021 00:57
by AlphaInc.
Thanks for your reply, I hope you can still help me since it's a little while ago, I used this code but it doesn't work:

@echo off &setlocal DisableDelayedExpansion

Code: Select all

rem This will grab information about the file
set /a fileCounter=0
	for %%A in (*.mkv) do (
		set /a "fileCounter+=1"
		set "name=%%A"
		setlocal EnableDelayedExpansion
		C:\System\Mediainfo\MediaInfo.exe --Inform=file://C:\System\Mediainfo\template.txt %%A >> Output_%fileCounter%.txt
		endlocal
		)
exit
It only creates output_0 for one of the three files and does not cloes the cmd-window. So I assume it's stuck somewhere

Edit: It seems, that the counter does not increase.

Edit2: Sorry, forgot ro replace %fileCounter% with !fileCounter! now it works thank you.

Re: Creating seperate output-files

Posted: 09 Aug 2021 02:00
by aGerman
I've written about exclamation points rather than percent signs. You should have a closer look at my example. It's been fo a reason that I used !name! and !fileCounter!.

Steffen

Re: Creating seperate output-files

Posted: 09 Aug 2021 02:05
by AlphaInc.
Yeah I've just noticed that :D
Sorry and thank you.