• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Batch Command - Add File Names

Any ideas how to write all file names in a folder to a single new text file using batch commands? Thank You
1 Solution

dir /b/A-D c:\foldername >yourfilename.txt
I don't really have enough info from you question to give you a perfect answer but this batch file will probably do what you need:

@echo off
:: ListAllFiles.cmd
:: Will list all files in a directory and display them or put the list of name into a file.

setlocal enableextensions

if not "%~1"=="/?" goto:nohelp

echo Will place a list of all files in a directory into a file or to the screen.
echo %~n0 [/h] directory [filename]
echo        /h  List hidden files also.
echo directory  The directory to list the files from.
echo            Use a dot (.) for the current directory.
echo  filename  The filename to store the list.
echo            If no filename is given the list will be displayed on the screen.

set _scriptname=%~n0

:: Test for /h flag.
set _dirflag=/a-d-h
if "%~1"=="/h" set _dirflag=/a-d
if "%~1"=="/H" set _dirflag=/a-d
if "%_dirflag%"=="/a-d" shift

set _directory=%~1
:: Test that the directory exists.
if exist "%_directory%\*.*" goto:directory_exists
echo The %_directory% directory does not exist.
echo %_scriptname% aborted.

set _filename=%~2
if "%_filename%"=="" goto:show_list
:: Test if the filename exists.
if NOT exist "%_filename%" goto:filename_new
SET /P _answer=There is already a "%_filename%" file. Overwrite [y^|n]? 
if "%_answer%"=="y" goto:filename_delete
if "%_answer%"=="Y" goto:filename_delete
if "%_answer%"=="n" goto:abort
if "%_answer%"=="N" goto:abort

del "%_filename%"
if NOT exist "%_filename%" goto:filename_new
echo Unable to delete "%_filename%".

:: Ensure we can create filename.
if exist "%_filename%" goto:filename_OK
echo Unable to create "%_filename%".

del "%_filename%"
for /f %%a in ('dir %_dirflag% /b /one "%_directory%"') do echo %%a>>"%_filename%"
echo The file %_filename% contains the list of files from "%_directory%".

echo The list of files from "%_directory%" is:
for /f %%a in ('dir %_dirflag% /b /one "%_directory%"') do echo %%a

Open in new window

Steve KnightIT ConsultancyCommented:
WOW jimmart... never seen such a long routine to doa one line dir command!!

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Its all in the error checking baby!
Steve KnightIT ConsultancyCommented:
Surprised you don't have an option or three to sort it, open it in Notepad when finished etc...

Looks impressive when you justify the 6 week project for producing a directory listing!!

GarySBAuthor Commented:
Perfect Solution!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now