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.

user2053989
  • 55
  • 3
  • 6

2 Answers2

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
Community
  • 1
  • 1
Mario
  • 35,726
  • 5
  • 62
  • 78
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)
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 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. – user2053989 Apr 24 '13 at 14:21
  • and more over can one plz suggest nice site to learn batch scripting – user2053989 Apr 24 '13 at 14:22
  • 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 – ElektroStudios Apr 24 '13 at 14:37
  • @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. – user2053989 Apr 24 '13 at 15:41