Need a batch file to list all folders and a specific file

October 8, 2012 at 13:56:16
Specs: Windows 7 Enterprise, Core i5 2.4GGHz/4gb
Hi.

Before anyone gets on me about SEARCHING the forums, I have, but didnt find anything that worked to well. If there is something in the forums, then i missed it.
I need a batch file to list all the folders in a certain directory and list a specific file in each of those folders while outputting this to txt or xlsx.
the file in the folder i am looking for is a .tpm file. i would also like to strip the extension when i run this but if not then i can strip it in excel i guess.
id like the file to list everything like this if possible:

folder name
file name
folder name
file name
etc...

i got this to give me the folder name but i am a bit green creating batch files.

dir /a /b /-p /o:gen >list.txt

any ideas?
thanks in advance : )


See More: Need a batch file to list all folders and a specific file

Report •

#1
October 8, 2012 at 15:25:50
Will the folder names contain spaces?

Report •

#2
October 8, 2012 at 20:36:50
::===== begin script
(for /f %%a in ('dir /b /s *.tpm') do @echo %~dpa & @echo %~na) >list
::====== end script

although i was confused about a couple of things. you said "a .tpm file", but you also said "a specific file", so if specific, replace the asterisk with the filename you want to find. As for "folders" vs "subdirectories", is there a difference? Things change, and I'm never sure when I'm on the trailing edge and sinking fast. I assumed that folders were same as subdirectories.


Report •

#3
October 9, 2012 at 08:13:33
@thebest123456 - yes they will have spaces

Report •

Related Solutions

#4
October 9, 2012 at 09:33:33
@nbrane - sorry that didnt work for me.
the specific file is the .tpm file.
nothing happens when i run it

Report •

#5
October 9, 2012 at 17:29:03
was there any content in file "list"? (the output of the script). errors etc?

Report •

#6
October 10, 2012 at 10:24:03
@nbrane - no errors, no output file.

im almost there with this:

dir /s /b *.tpm /o:gen >List.txt

but it gives me the full folder path with it.
trying to figure out how to remove the path so i just get the folder and filename


Report •

#7
October 10, 2012 at 11:50:33
It's not much help saying it nearly works without providing any specific details as to what you are trying to fix.
E.g post the location.

Report •

#8
October 10, 2012 at 12:07:12
@thebest123456 - i did give you the details and what i need to fix...

"but it gives me the full folder path with it.
trying to figure out how to remove the path so i just get the folder and filename"


Report •

#9
October 10, 2012 at 12:12:10
have you tried

for /f %%G IN('dir /s /b *.tpm /o:gen')DO echo %%~nxG>List.txt?

:: mike


Report •

#10
October 10, 2012 at 12:48:53
@mikelinus - i am getting the error below

IN('dir was unexpected


Report •

#11
October 10, 2012 at 13:48:17
for /f %%G IN ('dir /s /b *.tpm /o:gen') DO echo %%~nxG>List.txt?
oops forgot spaces/they got truncated.

:: mike


Report •

#12
October 10, 2012 at 13:54:31
nope didnt work either. i didnt get the error but the only thing the txt doc had in there was the word windows

Report •

#13
October 10, 2012 at 18:44:44
apologies to op. I was working from commandline, so i got percents all f_d up.
here is another go:
::===== begin script
(for /f "tokens=*" %%a in ('dir /b /s /o:gen *.tpm') do @echo %%~dpa & @echo %%~na) >list
::====== end script

also fixed (added) 'tokens=' to handle spaces in filenames, added '/o:gen' to get the specified sorting order.
My mistake altogether. hope this one flies.


Report •

#14
October 11, 2012 at 06:20:47
@nbrane - ok this works but it seems to be the same problem with the batch i was running. it gives the entire folder path.
i was using this:
dir /s /a:a *.tpm /b /o:gen >List.txt
i like yours better since it removes the extension but is there a way to remove the file path so its just the folder name and the file name?
thanks

Report •

#15
October 11, 2012 at 06:42:33
This sounds like an intermediate step in some long and convoluted workflow. Have you considered reducing or altering it to simplify the steps?

How To Ask Questions The Smart Way


Report •

#16
October 11, 2012 at 06:53:08
i am open to whatever anyone might suggest as long as the end result gives me a list with the folder name and the file name.
i thought something like below was simple.
dir /s /a:a *.tpm /b /o:gen >List.txt
but like i said i am open thats why i am here for a little help

Report •

#17
October 11, 2012 at 07:15:52
Let me rephrase:
Why do you want a spreadsheet that includes a list of paths without files, and a list of files without paths?

If it's for a report that a human will read, you should probably look into using another scripting language, as that will allow some basic formatting you'd apply manually.

If it's for a report that a computer / program will read, you're just making more work for the computer / program.

How To Ask Questions The Smart Way


Report •

#18
October 11, 2012 at 07:32:02
If folder location is a variable use this:
@echo off & setlocal enabledelayedexpansion
set folderlocation=?
set /P folderlocation=Enter main directory:
for /f "delims=" %%a in ('dir /s /a:a *.tpm /b /o:gen') do set filtered=!filtered!%%a
set filtered=!filtered:%folderlocation%=!

if it's static just set folderlocation to what ever you want and remove set /P folderlocation=Enter main directory:

Report •

#19
October 11, 2012 at 07:40:29
@Razor2.3 - i never said i want a list of paths without files, and a list of files without paths. im not sure where you got that from?
ive stated i want the batch to spit out the folder name and the .tpm file only.
i said that i do not want the full folder path included.
end result should be:

john smith
4323423
jane doe
8756675

i do not want:

\\server\parent folder\john smith
4323423

i can already get to do give me the info i need BUT it gives me the full folder path with it which i do not want.


Report •

#20
October 11, 2012 at 07:46:37
Re-posting to gain your attention:
If folder location is a variable use this:
@echo off & setlocal enabledelayedexpansion
set folderlocation=?
set /P folderlocation=Enter main directory:
for /f "delims=" %%a in ('dir /s /a:a *.tpm /b /o:gen') do set filtered=!filtered!%%a
set filtered=!filtered:%folderlocation%=!

if it's static just set folderlocation to what ever you want and remove set /P folderlocation=Enter main directory:

///Edit just noticed you want \\server\parent folder\ removed, use this
This works better:
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /s /a:a *.tpm /b /o:gen') do set filtered=!filtered!%%a
set filtered=!filtered:\\server\parent folder\=!
set filtered=%filtered:\= %
echo.%filtered%> list.txt
pause > nul
exit /b

Report •

#21
October 11, 2012 at 07:46:43
Adjust to your needs:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CD \Temp
FOR /f "tokens=* delims=" %%A IN ('DIR /b /s /o:gen *.tpm') DO (
    SET folder=%%~dpA
    SET folder=!folder:~0,-1!
    FOR /f "tokens=* delims=" %%B IN ("!folder!") DO SET folder=%%~nxB
    ECHO Full path = "%%A"
    ECHO Folder = "!folder!" 
    ECHO File = "%%~nA"
    ECHO(
)

EXIT /B

Results I get having 3 .tpm files in various folders:

Thu 10/11
 9:51:28=>28506
Full path = "E:\Temp\Your\Test.tpm"
Folder = "Your"
File = "Test"

Full path = "E:\Temp\Your\Other\Path with spaces\Test.tpm"
Folder = "Path with spaces"
File = "Test"

Full path = "E:\Temp\Your\Other\Path with spaces\To\Test\Test.tpm"
Folder = "Test"
File = "Test"

When your only tool is a hammer, every problem looks like a nail.


Report •

#22
October 11, 2012 at 08:24:44
@orangeboy - thanks but thats a bit more info than i need.
i am just looking for the folder name and file name

Report •

#23
October 11, 2012 at 08:27:17
@thebest123456 - i tried :

@echo off & setlocal enabledelayedexpansion
set folderlocation=?
set /P folderlocation=Enter main directory:
for /f "delims=" %%a in ('dir /s /a:a *.tpm /b /o:gen') do set filtered=!filtered!%%a
set filtered=!filtered:%folderlocation%=! >list.txt

but i got a blank txt file


Report •

#24
October 11, 2012 at 08:28:17
Try:
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /s /a:a *.tpm /b /o:gen') do set filtered=!filtered!%%a
set filtered=!filtered:\\server\parent folder\=!
set filtered=%filtered:\= %
echo.%filtered%> list.txt
pause > nul
exit /b


Report •

#25
October 11, 2012 at 08:33:33
ok i got results from that but its giving me the folder path without the \ and its all one long string for the results. theres no separation for each folder

Report •

#26
October 11, 2012 at 08:45:20
JoeSpats: @Razor2.3 - i never said i want a list of paths without files, and a list of files without paths. im not sure where you got that from?
I got it from the OP:
folder name
file name
folder name
file name
etc...

That's two separate lists, interspersed with each other. That's fine if you're doing a human consumed report. It's also busy work for both you and the consumer if this report is going to be fed into another program / script. That's why I'm inquiring about the workflow.

How To Ask Questions The Smart Way


Report •

#27
October 11, 2012 at 08:46:05
Think about it this way, the string following the .tmp file is the folder.

Report •

#28
October 11, 2012 at 09:12:05
That is why I prefixed with: "Adjust to your needs."
If you don't want the full path, remove
ECHO Full path = "%%A"

If you don't want "Folder = ...", alter it to:
ECHO !folder! 

If you don't want "File = ...", alter it to:
ECHO %%~nA

I was more verbose in the output so you could determine that the method used would suit your needs with a little effort on your part.

When your only tool is a hammer, every problem looks like a nail.


Report •

#29
October 11, 2012 at 10:00:28
@orangeboy - my apologies. i have made the adjustments and it looks like what i want but i cant export it.
what am i missing?
i tried EXIT /B >list.txt but that didnt work.
i also tried ECHO(
) >list.txt but no dice.
when that exports to txt will each grouping of folder and file have a space between them or will there be no spaces? i prefer no spaces if possible.
meaning now it shows this:
user name
file

user name
file

user name
file

etc....

i think id prefer:
user name
file
user name
file
user name
file

thanks


Report •

#30
October 11, 2012 at 10:43:32
Tell me the number of spaces in between the lines.

Report •

#31
October 11, 2012 at 11:05:35
looks like 1 space to me.
id prefer no space

Report •

#32
October 11, 2012 at 13:51:30
Ok, remove:
ECHO(

That will eliminate the spaces.

Similar to your original post where you indicated you've tried:

dir /a /b /-p /o:gen >list.txt

use instead:

Name.of.bat.file.you.copied.and.altered.from.this.topic.bat >list.txt

When your only tool is a hammer, every problem looks like a nail.


Report •

#33
October 11, 2012 at 19:10:52
oops! orangeboy nailed this one (good job!). I posted before I saw the last entries.

Report •

#34
October 12, 2012 at 07:18:29
ok that sorta fixed it.
at first i was getting this msg:
the system cannot find the specified path

then the msg changed to this:
the system cannot find the specified path
Maximum setlocal recursion level reached

that msg displayed for every folder and file before it would write to the txt file so i removed CD \Temp and that got rid of that error but now i am getting:

Maximum setlocal recursion level reached

it takes a long time to write to the file and there are only 80 folders and files. it ran for 13 minutes before i cut it off.

i removed what you said and addedat the end:

test2.bat >list.txt

any thoughts?


Report •

#35
October 12, 2012 at 15:17:45
...but now i am getting:
Maximum setlocal recursion level reached
it takes a long time to write to the file and there are only 80 folders and files. it ran for 13 minutes before i cut it off.
i removed what you said and addedat the end:
test2.bat >list.txt
any thoughts?

Remove

test2.bat >list.txt
from the test2.bat file and invoke the bat file (from the command line) like:

test2.bat >list.txt

By having

test2.bat >list.txt
inside the actual batch file, you are creating an unending loop of calling the batch file from within itself.

When your only tool is a hammer, every problem looks like a nail.


Report •

#36
October 12, 2012 at 18:46:13
well, I trashed this response, but since/(if?) you're still having problems, I'll revive it. It is just a reiteration of the code Orangeboy has already posted, basically.
::==== begin script
@echo off & setlocal enabledelayedexpansion
(for /f "tokens=*" %%a in ('dir /b /s /o:gen *.*') do (
set p=%%~pa
set p=!p:~0,-1!
for /f "tokens=*" %%b in ("!p!") do echo %%~nxb
echo %%~nxa
)) > list
echo output is in file "list", which has this content:
more list
::====== end script

Report •

#37
October 15, 2012 at 06:28:14
@orangeboy & @nbrane - ok i combined the both of yours and i got what i need.

thanks so much guys for your help. really appreciate it.


Report •

Ask Question