2

I want to create new file and to add info in it. But at the beginning I want to clear all its information. So I wrote:

@if "%DEBUG%" == "" @echo off
@rem ############################################
@rem # Create new empty file                    #
@rem ############################################

:setup
if "%OS%"=="Windows_NT" setlocal
SetLocal EnableDelayedExpansion
goto create

:create    
@cmd /c echo( >%temp%\hosts
goto end

:end
if "%OS%"=="Windows_NT" endlocal
PAUSE

But when I open the file there is leading empty line

<empty line>
SomeInfo

Where is just empty line. How to create new file with no leading empty line?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Xelian
  • 16,680
  • 25
  • 99
  • 152
  • 2
    Possible duplicate of http://stackoverflow.com/questions/210201/how-to-create-empty-text-file-from-a-batch-file – Rafael Nov 04 '14 at 13:29

3 Answers3

4
break>empty.file

My favorite way..

Break is an internal command so it is fast.And almost allays produces empty output (he only exception is /?) no matter of the arguments , which reduces the chance of errors. And never does changes to the environment.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
3
>empty.file @echo off

Just wanted to throw this out there, but I would recommend the break or set /p methods first. This method along with creating a blank file will also turn command-echoing off

Also, here is an alternate method of the set /p method using nul input.

>empty.file set /p "=" <nul
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
1
ECHO.|SET /P A="">empty.file

SET /P doesn't emit newlines.

mojo
  • 4,050
  • 17
  • 24