Solved

Batch Command - Add File Names

Posted on 2011-09-14
6
257 Views
Last Modified: 2012-05-12
Any ideas how to write all file names in a folder to a single new text file using batch commands? Thank You
0
Comment
Question by:GarySB
6 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
Comment Utility

dir /b/A-D c:\foldername >yourfilename.txt
0
 
LVL 1

Expert Comment

by:jimmart
Comment Utility
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

:help
echo Will place a list of all files in a directory into a file or to the screen.
echo.
echo %~n0 [/h] directory [filename]
echo.
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.
goto:EOF

:nohelp
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.
:abort
echo %_scriptname% aborted.
goto:EOF

:directory_exists
set _filename=%~2
if "%_filename%"=="" goto:show_list
:: Test if the filename exists.
if NOT exist "%_filename%" goto:filename_new
:overwrite_ask
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
goto:overwrite_ask

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

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

:filename_OK
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%".
goto:EOF

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

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
WOW jimmart... never seen such a long routine to doa one line dir command!!

Steve
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:jimmart
Comment Utility
Its all in the error checking baby!
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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!!

Steve
0
 

Author Closing Comment

by:GarySB
Comment Utility
Perfect Solution!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now