create an empty .txt file if the file doesnt exist

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

create an empty .txt file if the file doesnt exist

#1 Post by daillest319 » 28 Jun 2012 12:19

i'm trying to create an empty .txt file only if the file doesn't exist and then start the program once the file is created. this is creating the file for me but its not empty can anyone help?



Code: Select all

@echo off

IF EXIST "C:\test\page1.txt" (
     start /d "C:\test\" prog.EXE
     ) else (
         dir c:\ > C:\test\page1.txt
         start /d "C:\test\" prog.EXE
     GOTO :EOF
)


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

Re: create an empty .txt file if the file doesnt exist

#2 Post by aGerman » 28 Jun 2012 13:42

Try

Code: Select all

>"test 1.txt" type nul
>nul copy nul "test 2.txt"
>"test 3.txt" rem.


One more possible way:

Code: Select all

>nul fsutil file createnew "test 4.txt" 0

PRO The fsutil doesn't overwrite an existing file.
CON You have to run as administrator on Vista and newer.

Regards
aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: create an empty .txt file if the file doesnt exist

#3 Post by Fawers » 28 Jun 2012 15:07

>test.txt type nul will work just fine.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: create an empty .txt file if the file doesnt exist

#4 Post by Liviu » 28 Jun 2012 16:17

aGerman wrote:PRO The fsutil doesn't overwrite an existing file.

I believe your "type" and "rem" lines will also not overwrite if you append instead of write, yet would still create a 0-byte file if it doesn't exist already.

Code: Select all

>>"test 1.txt" type nul
>>"test 3.txt" rem.

Liviu

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: create an empty .txt file if the file doesnt exist

#5 Post by Fawers » 28 Jun 2012 21:48

Liviu wrote:
aGerman wrote:PRO The fsutil doesn't overwrite an existing file.

I believe your "type" and "rem" lines will also not overwrite if you append instead of write, yet would still create a 0-byte file if it doesn't exist already.

Code: Select all

>>"test 1.txt" type nul
>>"test 3.txt" rem.

Liviu

That is correct. But, in this case, it would make no difference in the end. Writing or appending will result the same.
daillest319 wrote:i'm trying to create an empty .txt file only if the file doesn't exist

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

Re: create an empty .txt file if the file doesnt exist

#6 Post by foxidrive » 29 Jun 2012 02:26

So this should work.

Code: Select all

@echo off
IF not EXIST "C:\test\page1.txt" type nul>"C:\test\page1.txt"
start /d "C:\test\" prog.EXE

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: create an empty .txt file if the file doesnt exist

#7 Post by Aacini » 29 Jun 2012 18:24

In the old MS-DOS days, REM command could be used to create an empty file this way:

Code: Select all

rem > empty.txt
but that "functionality" was removed in Windows.

You may use any command that may show a text, but that show nothing. I used CD .:

Code: Select all

cd . > empty.txt

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: create an empty .txt file if the file doesnt exist

#8 Post by Liviu » 29 Jun 2012 19:05

Aacini wrote:In the old MS-DOS days, REM command could be used to create an empty file this way [...] but that "functionality" was removed in Windows.

Per aGerman's first reply in this topic, "rem." works fine, verified here under xp sp3.

Liviu

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: create an empty .txt file if the file doesnt exist

#9 Post by Aacini » 29 Jun 2012 19:56

Funny thing! REM command followed by space and any comment does NOT create the empty file:

Code: Select all

rem > notworks.txt
rem any comment > notworks.txt
However, REM followed by certain special separator characters DO create the empty file!

Code: Select all

rem. > thisworks.txt
rem/any comment > thisworks.txt
rem:anything > thisworks.txt
But this does NOT work with ALL special characters!

Code: Select all

rem;xyz > notworks.txt
rem=xyz > notworks.txt
rem(xyz > notworks.txt
rem+xyz > notworks.txt

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: create an empty .txt file if the file doesnt exist

#10 Post by Liviu » 29 Jun 2012 20:10

Aacini wrote:Funny thing!

Indeed. Now that you mentioned it, there seem to be a few more characters generally considered not-special where REM will still create an empty output, for example...

Code: Select all

rem[ >thisworks.txt
rem] >thisworks.txt

Liviu

Post Reply