1.

Solve : Filenames in Subdirectories?

Answer»

I'm trying to figure out a simple way to get a list of file names from a parent folder and all of its subfolders.

Currently, I’m using the command PROMPT with

dir /B /s /o:ng>Directory.txt

to print the filenames and pathways of all the files in the main folder and the subdirectories, but I also want to get a printout of just the names of the files. I can enter each subfolder and simply print a directory, but there has to be a more automated way.

I have tried using variations of the the command

(for /D /R %F in (*) do dir %F /b /o:ng) > mytextfile.txt

but it isn't working.

On a funny note, in my quest to figure this out I managed to write over the content of every file in one of my folders and its subfolders with the name of the file. My IT guys were impressed, but cautioned me to respect the power of the Command Prompt in the future They aren't sure how I did it (nor am I), and unfortunately they don't know how to do what I want. Thank goodness for backups!

Thanks in advance for any help. Fortunately that script doesn't delete anything

But if you want just the file names of a files in all SUBDIRS in a specific folder try this script:

Code: [Select]@echo off
echo. Files > DirectoryList.txt
set filecount=0
for /R "C:\Parent Folder" %%a in (.) do (
echo. File Found >> DirectoryList.txt
echo. %~dpa >> DirectoryList.txt
echo. %~nxa >> DirectoryList.txt
echo. >> DirectoryList.txt
set /a filecount+=1
)
echo. Total Files Found: %filecount% >> DirectoryList.txt
echo. Done
pause

You can drop whatever lines you want, but it should turn out like this in DirectoryList:

File Found
Filename.txt
C:\Folder\

Total Files Found: 1


Hope this helps
,Nick(macdad-)Thank you for the help.

I found that I had to put a double %% symbol in the lines where only a single was show to make the script work, though I don't know why. I also found that this gave me the names of all the subdirectories, but not the files in the subdirectory folders. I have hope though that I will figure it out though with your code. The double %% is exclusive to FOR, its basically a kind of local variable only assigned to the FOR command.

The FOR loop with the /R switch just makes FOR step thru all files in a folder and sub folders.

Code: [Select]echo. %~dpa >> DirectoryList.txtThis part of the code gets the current file's(That FOR has stepped to) Location in the folder.

Code: [Select]echo. %~nxa >> DirectoryList.txt
This one gets the actual file's name and extension.Macdad, you forgot something... You got it right in the FOR line but...

Code: [Select]echo. %~dpa >> DirectoryList.txt
echo. %~nxa >> DirectoryList.txt
...those single percent signs should be doubled: in a batch it's %%~dpa and %%~nxa

Fool me twice.OK, I'm putting this here for someone who might scour old posts like me in the future. One of the problems I was having was my program listing folder names as well as files names (which I didn't want), but also it climbing up the directory tree to include directories "." and ".."

After MUCH work and help from this forum and some smart friends, I finally came up with this which now counts the number of files and exports the names and then the file pathways of all files within a folder to text files (called "files" and "fullnames"). This includes all files stored within subfolders of the main folder.

Life is now GOOD.

@echo off
echo > files.txt
echo > fullnames.txt
set filecount=0
for /R %%f in (*) do ( echo %%~dpnxf >> fullnames.txt ) & (echo %%~nxf >> files.txt ) & (set /a filecount+=1)
echo. Total Files Found: %filecount%
echo. Done



Discussion

No Comment Found