Creating seperate output-files [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AlphaInc.
Posts: 21
Joined: 15 Apr 2021 08:15

Creating seperate output-files [SOLVED]

#1 Post by AlphaInc. » 06 Aug 2021 14:37

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
Last edited by AlphaInc. on 09 Aug 2021 02:05, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating seperate output-files

#2 Post by aGerman » 06 Aug 2021 15:18

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

AlphaInc.
Posts: 21
Joined: 15 Apr 2021 08:15

Re: Creating seperate output-files

#3 Post by AlphaInc. » 09 Aug 2021 00:57

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.
Last edited by AlphaInc. on 09 Aug 2021 02:05, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating seperate output-files

#4 Post by aGerman » 09 Aug 2021 02:00

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

AlphaInc.
Posts: 21
Joined: 15 Apr 2021 08:15

Re: Creating seperate output-files

#5 Post by AlphaInc. » 09 Aug 2021 02:05

Yeah I've just noticed that :D
Sorry and thank you.

Post Reply