2

The command for creating a new file on windows using cmd is:

cd.> filename.txt

But how can I create more than one file using a single command?

Rohit Raj
  • 21
  • 1
  • 4
  • BTW: The command `cd.> filename.txt` is of invalid syntax. There is a space character missing between the command `cd` (argument 0) and the directory name `.` (current directory). So correct would be `cd .> filename.txt` or `cd .>filename.txt`. `cd.` results in accessing the file system searching for a file with name `cd`. The dot at end is removed by the Windows file IO functions as described by the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file). – Mofi Feb 03 '22 at 18:49
  • If there is in current directory really a folder with name `cd` or a file with name `cd` which is not an executable, the usage of `cd.> filename.txt` instead of `cd .>filename.txt` results on execution in the error message "*'cd.' is not recognized as an internal or external command, operable program or batch file.*" Please use always a space between the name of an internal command and its (first) argument to make sure that `cmd.exe` recognizes always from the beginning the internal command and does not access the file system to find an executable or script file with name consisting of ... – Mofi Feb 03 '22 at 18:52
  • ... internal command name and its first argument not separated with a space as required by syntax of `cmd.exe`. The usage of `cd.` works just due to automatic error correction on `cmd.exe` not finding in file system an entry with name `cd` and so starts analyzing what could be the reason and detects now that the user means its internal command `cd` with the argument `.`. – Mofi Feb 03 '22 at 18:54
  • 1
    What about `for /L %I in (1,1,4) do rem/> "file%I.txt"`? – aschipfl Feb 03 '22 at 20:12

2 Answers2

2

With PowerShell you can use New-Item with an array of filenames.

Note: ni is a built-in alias for New-Item, which is useful to substitute interactively.

# New-Item
ni fileA.txt, fileB.txt, filec.txt, "file_with_a_${var}_in_it.txt"

You can also use New-Item to create directories, but a shorter way to do this is to use mkdir instead, which is a built-in "alias" function for essentially calling New-Item -ItemType Directory:

# New-Item -ItemType Directory
mkdir dirA, dirB, dirC

Some tips

  • If you want to suppress the output, you can pipe or redirect the output. Add | Out-Null or
    > $null to either example above (errors and other streams will still show as written).

  • Both commands support fully-qualified paths and relative paths. You can provide a single string if you are only creating one item.

  • You can add as many array elements as you would like, just separate each element in the array with a comma ,. You can also provide an array variable instead if you need.

codewario
  • 19,553
  • 20
  • 90
  • 159
0

here is one way to generate a series of files with powershell. [grin]

the code ...

1..9 |
    ForEach-Object {
        New-Item -Path $env:TEMP -Name ('FileNumber_{0}.txt' -f $_) -ItemType 'File'
        }

what it does ...

  • generates a number range
  • pipes each number to the New-Item cmdlet
  • uses the -f string format operator to build the file name
  • creates the file

truncated output ...

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        2022-02-03   1:10 PM              0 FileNumber_1.txt

[*...snip...*] 

-a----        2022-02-03   1:10 PM              0 FileNumber_9.txt

if you want to get rid of the output to the screen, add $Null = in front of the New-Item line.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26