3

I am working on a project where I need to create multiple empty files. In the past, I have used the touch command, but this only allows me to create one file at a time. Is there a way to create multiple empty files using the command prompt in Windows?

I appreciate any suggestions or solutions you may have. Thank you!

Roee Angel
  • 143
  • 2
  • 15
  • 4
    Does this answer your question? [How to create an empty file at the command line in Windows?](https://stackoverflow.com/questions/1702762/how-to-create-an-empty-file-at-the-command-line-in-windows) – dan1st Mar 13 '20 at 21:25
  • @dan1st - The answer indicated as duplicate only addresses how to create one (1) empty file. The OP wants to create many. – lit Mar 14 '20 at 07:28

3 Answers3

5

A FOR loop will allow you to make as many files as you want. The following command creates ten (10) files.

FOR /L %A IN (1,1,10) DO (TYPE>"%TEMP%\%~A.txt" NUL)

If this is used in a .bat file script, be sure to double the % character on the variable.

FOR /L %%A IN (1,1,10) DO (TYPE>"%TEMP%\%%~A.txt" NUL)
lit
  • 14,456
  • 10
  • 65
  • 119
  • I get error when running this script on Windows 10 in BAT file: "%%A was unexpected at this time." – Boris Zinchenko Nov 07 '21 at 11:53
  • 1
    @BorisZinchenko, sorry, there needs to be a SPACE character between `IN` and `(`. Also, the first line is for use at the interacting cmd.exe prompt. The second line is for use in a .bat file script. – lit Nov 07 '21 at 13:47
  • Thank you so much! Everybody trying to copy this line from here, be aware that you must explicitly put spaces instead of other characters copied by browser. I was totally confused. It was very hard to distinguish. Now it works perfectly. – Boris Zinchenko Nov 07 '21 at 16:18
  • @רועי אנגל, if you have a answer, please select what you consider to be the best answer and select the check mark for that answer. That is how SO works. – lit Nov 07 '21 at 18:06
3

You can use the FOR command like this:

FOR %N IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (echo null > C:\temp\%N.txt)

This will create 26 empty .txt files with one line.

If you want to clean up the files created, use this:

FOR %N IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (del C:\temp\%N.txt)

sippybear
  • 256
  • 2
  • 6
2

Great solutions for a huge number of files although
they just didn't work for me. Here is what
worked for me using windows CMD or VScode Terminal/

Create multiple files:

ni file_name.php, other.css, thirdexample.html

Create multiple directories:

mkdir folder-a, folderama, library
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sagive
  • 1,699
  • 1
  • 24
  • 34