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?
With PowerShell you can use New-Item with an array of filenames.
Note:
niis a built-in alias forNew-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
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.
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 ...
New-Item cmdlet-f string format operator to build the file nametruncated 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.
TL;DR
I finally decided one line is actually enough for any number of files, but to see the slow thought process that led me there. Here are my workings towards that last single line!
Well to keep it a short cmd just use a for loop. Remember inside a batch file both %c must be %%c.
For /l %c in (1,1,100) do echo.>file%c.txt
The above method shows as 2 byte files (cr=hex 0D0A) of 0 disk space and the OP aim is 0 byte file using cd . > However it does not really matter since AFAIK in general 4 KB will be reserved in both cases.
However a "Gotcha" is that you should always avoid unpadded numbers in filenames. Thus better if done per number of digits.
Without using complicated for nested we can simply use in a template batch file, copy and paste. then edit the numbers to suit your needs. Also break is a suggested "cleaner" method.
Note the following is not strictly correct in naming u=units t=tens h=hundreds but shows how it needs reversing later on in "nesting".
For /l %%u in (1,1,9) do break >file-00%%u.txt
For /l %%t in (10,1,99) do break >file-0%%t.txt
For /l %%h in (100,1,999) do break >file-%%h.txt
As the question asks for one line command. We can simply append similar on the command line, however we do need to use nesting and the result will include 000 so be 1000 files.
@For /l %h in (0,1,9) do @(For /l %t in (0,1,9) do @(For /l %u in (0,1,9) do @(break >file-%h%t%u.txt)))
Should you need a lesser number such as up to 256 we would need to run 3 separate lines.
@For /l %h in (0,1,1) do @(For /l %t in (0,1,9) do @(For /l %u in (0,1,9) do @(break >file-%h%t%u.txt)))
@For /l %t in (0,1,4) do @(For /l %u in (0,1,9) do @(break >file-2%t%u.txt))
@For /l %u in (0,1,6) do @(break >file-25%u.txt)
Hence it would then be simpler to use a batch file, rather than mental maths!
However on reflection we can simply use a loop with any start and end value as seen here. From 0075 to 0128 thus vary the first and last number in the /Loop and alter the -4 as desired.
"%comspec%" /V=ON /C "For /L %a in (75,1,128) do @(set "c=000%a") & @break >file-!c:~-4!.txt"
cd.> filename.txtis of invalid syntax. There is a space character missing between the commandcd(argument 0) and the directory name.(current directory). So correct would becd .> filename.txtorcd .>filename.txt.cd.results in accessing the file system searching for a file with namecd. The dot at end is removed by the Windows file IO functions as described by the Microsoft documentation about Naming Files, Paths, and Namespaces.cdor a file with namecdwhich is not an executable, the usage ofcd.> filename.txtinstead ofcd .>filename.txtresults 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 thatcmd.exerecognizes 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 ...cmd.exe. The usage ofcd.works just due to automatic error correction oncmd.exenot finding in file system an entry with namecdand so starts analyzing what could be the reason and detects now that the user means its internal commandcdwith the argument..for /L %I in (1,1,4) do rem/> "file%I.txt"?