views:

277

answers:

3

Hello,

I want to create a file named "new text document.txt" in the folder %tv% using a batch file ( *.bat). This is my batch file:

set tv=D:\prog\arpack96\ARPACK\SRC
cd "%tv%"
@CON >> "new text document.txt"
set tv=

Although I can really create the file in %tv%, but when I run the above batch file, I will get an error message saying

' ' is not recognized as an internal or external command, operable program or batch file.

Is there anyway to get rid of this error message? Or I am creating the file using the wrong command?

+1  A: 
type nul > file.txt

file.txt should be created empty

or paxdiablo's answer

jspcal
NO, it doesn' work.
Ngu Soon Hui
I don't want user interaction; all I want is to just silently create an empty file.
Ngu Soon Hui
type nul should do it
jspcal
A: 

con is the windows specialized file name and it should not be used.

copy con >> filename.txt

shall ask you to enter the text and you can save the text by typing Ctrl Z.

Gopalakrishnan Subramani
I don't want to be asked to enter the text; I want a file to be created silently at the specified location
Ngu Soon Hui
+4  A: 

In order to get a truly empty file without having to provide user interaction, you can use the set /p command with a little trickery:

set tv=c:\documents and settings\administrator
cd "%tv%"
<nul >"new text document.txt" (set /p tv=)

The set /p asks the user for input into the tv variable after outputting the prompt after the = character.

Since the prompt is empty, no prompt is shown. Since you're reading from nul, it doesn't wait for user interaction. And you can store the empty string straight into tv thus removing the need to unset it.


Actually, after some more thought, there's an easier way. I've used that set /p trick in the past since it's the only way I know of echoing text without a newline being added (great for progress bars in the console). But if all you want is an empty file, you can probably get away with:

copy /y nul "new text document.txt"

The copy simply copies the contents of the nul device (effectively an empty file) to your new file name. The /y is to ensure it's overwritten without bothering the user.

paxdiablo
This is the correct answer.
Ngu Soon Hui
Well, it's *a* correct answer, it may not be the only one :-)
paxdiablo
I usually used either `type nul >` or `copy nul`. But I learned about that `set /p` trick much later here anyway :).
Joey