Solved Create .txt files of all filenames in a directory

June 3, 2015 at 08:05:02
Specs: Windows 7
Hello, I was wondering if there is a way to use command line batch processing to basically create an empty .txt file for each file name that exists in a directory?

My directory contains some 1000 files and I need to have .txt files named exactly the same as the directory contents.

I have:
cde.xls (and so on)

I want to run a command batch file that will allow me to quickly create:
cde.txt (and so on)

The text files will be empty, I will populate them with the comments that need appended for each file.

The final result of the directory in my example would be:

...and so on...

Can anybody help me with accomplishing this?



See More: Create .txt files of all filenames in a directory

Report •

June 3, 2015 at 09:32:00
This script performs the given task. Place the script in a .bat file inside the folder you want to perform the action.

It will not work if you have multiple files with the same name, or if you have files with no name and only extension.

@echo off
setlocal enabledelayedexpansion
for /f %%A in ('DIR /B /A:-D') do (
for /f "tokens=1 delims=." %%B in ("%%A") do (
if exist %%B.txt (
Echo ERR: %%B.txt already exists. txt file for "%%A" skipped.
) else (
echo. >%%B.txt
Echo Operation complete.
pause > nul

To make the script work with equal file names, replace this line
for /f "tokens=1 delims=." %%B in ("%%A") do (
for /f %%B in ("%%A") do (

If it processes a file "abc.xls", you'll end up with a text file called "abc.xls.txt".

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ

Report •

June 3, 2015 at 09:46:15
Store the following batch in the directory that holds the files you want to process and you are done.

@echo off
for /F "delims=" %%i in ('dir /B /A-D') do type nul > "%%~ni.txt"
del "%~n0.txt"

Report •

June 5, 2015 at 06:13:10
✔ Best Answer
The script I posted in post#2 is for "one shot only" execution. If you need to repeat the process in a previously processed directory, use instead the following one that leaves the existing .txt files untouched.

@echo off
for /F "delims=" %%i in ('dir /B /A-D') do (
  if /I not "%%~xi"==".txt" if not exist "%%~ni.txt" type nul > "%%~ni.txt"
del "%~n0.txt"

Report •
Related Solutions

Ask Question