Solved

Batch Command - Add File Names

Posted on 2011-09-14
6
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

717 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