echo multiple lines + redirection

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

echo multiple lines + redirection

#1 Post by colargol » 03 Oct 2011 08:19

Hi all

I have a batch script that generates another batch (%outp%)

First I was doing like this:

Code: Select all

   echo :aac>>"%outp%"
   echo "%wavi_path%" "%%avs_file%%" - ^|"%neroAAC_path%" -ignorelength -q %%aac_quality%% -if - -of "%%audio_file%%">>"%outp%"
   echo if %%errorlevel%%==0 (set audio_status=ok) else (set audio_status=erreur avec le aac)>>"%outp%"
   echo goto video>>"%outp%"
   echo.>>"%outp%"


Then I realized it can save space doing like this
I just had to escape ( and )

Code: Select all

(
   echo :aac
   echo "%wavi_path%" "%%avs_file%%" - ^|"%neroAAC_path%" -ignorelength -q %%aac_quality%% -if - -of "%%audio_file%%"
   echo if %%errorlevel%%==0 ^(set audio_status=ok^) else ^(set audio_status=erreur avec le aac^)
   echo goto video
   echo.
)>>"%outp%"


But some of the variables can contain ( or ) and I didn't pay attention before. :?
So I have to make a chars substitution with every variables and I don't like this solution.

Is there a way do echo multiple lines easily with file redirection?

Thanks

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

Re: echo multiple lines + redirection

#2 Post by aGerman » 03 Oct 2011 10:19

Could you give an example variable content, which doesn't work?!
I tried that without any problems:

Code: Select all

@echo off &setlocal
set "outp=file(1).bat
set "wavi_path=c:\folder(1)\whatever.ext"
set "neroAAC_path=c:\folder(2)\whatever.ext"

>"%outp%" (
   echo :aac
   echo "%wavi_path%" "%%avs_file%%" - ^|"%neroAAC_path%" -ignorelength -q %%aac_quality%% -if - -of "%%audio_file%%"
   echo if %%errorlevel%%==0 ^(set audio_status=ok^) else ^(set audio_status=erreur avec le aac^)
   echo goto video
   echo.
)

Regards
aGerman

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: echo multiple lines + redirection

#3 Post by colargol » 03 Oct 2011 11:14

Thanks for reply! :)

In fact, it works because %wavi_path% and %neroAAC_path% are surrounded by quotes, but if I remove quote, it fails
It was not a good example :oops:

This fails

Code: Select all

set "outp=file(1).bat"
set "wavi_path=c:\folder(1)\whatever.ext"

>"%outp%" (
   echo rem are using: %wavi_path%
)

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

Re: echo multiple lines + redirection

#4 Post by aGerman » 03 Oct 2011 11:24

I see.

Code: Select all

@echo off &setlocal
set "outp=file(1).bat"
set "wavi_path=c:\folder(1)\whatever.ext"

setlocal enabledelayedexpansion
>"%outp%" (
   echo rem are using: !wavi_path!
)
endlocal

Note that I used enabledelayedexpansion AFTER setting the variable values. Otherwise exclamation marks (if contained) are removed in these values.

Regards
aGerman

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: echo multiple lines + redirection

#5 Post by colargol » 03 Oct 2011 14:02

ok it works, thank you very much! :)
Si I just have to replace %% with !! on all variables that may contain brackets.
very good, I didn't thought to this :)

colargol

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: echo multiple lines + redirection

#6 Post by colargol » 05 Oct 2011 07:19

I still have problems with brackets:

Code: Select all

>>"%outp%" (
   echo for /f "tokens=1-2 delims=: " %%%%a in ^("%%time_file%%"^) do ^(
   echo    set mkv_file=!avs_base! [%%date_file:~0,2%%-%%date_file:~3,2%%-%%date_file:~6,4%% - %%%%ah%%%%b].mkv
   echo ^)
)

the generate batch will fail if !avs_base! has a ) char :?


Isn't there a trick to write a bloc command safely?

Code: Select all

(
...
)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: echo multiple lines + redirection

#7 Post by Ed Dyreen » 08 Oct 2011 20:28

'
Try:

Code: Select all

>>"%outp%" (
   echo.for /f "tokens=1-2 delims=: " %%%%a in ^("%%time_file%%"^) do ^(
   echo.   set mkv_file=!avs_base! [%%date_file:~0,2%%-%%date_file:~3,2%%-%%date_file:~6,4%% - %%%%ah%%%%b].mkv
   echo.^)
)
or:

Code: Select all

set "@necho=echo("
setlocal enabledelayedexpansion
>>"%outp%" (
   %@necho%for /f "tokens=1-2 delims=: " %%%%a in ^("%%time_file%%"^) do ^(
   %@necho%   set mkv_file=!avs_base! [%%date_file:~0,2%%-%%date_file:~3,2%%-%%date_file:~6,4%% - %%%%ah%%%%b].mkv
   %@necho%^)
)

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: echo multiple lines + redirection

#8 Post by colargol » 14 Oct 2011 01:15

Thanks Ed Dyreen

But I think it can't work as long as !avs_base! will have a end parenthesis :shock:
It does'nt matter, I finally had to do this in 2 steps and it works.

But now, I have a similar problem with this code :o
Of course it doesn't work

Code: Select all

if !nb_ch_use! LSS !nb_ch! (
   >"!dmix_file!" (
      echo import("downmix.avsi")
      echo import("!avs_file!")
      echo convert_audio(!nb_ch_use!)
   )
)



edit
I used goto:

Code: Select all

if !nb_ch_use! GEQ !nb_ch! goto audiocfg2
>"!dmix_file!" (
   echo import^("downmix.avsi"^)
   echo import^("!avs_file!"^)
   echo convert_audio^(!nb_ch_use!^)
)   

:audiocfg2

Post Reply