Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Filenames in Subdirectories  (Read 4112 times)

0 Members and 1 Guest are viewing this topic.

mbm

    Topic Starter


    Starter

    Filenames in Subdirectories
    « on: October 09, 2009, 02:32:03 PM »
    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. 

    macdad-



      Expert

      Thanked: 40
      Re: Filenames in Subdirectories
      « Reply #1 on: October 09, 2009, 03:05:56 PM »
      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-)
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      mbm

        Topic Starter


        Starter

        Re: Filenames in Subdirectories
        « Reply #2 on: October 09, 2009, 03:36:19 PM »
        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. 

        macdad-



          Expert

          Thanked: 40
          Re: Filenames in Subdirectories
          « Reply #3 on: October 11, 2009, 02:46:47 PM »
          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.
          If you dont know DOS, you dont know Windows...

          Thats why Bill Gates created the Windows NT Family.

          Salmon Trout

          • Guest
          Re: Filenames in Subdirectories
          « Reply #4 on: October 12, 2009, 12:14:14 AM »
          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


          macdad-



            Expert

            Thanked: 40
            Re: Filenames in Subdirectories
            « Reply #5 on: October 12, 2009, 08:41:09 AM »
            Fool me twice.
            If you dont know DOS, you dont know Windows...

            Thats why Bill Gates created the Windows NT Family.

            mbm

              Topic Starter


              Starter

              Re: Filenames in Subdirectories
              « Reply #6 on: October 13, 2009, 11:19:00 AM »
              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