Deepak Play
Deepak Play

Reputation: 273

How to make a batch file containing >nul on echo command

I want to make the batch file with this code


@echo off
echo ping -n5 localhost>nul >>hiddenping.bat
exit

the output in the hiddenping.bat is

ping -n5 localhost

nul is not saved in the file

how to make the hiddenping.bat file containing the code of

ping -n5 localhost>nul

Upvotes: 3

Views: 1712

Answers (3)

dbenham
dbenham

Reputation: 130879

I would escape the poison character(s) as foxidrive has suggested..

Another option is to put the desired string in a variable, and then ECHO the value using delayed expansion.

@echo off
setlocal enableDelayedExpansion
set "str=ping -n5 localhost>nul"
echo !str!>>hiddenping.bat

A similar effect can be achieved via FOR variables.

@echo off
for %%S in ("ping -n5 localhost>nul") do echo %%S>>hiddenping.bat

Upvotes: 0

user207421
user207421

Reputation: 310985

You need to quote the part you don't want executed now.

echo "ping -n5 localhost>nul" >>hiddenping.bat

Upvotes: -1

foxidrive
foxidrive

Reputation: 41244

The caret ^ allows you to escape certain characters.

@echo off
echo ping -n5 localhost^>nul >>hiddenping.bat
exit

Upvotes: 3

Related Questions