4

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?

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. Commented Feb 3, 2022 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 ... Commented Feb 3, 2022 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 .. Commented Feb 3, 2022 at 18:54
  • 2
    What about for /L %I in (1,1,4) do rem/> "file%I.txt"? Commented Feb 3, 2022 at 20:12

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

from cmd
echo. >file1.txt & echo. >file.txt

Comments

0

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

enter image description here

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

enter image description here

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)))

enter image description here

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!

Answer

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"

enter image description here

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.