Link to home
Start Free TrialLog in
Avatar of JGH5
JGH5Flag for United States of America

asked on

DOS command to get all files in a directory

I am trying to extract a list of all file names and extensions on a file share and all I have been able to extract the root folder.  What am I missing here?

FOR %%i IN \\DIR\data\jgh\ Incoming\*.* DO echo %%i >> List_Incoming.txt
Avatar of Bill Prew
Bill Prew

It's easier than that.  Do this:

dir /b /s /a-d "\\DIR\data\jgh\Incoming\*.*" >>List_Incoming.txt

Open in new window

~bp
To get subfolders, a command like DIR  /s works. So put a /s after *.*  . /s is the subfolder switch .

... Thinkpads_User
Avatar of JGH5

ASKER

I am changing the extension to a .bat file.  Which folder should I execute the .bat command from? The root or within the directory?
If you did want to use a FOR, you need the /R option to drill into subfolders, like:

for /R "\\DIR\data\jgh\Incoming" %%A in (*.*) do echo %%A>> List_Incoming.txt

Open in new window

and if you just want the file base name and extension, without the path you could do:

for /R "\\DIR\data\jgh\Incoming" %%A in (*.*) do echo %%~nxA>> List_Incoming.txt

Open in new window

~bp
==> I am changing the extension to a .bat file.  Which folder should I execute the .bat command from?
==> The root or within the directory?

Since you have the full path to the folder to list the files in, it doesn't matter where the BAT file resides, or what the current directory is when you run it.  Do keep in mind though that since you haven't fully qualified the output file path (it's just the file name), then it will be created in the current folder when the BAT script is run.

~bp
@JGH5:
Since you quoted "able to extract the root folder." I was just wondering if you needed to include the sub-directories and there content?

Cheers
Avatar of JGH5

ASKER

I found a solution:
 
FOR /r /w %%a IN (*.*) DO echo %%a >> File_List.txt

Pause

Does anyone know how to include the Date Modified for each file in the sub directories?
ASKER CERTIFIED SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@JGH5:
My answer is more %%~ta then the command line itself, because I am not familliar with the /w, in the FOR loop.

Billprew would know though.

Cheers
==> I found a solution:

Isn't that essentially what I proposed?

As Rene indicates, %%~ta will get you the date modified.

~bp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I fired up a fully updated XP Pro virtual PC and the same for me. The /W is not accepted as an option on the FOR.

Just currious, where did you get this info? Does it work for you?

Cheers,
Rene
Avatar of JGH5

ASKER

Still working on getting this to work. I have WIN 7 as well and still tweaking.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad I could help and thanks for the points.

Cheers,
Rene