DOS - list files in folders without path

tuxalot

New member
Local time
10:08 PM
Messages
12
I have a text file showing files in directories. The text file looks like this:

"C:\folderA\file1.zip"
"C:\folderA\folderB\folderC\file2.zip"
"C:\folderA\folderE\folderX\folderU\file3.zip"

The text file is created by using a batch file with this code segment:
Code:
for /r %inpath% %%g in (*.zip) do echo "%%g" >> textfile.txt
Now, I wish to use the same directory tree and build a second text file with a list of the zip file names contained within, but I want to strip out the folder tree and just list the zip file names. And the zips are on different levels as the above example shows. As part of the solution, I would like to list just the file name and not the extension, like this (note the quotes which are intentional and should appear in the text file):

"file1"
"file2"
"file3"

Thanks for looking and I appreciate any help.
 

My Computer

OS
Windows XP
:D:D:D oystercatcher :D:D:D

Fantastic. You don't know how many said it can't be done, that I would need to use VB, coupled with windows scripting, and some even suggested excel to parse the file name from the full path.

Your a life saver. Thanks for your expertise. I'm grateful.

This is my final code, run in a batch file at the top level directory:

Code:
for /r %inpath% %%g in (*.zip) do echo "%%~ng" >> test.txt

Tux.
 
Last edited:

My Computer

OS
Windows XP
Nice scripts, i'll be adding those to my repository :)

FYI - If you wanted to to see where the files came from without the path. you may consider something like

tree > "test.txt" /A /F && test.txt
OR
tree "E:\" > "test.txt" /A /F && test.txt
(if you wanted to run it on the E drive for example)
 

My Computer

Computer Manufacturer/Model Number
Custom Hack job
OS
Windows 7 Ultimate 64 bit
CPU
Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz
Motherboard
GigaByte EP45-UD3
Memory
8 GB Kingston
Graphics Card(s)
Nvidia GeForce 9600GT
Sound Card
Soundblaster Audigy
Monitor(s) Displays
AOC x2 22"
Hard Drives
2 x Samsung 500GB (RAID)
2 x Samsung 2TB
1 x WD Green 2TB
1 x Seagate 1TB
1 x Samsung 500GB
PSU
Thermaltake QFan 750W
Case
CM Scout Gaming
Cooling
Coolermaster V10
Keyboard
Microsoft Wireless Keyboard 1000
Mouse
Microsoft Wireless Optical 2000
Internet Speed
320
Other Info
Winfast PVR2000CP
LG DVD-RAM DVD/RW/DL Super Multi
Linksys WMP300Nx2 Wireless Adapter
Thanks Tim and Happy New Year to all!

I have one more request to make this code really sing. Consider this file list:

fileA.zip
fileB.zip
fileC.ext1
fileC.ext2
fileD.ext1
fileE.ext1

Is it possible to add logic to the batch so the resulting text file would only show:

fileA
fileB
fileC

In other words, even though fileD and fileE are ext1, there is no matching ext2 so they are skipped, or in speaking terms, "find and index all ext1 but only if there is also an ext2 file present with the same name."

Way above my pay grade but hopefully someone here can offer some guidance.

Thanks again,

Tux.
 

My Computer

OS
Windows XP
ok, here is one failed attempt:

Code:
for /r %inpath% %%a in (*.ext1) do CALL :Subroutine %%g
GOTO:EOF

:Subroutine
if exist "%%g.ext2" do echo "%%~ng"
pause
Or maybe this, but it also doesn't work:

Code:
for /f "tokens=* delims=" %%k in ('dir /s /b *.ext1') do CALL :Subroutine %%g
GOTO:EOF

:Subroutine
if exist "%%g.ext2" do echo "%%~ng"
pause
back to reading...thanks for your help in advance
 

My Computer

OS
Windows XP
If doesn't have to be a CMD script. It can be done easily in VBScript. Let me know if you would like me to code it and post it.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell OP7010
OS
Windows 7 Enterprise (x64); Windows Server 2008 R2 (x64)
Memory
16GB
Monitor(s) Displays
4 Dell 24" LCD
Screen Resolution
1280x1024
Keyboard
Dell
Mouse
Dell Optical
Internet Speed
40meg
WindowsStar: While I would prefer to see a solution using CMD (for learning purposes mainly) a VB script would be good as well. Also, the app that this is written for is VB, so that might make it easier to integrate it directly into the main app :)

If we go that route maybe you could offer guidance on switching the small batch to VBS. Code is attached.

Thanks, and I look forward to your response.
 

Attachments

My Computer

OS
Windows XP
I have to run out and do some errands. I will do this later tonight...say 5 hours from now. Just don't want you checking back every 15 min.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell OP7010
OS
Windows 7 Enterprise (x64); Windows Server 2008 R2 (x64)
Memory
16GB
Monitor(s) Displays
4 Dell 24" LCD
Screen Resolution
1280x1024
Keyboard
Dell
Mouse
Dell Optical
Internet Speed
40meg
Thanks a lot. I attached the .bat file and maybe switching it over to VBS would be a good idea for the reasons mentioned in that post.

I'll check in later then. have a good one, Tux
 

My Computer

OS
Windows XP
Hi WindowsStar,

I got it working in CMD with a LOT of help from various sources. So if I caught you soon enough please don't spend too much time on a VBS solution.

I can paste the .bat if you want to see it.

Thanks,

Tux
 

My Computer

OS
Windows XP
Sure paste it here...
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell OP7010
OS
Windows 7 Enterprise (x64); Windows Server 2008 R2 (x64)
Memory
16GB
Monitor(s) Displays
4 Dell 24" LCD
Screen Resolution
1280x1024
Keyboard
Dell
Mouse
Dell Optical
Internet Speed
40meg
Attached. The only thing I'd like to add possibly is a progress timer for processing large directories.
 

Attachments

My Computer

OS
Windows XP
Hi Tuxalot,

Nice Work!

BTW - It doesn't work for me unless I set the current path in the batch at the start.
Code:
:: SET Automatically the current path (i.e. the path that this is run from) 
 set  curpath=%~dp0
:: CD to the current path
 pushd  %curpath%

I would also recommend using variables for the program files location rather than hardcoding it to the C drive.
Code:
%programfiles%\SHCK\SHCKdocs

In regards the bit of code that you didn't know, the %~dp0 is for expanding drive letters and paths.

If you want to add a progress timer, try counting the total number of files in the location, here's some code for C:\ to get you started.
Code:
dir "C:\" /b | find /v /c "::"
Now set up a loop that displays an ongoing count after one of your %outfile% write entries.

Hope this helps.
 

My Computer

Computer Manufacturer/Model Number
Custom Hack job
OS
Windows 7 Ultimate 64 bit
CPU
Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz
Motherboard
GigaByte EP45-UD3
Memory
8 GB Kingston
Graphics Card(s)
Nvidia GeForce 9600GT
Sound Card
Soundblaster Audigy
Monitor(s) Displays
AOC x2 22"
Hard Drives
2 x Samsung 500GB (RAID)
2 x Samsung 2TB
1 x WD Green 2TB
1 x Seagate 1TB
1 x Samsung 500GB
PSU
Thermaltake QFan 750W
Case
CM Scout Gaming
Cooling
Coolermaster V10
Keyboard
Microsoft Wireless Keyboard 1000
Mouse
Microsoft Wireless Optical 2000
Internet Speed
320
Other Info
Winfast PVR2000CP
LG DVD-RAM DVD/RW/DL Super Multi
Linksys WMP300Nx2 Wireless Adapter
Thanks for the insight Tim.

BTW - It doesn't work for me unless I set the current path in the batch at the start.
I wonder why you need to set the current path? While it seems like a good idea anyways, it was working for me.

I would also recommend using variables for the program files location rather than hardcoding it to the C drive.
I've incorporated these changes. I may wait on the progress timer idea...I've found a more pressing issue that I need to address. It turns out that the application that uses these text files, needs the files in ascending alpha order. Argggg!

Thinking more about this, what about this approach?

To summarize, the remaining goal is to sort Output1 and Output2. Output1 is FQPN of zip and mp3 files. Output2 is just a sorted list of filenames.

So if the code can produce a temporary outputX that looks like this:

file1.zip(a delimiter of some type)D:\folder1\folder2\folder3\etc...\file1.zip

Now, we can sort on file name and build outputX. So this is the code for that:
Code:
PushD "%_Source%"
For /F "Tokens=* Delims=" %%g In ('Dir /A-D /B /S *.zip 2^>Nul') Do Echo."%%~ng"\#"%%~dpng">>"%_OutFileX%"
SORT "%_OutFileX%" > "%_OutFileXS%"
After, we read OutFileXS back into the script and split the data into two separate files at the delimiter (I chose # as the delimiter) yielding Output1, Output2. Here is the code for that but it's not working:
Code:
for /F "Tokens=1 delims=#" %%a In ("%_OutFileXS%") Do Echo.%%a>>"%_OutFile2%"

Any help is appreciated.

Tux.
 
Last edited:

My Computer

OS
Windows XP
Code is done now. I thank all who helped me with this. I can post the batch if anyone wants to see it.

Thanks again,

Tux.
 

My Computer

OS
Windows XP
Please post it. Would like to see what you came up with. Plus others maybe able to use it.
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Dell OP7010
OS
Windows 7 Enterprise (x64); Windows Server 2008 R2 (x64)
Memory
16GB
Monitor(s) Displays
4 Dell 24" LCD
Screen Resolution
1280x1024
Keyboard
Dell
Mouse
Dell Optical
Internet Speed
40meg
I'm working through some tests with the script today and will post later.

Tux.
 

My Computer

OS
Windows XP
Hi there
why not start with something REALLY SIMPLE such as a bog standard DOS command.

FOR %%i IN (directory\*.*) DO echo %%i >> list.txt

This should put the name of every file in directory into list.txt

You can route it to a spreadsheet or whatever as well.

Cheers
jimbo
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom built, several laptops HP/ASUS
OS
Linux CENTOS 7 / various Windows OS'es and servers
CPU
Intel i7 Intel i5
Memory
8GB, 16GB
Graphics Card(s)
On Motherboard
Sound Card
Realtek HD audio
Monitor(s) Displays
Apple Cinema display, Samsung LCD
Screen Resolution
1920 X 1080
Hard Drives
4 X 1TB SATA
Mouse
Toshiba wireless laser
Internet Speed
> 20MB up
jimbo45: Could do...but then would still have to parse out the extensions that I need, filter the ones I do not, and build the two output files that my app needs. So I don't see the point. Unless of course I could come up with a way to execute the batch faster, that would be sweet.

I cannot take much (if any) credit for the attached batch. It was a melding of efforts from various sources. But it works and I'm grateful!

Thanks all for the assistance on this.

Just change the extension to bat and run.

Cheers,

Tux.
 

Attachments

My Computer

OS
Windows XP
Back
Top