create multiple files and Directories at once in Windows Command line

The windows commandline also known as cmd provides the cli interaction to the windows operating system.

The Windows powershell which has been recently made opensource provides cross platform compatibility and can also be installed on Linux OS.

The command to create a file in Powershell:

PS /home/vamshi> New-Item -ItemType file testfile.txt

There is another simpler way to create a file in windows and also used often in Linux/Unix

PS /home/vamshi> echo " " > testfile2.txt
PS /home/cloud_user> dir

Directory: /home/vamshi

Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 04/25/2020 07:40 2 testfile.txt
----- 04/25/2020 07:40 2 testfile2.txt

Command to create a Directory in Powershell.

PS /home/vamshi>New-Item -ItemType directory testDir.

How to create multiple files in Windows using Command-line?

Write down the list of all filenames in a file filenames.txt as below:

PS /home/vamshi> cat filenames.txt
file1
file2
file3
file4

To achieve this we have to create a for loop.

PS /home/vamshi> cat filenames.txt | foreach-object -process { echo "" > $_ }
PS /home/cloud_user> dir

Directory: /home/vamshi

Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 04/25/2020 07:49 1 file1
----- 04/25/2020 07:49 1 file2
----- 04/25/2020 07:49 1 file3
----- 04/25/2020 07:49 1 file4

The foreach command can be used along with New-Item -ItemType as below:

PS /home/vamshi> cat filenames.txt | foreach-object -process { New-Item -ItemType file $_ }

Create multiple Directories in windows:

PS /home/vamshi> cat ./dirs.txt
dir1
dir2
dir3
PS /home/vamshi> cat ./newdirs.txt | foreach-object -process { New-Item -ItemType directory $_ }
PS /home/vamshi> dir
Directory: /home/vamshi
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 04/25/2020 07:54 dir1
d---- 04/25/2020 07:54 dir2
d---- 04/25/2020 07:54 dir3

Leave a Comment