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

Author Topic: Searching for files within subdirectories  (Read 8387 times)

0 Members and 1 Guest are viewing this topic.

Grimbear13

    Topic Starter


    Rookie

    Searching for files within subdirectories
    « on: February 03, 2010, 07:21:56 AM »
    Sorry, this probably has been answered before but,

    I'm trying to take a list of files that are potentially in a file, read the file and then search through a folder and all subdirectories for the files in the list.  I know how to read the list, however I do not know how to make it search all subdirectories without mapping each directory within its own nested for loop.

    Also then conversely I want to search all files in the directories and subdirectories and have them checked off against the list to make sure there are not any extra files in the folders.

    If someone could show me the command for subdirectory searching or an example of it I could probably figure out how to make both of these processes work.

    EDIT:  Something to take into consideration, these folders may have spaces in them and I do not have the power to rename said folders.  They are given to me to check, I'm not sure if this will be an issue when doing a nested for to check through but figured I'd bring it up now as opposed to after I get a potential answer only to have it blow up due to the space int he folder name.

    Grimbear13

      Topic Starter


      Rookie

      Re: Searching for files within subdirectories
      « Reply #1 on: February 03, 2010, 08:35:51 AM »
      Okay through some reasearch I found out how to use both "tokens=*" which would handle the spaces issue, and then I found the /R switch for the for loop but no matter how I try to use it it's always crashing.  Can someone provide an example similar to this:

      Code: [Select]
      for /F "tokens=*" %%I in (inlist) do (

      for /R "tokens=*" %%J in (*) do (
      if %%I==%%J (echo %%I CHECK >> Results.txt)
      )

      echo %%I MISSING >> Results.txt
      )

      BillRichardson



        Intermediate

        Thanked: 15
        Re: Searching for files within subdirectories
        « Reply #2 on: February 03, 2010, 10:08:07 AM »
        C:\>dir /s /b *.pdf  >  pdffiles.txt

        C:\>


        C:\>dir /s *.pdf
         
         Volume Serial Number is F4A3-D6B3

         Directory of C:\bin\doc\gawk\3.1.6\gawk-3.1.6

        12/29/2007  02:48 AM         2,058,611 gawk.pdf
                       1 File(s)      2,058,611 bytes

         Directory of C:\bin\doc\gawk\UnixV7

        07/29/2007  07:07 AM            28,217 awk_V7.1.pdf
        07/29/2007  05:41 AM            60,796 awk_V7.pdf
                       2 File(s)         89,013 bytes

         Directory of C:\bin\man\pdf

        12/28/2007  03:45 PM           315,094 gawk-man.pdf
                       1 File(s)        315,094 bytes

         Directory of C:\DELL\Docs\manual

        09/10/2007  03:44 PM        13,980,991 eom.pdf
                       1 File(s)     13,980,991 bytes
        .
        .
        .
        Bill Richardson

        Grimbear13

          Topic Starter


          Rookie

          Re: Searching for files within subdirectories
          « Reply #3 on: February 03, 2010, 11:26:23 AM »
          Thanks for the reply, but I'm not looking to see subdirectories.  I need to know how to execute commands through all subdirectories.  dir shows what subdirectories are there, but I need to be able to compare the names of all files in sub directories to the name of the file in the list.

          This is the full code that I've worked out thus far

          Code: [Select]
          ::@echo off > debug.txt
          color 0A
          ::The preceding line turns off screen printing and sets the text color to Bright Green.

          ::Created by Scott Dean

          for /F "tokens=*" %%I in (inlist) do (
          Call :ResetBool
          set filename=%%I

          for %%J in (*) do (
          set file=%%J
          Call :ValidCheck
          )
          )
          Call :Print
          Call :MissingCheck
          pause
          )
          GoTo :EOF

          :ValidCheck
          if %filename%==%file% (Goto :CheckPrint)
          GoTo :EOF

          :MissingCheck
          if %bool%==false (GoTo :MissingPrint)
          GoTo :EOF

          :CheckPrint
          echo %filename% CHECK >> Results.txt
          set bool=true
          GoTo :EOF

          :MissingPrint
          echo %filename% MISSING >> Results.txt
          GoTo :EOF

          :ResetBool
          set bool=false
          GoTo :EOF

          :Print
          echo %filename% >> lol.txt
          echo %bool% >> rofl.txt
          GoTo :EOF

          I have one of the files listed in the inlist in the directory and that gives an appropriate response in the Results.txt.  However it seems like nothing after the ValidCheck in the nested loop executes.  I figured it has something to do w/ me sending it to the end of file, but when I remove that it will execute down all of those other regions as well.  I even tried to put new regions within the loops to send it back to specific areas after execution however it bombs before it even does anything if you put regions within the for's.

          Thanks,
          « Last Edit: February 03, 2010, 11:41:31 AM by Grimbear13 »

          BillRichardson



            Intermediate

            Thanked: 15
            Re: Searching for files within subdirectories
            « Reply #4 on: February 03, 2010, 12:02:38 PM »

            "I have one of the files listed in the inlist in the directory and that gives an appropriate response in the Results.txt.  However it seems like nothing after the ValidCheck in the nested loop executes.  I figured it has something to do w/ me sending it to the end of file, but when I remove that it will execute down all of those other regions as well.  I even tried to put new regions within the loops to send it back to specific areas after execution however it bombs before it even does anything if you put regions within the for's."

            How did you create the inlist.txt   file?   Will  you post a few of the file names that are in the inlist file?

            I tried to run your code but I'm not sure what is in the inlist  file?

            Thanks
            Bill Richardson

            Grimbear13

              Topic Starter


              Rookie

              Re: Searching for files within subdirectories
              « Reply #5 on: February 03, 2010, 12:35:02 PM »
              I'm going to attempt to just give the entire package in a .zip.  I have been working out of the New Folder thats within there just to make sure that I can get it to do the primary task first (reading the list and verifying that the names in the list match the names of the files).

              [Saving space, attachment deleted by admin]

              BillRichardson



                Intermediate

                Thanked: 15
                Re: Searching for files within subdirectories
                « Reply #6 on: February 03, 2010, 03:23:28 PM »
                I'm going to attempt to just give the entire package in a .zip.  I have been working out of the New Folder thats within there just to make sure that I can get it to do the primary task first (reading the list and verifying that the names in the list match the names of the files).

                The zip file worked.  And the batch file ran.
                Of course, only one file  was found on my machine.

                ( I don't understand the need for the color change? But it worked. )

                Input:

                Review>type inlist

                Roof.txt
                Ceiling.txt
                Ground.txt
                Floor.txt
                Noob.txt
                Beginner.txt
                Decent Player.txt

                Output:

                Review>type Results
                ts.txt
                Roof.txt CHECK
                Ceiling.txt MISSING
                Ground.txt MISSING
                Floor.txt MISSING
                Noob.txt MISSING
                Beginner.txt MISSING
                Roof.txt CHECK
                Ceiling.txt MISSING
                Ground.txt MISSING
                Floor.txt MISSING
                Noob.txt MISSING
                Beginner.txt MISSING
                Roof.txt CHECK
                Ceiling.txt MISSING
                Ground.txt MISSING
                Floor.txt MISSING
                Noob.txt MISSING
                Beginner.txt MISSING
                Roof.txt CHECK
                Ceiling.txt MISSING
                Ground.txt MISSING
                Floor.txt MISSING
                Noob.txt MISSING
                Beginner.txt MISSING

                C:\Documents and Settings\Bill Richardson\Desktop\QA Review\QA Review>
                « Last Edit: February 03, 2010, 03:49:09 PM by BillRichardson »
                Bill Richardson

                BillRichardson



                  Intermediate

                  Thanked: 15
                  Re: Searching for files within subdirectories
                  « Reply #7 on: February 03, 2010, 03:43:58 PM »

                  Rem "color 0A"  was REMed out.
                  Rem "@echo off" was allowed to execute to cut down on volume of output

                  Code: [Select]
                  @echo off
                  rem  color 0A

                  for /F "tokens=*" %%I in (inlist) do (
                  Call :ResetBool
                  set filename=%%I

                          for /R %%K in (.) do (
                                  for %%J in (*) do (
                                  set file=%%J
                                  Call :ValidCheck
                                  echo HAMSTERS!
                  )
                                  )
                          Call :Print
                  )
                  GoTo :EOF

                  :ValidCheck
                  if %filename%==%file% (set bool=true)
                  GOTO :EOF

                  :Print
                  if %bool%==true (echo %filename% CHECK >> Results.txt
                  )else (echo %filename% MISSING >> Results.txt )
                  GOTO:EOF

                  :ResetBool
                  set bool=false
                  GoTo :EOF

                  Bill Richardson

                  BillRichardson



                    Intermediate

                    Thanked: 15
                    Re: Searching for files within subdirectories
                    « Reply #8 on: February 03, 2010, 06:15:40 PM »
                    Grim:

                    Your code works.  The only change was to change the main for loop to local variables with "setlocal enabledelayedexpansion"
                    ( I also placed all your files from the inlist in a subfolder called "trial" ). 

                    Code: [Select]
                    @echo off

                    setlocal enabledelayedexpansion
                    for /F "delims=" %%i in (inlist.txt) do (
                    rem Call :ResetBool
                    set filename=%%i
                    echo filename=!filename!


                            for /R %%K in (.) do (
                                    for %%J in (*) do (
                                    set file=%%J
                                    Call :ValidCheck
                                    echo HAMSTERS!
                    )
                                    )
                            Call :Print
                    )
                    GoTo :EOF

                    :ValidCheck
                    if %filename%==%file% (set bool=true)
                    GOTO :EOF

                    :Print
                    if %bool%==true (echo %filename% CHECK >> Results.txt
                    )else (echo %filename% MISSING >> Results.txt )
                    GOTO:EOF

                    :ResetBool
                    set bool=false
                    GoTo :EOF




                    C:\Review\type results.txt
                    Roof.txt CHECK
                    Ceiling.txt CHECK
                    Ground.txt CHECK
                    Floor.txt CHECK
                    Noob.txt CHECK
                    Beginner.txt CHECK
                    DecentPlayer.txt CHECK


                    « Last Edit: February 03, 2010, 06:33:27 PM by BillRichardson »
                    Bill Richardson

                    Grimbear13

                      Topic Starter


                      Rookie

                      Re: Searching for files within subdirectories
                      « Reply #9 on: February 05, 2010, 07:21:20 AM »
                      Okay, when I get some free time at work today hopefully I'll get to play around with it a bit haha.  Yeah I knew the code worked on files in the directory, my issue was getting into subdirectories.  I'll get back to you when I try your solution thanks for all the responses.

                      The color change was because that's my favorite color scheme, and the @echo off was commented out for debugging (as was the random "echo HAMSTERS!" in the one loop there was issues with execution of certain areas so I would put random outputs to make sure they were being executed...that one just got left it  :) )

                      Thanks Again hopefully I'll be able to get back with results.

                      Grimbear13

                        Topic Starter


                        Rookie

                        Re: Searching for files within subdirectories
                        « Reply #10 on: February 15, 2010, 12:53:03 PM »
                        Sorry for the super delayed response, but I just got a chance to try the change you suggested and my code still isn't finding any of the files in the subdirectories.  I tried both my code with your change and your code by itself (which gave a strange error about not being able to find the file at first).  But neither of them correctly reported that the files were there.

                        Also if you were wondering why there were extra files within the lower subdirectories it's because after I get this first step done I'm going to have it check all file names to what's in the list to ensure there are no extra files either.

                        Also it error's out on the Decent Players portion because of the space in the name, can you think of a way to remedy this?

                        Grimbear13

                          Topic Starter


                          Rookie

                          Re: Searching for files within subdirectories
                          « Reply #11 on: February 15, 2010, 12:59:51 PM »
                          Figure I'll post my latest code just to verify.  I broke out the nested loop because I'm trying to get it to terminate once it finds a match to save time.  Else for every object it will loop through the entire package and that will take considerable amounts of time with bigger packages.

                          Code: [Select]
                          @echo off
                          color 0A
                          ::The preceding line turns off screen printing and sets the text color to Bright Green.

                          ::Created by Scott Dean

                          ::Begins to search through the inlist file to get the names of the files.

                          setlocal enabledelayedexpansion
                          for /F "tokens=*" %%I in (inlist) do (
                          ::Calls the ResetBool function
                          Call :ResetBool
                          ::Sets filename equal to the value found from the inlist
                          set filename=%%I

                          ::Calls the two nested loops.  It is broken out this way because batch files do not
                          ::update variables until a process is terminated(not researched found by trial and error.
                          ::Therefore when if the loops are left to run the variables are never updated and incorrect
                          ::output will be given.
                          Call :LoopNest
                          ::Calls the Print function based on the bool variable's value will return MISSING or CHECK
                          Call :Print
                          )
                          GoTo :EOF

                          ::This function checks if the filename given from the inlist matches the current file.
                          :ValidCheck
                          if %filename%==%file% (set bool=true)
                          GOTO :EOF

                          ::This function prints whether the file is missing or not.
                          :Print
                          if %bool%==true (echo %filename% CHECK >> Results.txt
                          )else (echo %filename% MISSING >> Results.txt )
                          GOTO:EOF

                          ::Resets the value of the bool variable to be defaulted to false.
                          :ResetBool
                          set bool=false
                          GoTo :EOF

                          ::Pair of nested for's called from the first for loop.  These go into the subdirectories
                          ::and call the functions ValidCheck and BoolCheck. (May not go into subdirectories as of 02-08-2010)
                          :LoopNest
                          for /R %%K in (.) do (
                          for %%J in (*) do (
                          set file=%%J
                          Call :ValidCheck
                          Call :BoolCheck
                          if %bool%==true (GOTO :EOF)
                          )
                          if %bool%==true (GOTO :EOF)
                          )
                          GOTO :EOF

                          ::An attempt to break the loop cycle if the correct value is found, doesn't seem to work.
                          :BoolCheck
                          if %bool%==true (GOTO :EOF)

                          BC_Programmer


                            Mastermind
                          • Typing is no substitute for thinking.
                          • Thanked: 1140
                            • Yes
                            • Yes
                            • BC-Programming.com
                          • Certifications: List
                          • Computer: Specs
                          • Experience: Beginner
                          • OS: Windows 11
                          Re: Searching for files within subdirectories
                          « Reply #12 on: February 15, 2010, 10:14:53 PM »
                          when you have loops it's not really a good idea to use two colons to indicate a comment.
                          I was trying to dereference Null Pointers before it was cool.

                          Grimbear13

                            Topic Starter


                            Rookie

                            Re: Searching for files within subdirectories
                            « Reply #13 on: February 16, 2010, 09:09:44 AM »
                            By loops are you referring to the regions or within the loops?  I don't know standards for batches and I just find it easier to type :: rather than rem.  Also coming from a C (primarily C#) background I'm more comofortable with the :: beacuse its similar to the // comment.  I think the constant spam of rem in the code makes it harder to read.

                            BC_Programmer


                              Mastermind
                            • Typing is no substitute for thinking.
                            • Thanked: 1140
                              • Yes
                              • Yes
                              • BC-Programming.com
                            • Certifications: List
                            • Computer: Specs
                            • Experience: Beginner
                            • OS: Windows 11
                            Re: Searching for files within subdirectories
                            « Reply #14 on: February 16, 2010, 09:41:57 AM »
                            :: within the body of the loop can cause problems, since : is really a label delimiter.

                            the only "legitimate" comment is REM, and really that's just an internal command that does nothing.
                            I was trying to dereference Null Pointers before it was cool.