Echo an echo command to batch file using CLI

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SDee
Posts: 3
Joined: 15 Feb 2016 05:25

Echo an echo command to batch file using CLI

#1 Post by SDee » 15 Feb 2016 05:30

I am trying to write the following batch file directly from the command line,
however am facing a problem with the following line.

The output within my batch file should be a line as follows:
echo D | xcopy %USERPROFILE%\Desktop C:\%username% /E /Y /H

However I cannot write it on my TEST.bat file, what am trying is from the CLI:
echo echo D | xcopy %USERPROFILE%\Desktop C:\%username% /E /Y /H > TEST.bat



Any ideas?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Echo an echo command to batch file using CLI

#2 Post by foxidrive » 15 Feb 2016 05:35

I will merely comment that your code has other potential issues than the one that is clear.

Knowing what you are doing, and why the cmd prompt is being used, will very often change the answer

But on having a second look: this code does what your code was supposed to be doing:

Code: Select all

echo xcopy "%USERPROFILE%\Desktop\*.*" "C:\%username%\" /E /Y /H > TEST.bat

To keep the literal text as you have shown it:

Code: Select all

echo xcopy "%%USERPROFILE%%\Desktop\*.*" "C:\%%username%%\" /E /Y /H > TEST.bat

SDee
Posts: 3
Joined: 15 Feb 2016 05:25

Re: Echo an echo command to batch file using CLI

#3 Post by SDee » 15 Feb 2016 05:46

The idea is having an echo command to respond to the prompt to avoid user interaction.
This will still prompt for a confirmation, however if I use
echo D | xxxxxxxx

This will automatically respond with a D, so I basically need it to be present in the TEST.bat am creating.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Echo an echo command to batch file using CLI

#4 Post by Squashman » 15 Feb 2016 07:32

SDee wrote:The idea is having an echo command to respond to the prompt to avoid user interaction.

Did you test Foxidrive's code? I bet it does not prompt for user interaction.

SDee
Posts: 3
Joined: 15 Feb 2016 05:25

Re: Echo an echo command to batch file using CLI

#5 Post by SDee » 15 Feb 2016 16:54

Well it is not only about this code, am looking for something more generic, so that was just an example.
Regardless of the function, I want this string to be present in a newly created batch file named TEST.bat, but using CLI
(Assuming Command.exe will require a Y to run)

echo Y | Command.exe

This works with no problems at all if I manually typed it to the batch file, however using CLI strips off the first part

INPUT TO CLI: echo echo Y | Command.exe > Test.bat
OUTPUT IN BAT: Command.exe

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

Re: Echo an echo command to batch file using CLI

#6 Post by aGerman » 15 Feb 2016 18:19

The rules for a literal output of these characters are
% => %%
^ => ^^
< => ^<
> => ^>
| => ^|
& => ^&

And if delayed expansion is enabled
! => ^^!

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Echo an echo command to batch file using CLI

#7 Post by foxidrive » 15 Feb 2016 20:36

SDee wrote:Well it is not only about this code, am looking for something more generic, so that was just an example.


If you want to eat steak and chips for dinner, and you ask for Brussells sprouts and spinach, what do you think you will get? :D :D

That applies to programming also.

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Echo an echo command to batch file using CLI

#8 Post by pieh-ejdsch » 16 Feb 2016 07:48

Hi,

you're on CLI and make some tests
in your file you want anything like the successful commands in a batchfile to write in
take

Code: Select all

set /p Line=

use arrow key to find command and enter

Code: Select all

>test.cmd cmd /v /c echo !Line!
type test.cmd


your CLI-macro

Code: Select all

set "file=test.cmd"
set "Write=set /p Line=line 2 %FILE%: &>>%File% cmd /v /c echo !Line!& find /n /v "" %File%"
%Write%


Phil

Post Reply