37

How can I do the following in a Windows batch file?

  1. Write to a file called subdir/localsettings.py
  2. Overwrite all existing content...
  3. ...with multiple lines of text...
  4. ...including a string that is "[current working directory]/subdir" (which I think might be %cd%/subdir?)

Please note, I want to do this as part of a batch script so I can't use con + Enter (at least, maybe I can, but I don't know how to simulate Enter as part of a batch script).

Thanks!

3 Answers 3

68

Use output redirection > and >>

echo one>%file%
echo two>>%file%
echo three>>%file%

Or in a more readable way: (In cmd.exe, using "echo one >%file%" would include the whitespace before >.)

>%file%  echo one
>>%file% echo two
>>%file% echo three

You could also use:

(
    echo one
    echo two
    echo three
) >%file%
Sign up to request clarification or add additional context in comments.

7 Comments

For blank lines, echo. or echo= or echo: or... (There are quite a few separators that cmd.exe recognizes; not only space. So far I've discovered .;,/=+` with echo`)
The selected answer omits stderr - while it may not be necessary in this question, when redirecting output you should consider that if the command line app outputs errors to stderr,simply using > or >> to redirect output will not catch the errors. You would need to use 2>&1 or 2>>&1 to redirect to the same file, or specify a different file. For example: net /? >StdOutLog.txt 2>StdErrLog.txt (the net command is a little odd in that it displays output to standard error - net subcommand displays to standard output, so net use >stdOutLog.txt 2>StdErrLog.txt will find data in stdOutLog.txt)
@grawity echo. and the most others can collide with an existing file "echo" and they always forces a file system access, echo( seems to be "safe"
@grawity: You are right, not all variants check for a file, but they are not "safe" against other "problems" like /? or on. Discussed here echo. fails
@jeb: Re-tested my "safe" list; = fails with echo=/?, but the rest work. (Regarding your forum post: With / you can use echo//?, not echo/?. For a single question mark, simple echo ?. But by now any sane person would have ported the script into another language (the asker already has Python installed). It's what I would do, anyway.)
|
9
echo Line 1^

Line 2^

Line 3 >textfile.txt

Note the double new-lines to force the output:

Line1
Line2
Line3

Also:

(echo Line 1^

Line 2^

Line 3)>textfile.txt

Comments

-1

If you wann do it in 1 line, here a more complex example

  • I simulate a carriage return with \n
  • I escape special character for parenthesis
  • I replace the \n with what is needed in batch
set text=Cols:\n- Port de Lers (1517m) avec le col d'Agnès (1570m)\n- Col de la Core (1395m)\n- Col de Menté (1349m)\n- Col de Peyresourde (1569m)\n- Col du Tourmalet (2115m)\n- Col d'Aubisque (1709m)\n- Col de Marie-Blanque (1035m)\n- Col de Labays (1354m)
set "text=%text:(=^(%" :: I escape special character for parenthesis
set "text=%text:)=^)%" :: I escape special character for parenthesis
set "text=%text:\n= >> temp.txt & echo %" :: I replace the `\n` with what is needed in batch 
set "text=%text:"=%"
if exist "temp.txt" rm temp.txt :: just remove the file if exist to avoid to append in it
echo %text% >> temp.txt
cat temp.txt :: print result
C:\ > cat temp.txt
Cols:
- Port de Lers (1517m) avec le col d'Agnès (1570m)
- Col de la Core (1395m)
- Col de Menté (1349m)
- Col de Peyresourde (1569m)
- Col du Tourmalet (2115m)
- Col d'Aubisque (1709m)
- Col de Marie-Blanque (1035m)
- Col de Labays (1354m)

If you wanna delete the last \r\n use truncate -s -2 temp.txt Install git on windows to be able to use truncate

Comments