2

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

You may want copy /Y nul myEmptyFile.txt In case the file already exists and you want to overwrite it.
0

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)

4 Comments

Its not working with the above code. I am new to writing script files. I tried, below is the output, and program is not exiting.If not exist "C:\Users\pooja\Desktop\ForTesting\tmp.txt" (MKDIR "\Users\pooja\Desktop\ForTesting\" 2>NUL && FSutil File CreateNew "\Users\pooja\Desktop\ForTesting\\tmp.txt" 0 1>NUL ) ) & (GOTO:EOF) and if i saw the print, it's not showing the drive as C.
and more over can one plz suggest nice site to learn batch scripting
The function works, don't try to do it manually. in the code that you have pasted you don't starts with a "(", use it to agrupate or will not run, also you are using two "\\" in the FSUTIL path so can't create file,this is the corrected code from your pasted code: (If not exist "%HomeDrive%\Users\pooja\Desktop\ForTesting\tmp.txt" (MKDIR "%HomeDrive%\Users\pooja\Desktop\ForTesting" 2>NUL && FSutil File CreateNew "%HomeDrive%\Users\pooja\Desktop\ForTesting\tmp.txt" 0 1>NUL )) & (GOTO:EOF) if my answer resolved your problem then please use the accept button to accept the answer
@echo on Call :CheckTextFile "C:\Users\pooja\Desktop\ForTesting\tmp.txt" Pause&Exit :CheckTextFile (If not exist "%~1" ( MKDIR "%~dp1" 2>NUL && FSutil File CreateNew "%~dp1%~nx1" 0 1>NUL)) & (GOTO:EOF) ) exit . I modified %~p1 to %~dp1, then it giving drive path also. But still its not creating the file. I tried only till MKDIR %~dp1 then its creating the folder. But with the code above its not creating. May be u can correct me, where i am going wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.