none
Is there a way to echo %%a to a file? RRS feed

  • Question

  • Hi,

    I'm trying to write a bat file that creates another bat file by echoing commands to a file.

    code snippet:

    set str=for /F %%a IN (' "tasklist | find /C "vpnagent.exe" " ') DO (set /a COUNT2=%%a)

    echo %str% >>generated_bat_file.bat

    What I want to end up with is

    for /F %%a IN (' "tasklist | find /C "vpnagent.exe" " ') DO (set /a COUNT2=%%a)

    what I end up with is

    for /F %a IN (' "tasklist | find /C "vpnagent.exe" " ') DO (set /a COUNT2=%a)

    Is there a way to echo double %'s?

    Monday, February 28, 2011 9:18 PM

Answers

  • You need to double the percent signs.

    set str=for /F %%%%a IN (' "tasklist | find /C "vpnagent.exe" " ') DO (set /a COUNT2=%%%%a)

    echo %str% >>generated_bat_file.bat

    However, I'm curious why you want to do it.  I haven't found any reason to use a secondary batch procedure in an awfully long time (since about the time WinNT was introduced).



    Tom Lavedas
    Monday, February 28, 2011 9:56 PM
    Moderator

All replies

  • for /F %%a IN ('tasklist ^| find /C "vpnagent.exe"') do echo %%a 
    Monday, February 28, 2011 9:26 PM
  • You need to double the percent signs.

    set str=for /F %%%%a IN (' "tasklist | find /C "vpnagent.exe" " ') DO (set /a COUNT2=%%%%a)

    echo %str% >>generated_bat_file.bat

    However, I'm curious why you want to do it.  I haven't found any reason to use a secondary batch procedure in an awfully long time (since about the time WinNT was introduced).



    Tom Lavedas
    Monday, February 28, 2011 9:56 PM
    Moderator
  • Doesn't seem to work for me.

    What I have typed in

    set str=for /F %%a IN ('tasklist ^| find /C "vpnagent,exe" ') do echo %%a

    echo %str% > b.bat

    I'm receiving a message   FIND: Parameter format not correct

    Monday, February 28, 2011 9:59 PM
  • Tom,

    The quad % did the trick.  Thanks.

    Why I would want to do something like this, well, it's a long story.

    I have to repackage some apps and come up with a way to push out updates/hotfixes to our users.

    I can only assume the base O/S is present, (mostly XP).

    The requirements include:

    1) Hotfix can't be started if the app to be updated is currently running.

    2) System to be patched must be on the company domain

    3) System cannot be connected to the company domain via VPN

    4) Hotfix is scheduled and self checked every 10 minutes.  If conditions 1 through 3 are not meet, bat file reschedules self in 10 minutes

    5) The scheduled bat file also creates a bat file that will run at startup/logoff-on, (subject to conditions 1 through 3 above).

    6) If either the bat file in startup or the scheduled bat file runs, it erases the other bat file, (if startup bat file runs to completion, it erases thescheduled bat file.  If scheduled bat file runs to completion, it erase the startup bat file).

    Complicated enough?

     

    Again,  thank you for the help.

     

    Monday, February 28, 2011 10:12 PM
  • echo for /F %%a IN ('tasklist ^^^| find /C "vpnagent.exe"') DO (set /a COUNT2=%%a) >>generated_bat_file.bat
    Monday, February 28, 2011 10:22 PM