Pipe variables into a new batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
3dL
Posts: 2
Joined: 04 Dec 2015 03:08

Pipe variables into a new batch

#1 Post by 3dL » 04 Dec 2015 03:24

Hi guys,

i hope you can help me with that:


I try to pipe some lines into a new batch file.

There are some characters which i can't pipe directly:

For example ">" - I found out already, I have write "^>" to pipe it into a new batch.

But I got some variables aswell, which i want write into the batchfile.


----
Example:

echo here is some text; %CLASSPATH% >>newBatch.bat

so when I open the created batchfile, there is only:

"here is some text; "
----


I have found some different stuff so far, but i does not work or I am doing it the wrong way.

Can you help me? :)

Thanks so far!
3dL

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Pipe variables into a new batch

#2 Post by Ed Dyreen » 04 Dec 2015 06:30

3dL wrote:But I got some variables aswell, which i want write into the batchfile.

echo here is some text; %CLASSPATH% >>newBatch.bat

so when I open the created batchfile, there is only:

"here is some text; "
Apparently the variable CLASSPATH is empty or it contains illegal characters.

if you want to echo a variable that contain illegal characters you can protect it using delayedExpansion, example:

Code: Select all

setlocal enableDelayedExpansion

echo here is some text; !CLASSPATH!>>newBatch.bat
But if you want to have dos interpret it as text then you'll have to escape it just like you did with the > character, however % is special in the way that you can't escape it using ^. The correct way to escape it is to simply double the % char. Example:

Code: Select all

echo here is some text; %%CLASSPATH%%>>newBatch.bat

3dL
Posts: 2
Joined: 04 Dec 2015 03:08

Re: Pipe variables into a new batch

#3 Post by 3dL » 07 Dec 2015 01:46

Thanks! :)

Thats what I found out later aswell :)

it works fine with double %% - so %%CLASSPATH%%

Post Reply