Page 1 of 1
create an empty .txt file if the file doesnt exist
Posted: 28 Jun 2012 12:19
by daillest319
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
)
Re: create an empty .txt file if the file doesnt exist
Posted: 28 Jun 2012 13:42
by aGerman
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
Re: create an empty .txt file if the file doesnt exist
Posted: 28 Jun 2012 15:07
by Fawers
>test.txt type nul will work just fine.
Re: create an empty .txt file if the file doesnt exist
Posted: 28 Jun 2012 16:17
by Liviu
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
Re: create an empty .txt file if the file doesnt exist
Posted: 28 Jun 2012 21:48
by Fawers
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
Re: create an empty .txt file if the file doesnt exist
Posted: 29 Jun 2012 02:26
by foxidrive
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
Re: create an empty .txt file if the file doesnt exist
Posted: 29 Jun 2012 18:24
by Aacini
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.
You may use any command that may show a text, but that show nothing. I used CD .:
Re: create an empty .txt file if the file doesnt exist
Posted: 29 Jun 2012 19:05
by Liviu
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
Re: create an empty .txt file if the file doesnt exist
Posted: 29 Jun 2012 19:56
by Aacini
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
Re: create an empty .txt file if the file doesnt exist
Posted: 29 Jun 2012 20:10
by Liviu
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