user2053989
user2053989

Reputation: 55

How to create an empty text file with specified path using Windows batch script

It has to create full folder path, along with the empty file. My requirement is i will get textfile path as argument and if that file doesn't exists, need to create the file.
I thought to get the path first and check if that folder exists, if not create the folder path, Then create empty file using TYPE nul.

Can i get some thing like in java, String subStr = str.subString(0, str.lastIndexOf('.')) , how to do that using windows batch script .Any suggestions plz, Thanks in advance.

Upvotes: 2

Views: 5325

Answers (2)

ElektroStudios
ElektroStudios

Reputation: 20494

You can make a function like this:

@Echo OFF

Call :CheckTextFile "C:\Test1\Test.txt"
Call :CheckTextFile "C:\Test2\Test.txt"
Call :CheckTextFile "C:\Test3\Test.txt"
Pause&Exit

:CheckTextFile
(If not exist "%~1" (MKDIR "%~p1" 2>NUL && FSutil File CreateNew "%~p1\%~nx1" 0 1>NUL)) & (GOTO:EOF)

Upvotes: 0

Mario
Mario

Reputation: 36567

To split path/folder and file names you can use the answer provided to this question.

To create an empty file just copy "nothing" into it:

copy nul myEmptyFile.txt

As an alternative, you could probably redirect empty output, but I guess this is definitely more resource heavy than the previous idea:

cmd /c > myEmptyFile.txt

Upvotes: 1

Related Questions