Acca Emme
Acca Emme

Reputation: 376

Batch: how create .bat with a batch escaping special chars

Why it not works? I want to create a .bat by a batch file, but i can't create a .bat file with variables insides.

Thanks!

rem echo off

echo @echo off > c:\temp\ping.bat

echo rem Test della perdita di pacchetti verso un indirizzo >> c:\temp\ping.bat

echo set data^=20^%date:~6,4^%_^%date:~3,2^%_^%date:~0,2^% >> c:\temp\ping.bat

echo set ora=^%time:~0,2^% >> c:\temp\ping.bat

echo set name^=ping-^%data^% >> c:\temp\ping.bat

echo @echo on >> c:\temp\ping.bat

set comando=ping -t %addr%

echo %comando% >> c:\temp\ping.bat

Thanks!!! I did it because I need to create a "ping.bat" to put in autorun on Windows starts, if required by program caller. I forgot to save output on txt and show it on standard output, how prova.bat can redirect output into ping.txt? Thanks

@echo off
(
    echo @echo off
    echo rem Test della perdita di pacchetti verso %addr% 
    echo set data=20%%date:~6,4%%_%%date:~3,2%%_%%date:~0,2%%
    echo set ora=%%time:~0,2%%
    echo set name=ping.txt

echo %%data%% - %%Time:~0,5%% ^>> c:\temp\%name%

    echo @echo on 

echo ping -t %addr% ^>> c:\temp\%name%

    echo @exit /b
) > c:\temp\ping.bat

Upvotes: 1

Views: 119

Answers (1)

jeb
jeb

Reputation: 82287

This should create your file, but your code will not work!

@echo off
(
    echo @echo off
    echo rem Test della perdita di pacchetti verso un indirizzo 
    echo set data=20%%date:~6,4%%_%%date:~3,2%%_%%date:~0,2%%
    echo set ora=%%time:~0,2%%
    echo set filename=ping-%%data%%.txt
    echo ping -t %addr% ^>^> c:\temp\%%filename%%
) > c:\temp\ping.bat

You named your batch file ping.bat and then you try to call ping -t %addr%.
This will end up in an endless loop.

Btw. Why you don't use a simple batch function instead?

@echo off
set addr=10.0.0.1
call :ping %addr%
exit /b

:ping
set "data=20%date:~6,4%_%date:~3,2%_%date:~0,2%"
set "ora=%time:~0,2%"
set "name=ping-%data%"
ping -t %1
exit /b

Upvotes: 2

Related Questions