24

I am trying to create a batch file which will create a text file in a specific folder. I am able to create a text file on my desktop, but I need to create a file in a specific file path.

For example in D:/Testing folder I wants to create a user defined text file.

@echo off

echo .>> dblank.txt

I am using the above code to create a .txt file on my desktop.

I know this is a simple question but I searched google and have not found any good solution that could helpful to me.

iliketocode
  • 6,652
  • 4
  • 41
  • 57
user1926138
  • 1,238
  • 7
  • 25
  • 50

4 Answers4

37

You have it almost done. Just explicitly say where to create the file

@echo off
  echo.>"d:\testing\dblank.txt"

This creates a file containing a blank line (CR + LF = 2 bytes).

If you want the file empty (0 bytes)

@echo off
  break>"d:\testing\dblank.txt"
iliketocode
  • 6,652
  • 4
  • 41
  • 57
MC ND
  • 65,671
  • 6
  • 67
  • 106
  • Oho its working fine. last time i have not created that testing folder in my D drive. that why it was not working. Thanks a ton – user1926138 Jan 27 '14 at 14:08
  • how would you avoid hardcoding the path? – m4l490n Feb 05 '19 at 15:48
  • @m4l490n, not hardcoding it (sorry if it seems a joke, it is not). Question is about a specific folder, where do you want the file to be created? – MC ND Feb 05 '19 at 16:23
12

This code written above worked for me as well. Although, you can use the code I am writing here:

@echo off

@echo>"d:\testing\dblank.txt

If you want to write some text to dblank.txt then add the following line in the end of your code

@echo Writing text to dblank.txt> dblank.txt
iliketocode
  • 6,652
  • 4
  • 41
  • 57
TRIDIB BOSE
  • 321
  • 3
  • 3
  • If you need to write to the same file that you have been created you should use @echo Writing text to dblank.txt> d:\testing\dblank.txt – Ardalan Shahgholi Jun 08 '17 at 18:44
3

Changed the set to remove % as that will write to text file as Echo on or off

echo off
title Custom Text File
cls
set /p txt=What do you want it to say? ; 
echo %txt% > "D:\Testing\dblank.txt"
exit
Community
  • 1
  • 1
phillipbv
  • 31
  • 6
0

You can also use

cd %localhost%

to set the directory to the folder the batch file was opened from. Your script would look like this:

@echo off
cd %localhost%
echo .> dblank.txt

Make sure you set the directory before you use the command to create the text file.