Solved

Batch Command - Add File Names

Posted on 2011-09-14
6
260 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
ID: 36540719

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

Expert Comment

by:jimmart
ID: 36540867
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
ID: 36542179
WOW jimmart... never seen such a long routine to doa one line dir command!!

Steve
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:jimmart
ID: 36542370
Its all in the error checking baby!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36542406
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
ID: 36542890
Perfect Solution!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 109
VBA: Select SQL query based on a config Sheet v2 11 49
move command 5 62
Recommendation vb6 to vb.net or others 14 174
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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

840 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