Page 1 of 1

echo multiple lines + redirection

Posted: 03 Oct 2011 08:19
by colargol
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

Re: echo multiple lines + redirection

Posted: 03 Oct 2011 10:19
by aGerman
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

Re: echo multiple lines + redirection

Posted: 03 Oct 2011 11:14
by colargol
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%
)

Re: echo multiple lines + redirection

Posted: 03 Oct 2011 11:24
by aGerman
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

Re: echo multiple lines + redirection

Posted: 03 Oct 2011 14:02
by colargol
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

Re: echo multiple lines + redirection

Posted: 05 Oct 2011 07:19
by colargol
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

(
...
)

Re: echo multiple lines + redirection

Posted: 08 Oct 2011 20:28
by Ed Dyreen
'
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%^)
)

Re: echo multiple lines + redirection

Posted: 14 Oct 2011 01:15
by colargol
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