Double > in Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Double > in Batch File

#1 Post by john924xps » 08 Jun 2012 08:11

This may be a bit confusing.

I want to create a batch file that creates another batch file, which finally creates another file. Such as the example code below:
echo echo hi > output.txt > newbatch.bat

I colored the code for you. The point is that this program creates another program called newbatch.bat, which when opened creates a text file called output.txt with the file consisting of 'hi'. But when I run this code, it actually creates TWO files, I have tried everything there was to fix this, from quotation marks, all the way to more than one of >.

Advanced Thanks.

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

Re: Double > in Batch File

#2 Post by aGerman » 08 Jun 2012 08:30

Same question a few days ago:
viewtopic.php?f=3&t=3339&start=0

Regards
aGerman

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Double > in Batch File

#3 Post by john924xps » 08 Jun 2012 08:43

Very good! Are all the 'special characters' ignored when placed? (not including for the % of course). And what exactly does 'Escape special characters' mean?

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

Re: Double > in Batch File

#4 Post by foxidrive » 08 Jun 2012 08:56

A special character is > < & | and several others.

When they are 'escaped' with a ^ character then they can be echoed into a file.
Some characters are very difficult to deal with in batch scripts with " being a good example.

And from a command line there are issues with , = ; and others.

Alpha and numeric characters are in general ok but you will also find difficulties with foreign language characters and a command prompt.

And with delayed expansion ! becomes a problem character.

Welcome to batch files and weird and wonderful exceptions... :)

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Double > in Batch File

#5 Post by john924xps » 08 Jun 2012 08:59

Thanks. I was a newbie; for months I thought that DOS was namely called batch, so therefore I found no forum. You have taught me. And therefore I thank you all.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Double > in Batch File

#6 Post by john924xps » 08 Jun 2012 09:10

You said some characters are very difficult to deal with in batch scripts with " being a good example. Could you please expand this further? Why is that hard to deal with, and WHEN is it 'hard to deal with'?

In command lines there are issues with , = ; and what are the issues to this? What are the problems? That's it for now, thanks.

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

Re: Double > in Batch File

#7 Post by foxidrive » 08 Jun 2012 09:25

Filenames are double quoted all the time as in "This is an exe filename.exe" when including spaces etc.

But if you are parsing a text file and it has a line like this

"&ten books"and"thirteen spades&"

then it can be difficult to extract parts of the string, character by character. The presence of the & character means you need double quotes to handle the poison & character and using double quotes to process a double quote is a problem.

This isn't very clear but when you have a double quoted line and it has a double quote in the middle of it, and you need to manipulate the string, then it is an issue.




The other question has to do with calling a subroutine or a batch file from the command line.

If you try this batch file you will see that = , ; become invisible.

mybat "one & two" = "two & one" , three ; four

Code: Select all

:: mybat.bat
@echo off
echo %1 %2 %3 %4 %5 %6 %7 %8 %9

e:\>mybat "one & two" = "two & one" , three ; four
"one & two" "two & one" three four


But they will be preserved inside double quotes.

mybat "one = two" "two = one" "three ; four"
e:\>mybat "one = two" "two = one" "three ; four"
"one = two" "two = one" "three ; four"


The same thing happens when calling a label inside a batch file.

Post Reply