Nick
Nick

Reputation: 1

Batch command to create empty txt files with the file names in a folder

I have a folder of files, e.g: 001zzzqqq.* 002bbbccc.* 003nnnfff.* ...

And want to create a blank text file named after each of those files, e.g: 001zzzqqq.txt 002bbbccc.txt 003nnnfff.txt ...

Any quick way to knock this up in batch file? My mind seems to have gone blank on this.

Thanks

Upvotes: 0

Views: 7723

Answers (2)

Joey
Joey

Reputation: 354606

Iterate over the files in the folder:

for %x in (*) do ...

Create empty files:

type NUL > %~nx.txt

The %~nx evaluates to the file name without the extension of the loop variable %x. So, combined:

for %x in (*) do type NUL > %~nx.txt

You could also use copy NUL %~nx.txt but that will output 1 file(s) copied and throw errors if the text file already exists; this is the more silent variant (or use copy /Y NUL %~nx.txt >NUL 2>&1).

In a batch file you need to double the % but you won't need a batch file just for this one-liner (except it's part of a larger program):

for %%x in (*) do type NUL > %%~nx.txt

Upvotes: 7

ghostdog74
ghostdog74

Reputation: 342463

@echo off
for /f %%a in ('dir /B') do (
  wuauclt > %%~na.txt
)

Upvotes: 0

Related Questions