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!

2
  • 4
    Does this answer your question? How to create an empty file at the command line in Windows? Commented Mar 13, 2020 at 21:25
  • @dan1st - The answer indicated as duplicate only addresses how to create one (1) empty file. The OP wants to create many. Commented Mar 14, 2020 at 7:28

3 Answers 3

6

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

4 Comments

I get error when running this script on Windows 10 in BAT file: "%%A was unexpected at this time."
@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.
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.
@רועי אנגל, 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.
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)

4 Comments

Are you sure these files are empty? Zero length?
does not work, the for %n
@serge %N and %n both are working fine for me.
@lit to get the zero length file, we need to use either echo. 2> (please consider no space between 2 and >) OR type NUL >
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

Comments

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.