1.

Solve : Searching for files within subdirectories?

Answer» <html><body><p>Sorry, this probably has been answered before but,<br/><br/>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.<br/><br/>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.<br/><br/>If someone could <a href="https://interviewquestions.tuteehub.com/tag/show-236642" style="font-weight:bold;" target="_blank" title="Click to know more about SHOW">SHOW</a> me the command for subdirectory searching or an example of it I could probably figure out how to make both of these processes work.<br/><br/>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 <a href="https://interviewquestions.tuteehub.com/tag/name-237998" style="font-weight:bold;" target="_blank" title="Click to know more about NAME">NAME</a>.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:<br/><br/> Code: <a>[Select]</a>for /F "tokens=*" %%I in (inlist) do (<br/><br/> for /R "tokens=*" %%J in (*) do ( <br/> if %%I==%%J (echo %%I CHECK &gt;&gt; Results.txt)<br/> )<br/> <br/> echo %%I MISSING &gt;&gt; Results.txt<br/>)C:\&gt;dir /s /b *.pdf  &gt;  pdffiles.txt<br/><br/>C:\&gt;<br/><br/><br/>C:\&gt;dir /s *.pdf<br/> <br/> Volume Serial Number is F4A3-D6B3<br/><br/> Directory of C:\bin\doc\gawk\3.1.6\gawk-3.1.6<br/><br/>12/29/2007  02:48 AM         2,058,611 gawk.pdf<br/>               1 File(s)      2,058,611 bytes<br/><br/> Directory of C:\bin\doc\gawk\UnixV7<br/><br/>07/29/2007  07:07 AM            28,217 awk_V7.1.pdf<br/>07/29/2007  05:41 AM            60,796 awk_V7.pdf<br/>               2 File(s)         89,013 bytes<br/><br/> Directory of C:\bin\man\pdf<br/><br/>12/28/2007  03:45 PM           315,094 gawk-man.pdf<br/>               1 File(s)        315,094 bytes<br/><br/> Directory of C:\DELL\Docs\manual<br/><br/>09/10/2007  03:44 PM        13,980,991 eom.pdf<br/>               1 File(s)     13,980,991 bytes<br/>.<br/>.<br/>.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.<br/><br/>This is the full code that I've worked out thus far<br/><br/> Code: <a>[Select]</a>::echo off &gt; debug.txt<br/>color 0A<br/>::The preceding line turns off screen printing and sets the text color to Bright Green.<br/><br/>::Created by Scott Dean<br/><br/>for /F "tokens=*" %%I in (inlist) do (<br/>Call :ResetBool<br/>set filename=%%I<br/><br/> for %%J in (*) do (<br/> set file=%%J<br/> Call :ValidCheck<br/> )<br/> )<br/> Call :Print<br/> Call :MissingCheck<br/>pause<br/>)<br/>GoTo :EOF<br/><br/>:ValidCheck<br/>if %filename%==%file% (Goto :CheckPrint)<br/>GoTo :EOF<br/><br/>:MissingCheck<br/>if %bool%==false (GoTo :MissingPrint)<br/>GoTo :EOF<br/><br/>:CheckPrint<br/>echo %filename% CHECK &gt;&gt; Results.txt<br/>set bool=true<br/>GoTo :EOF<br/><br/>:MissingPrint<br/>echo %filename% MISSING &gt;&gt; Results.txt<br/>GoTo :EOF<br/><br/>:ResetBool<br/>set bool=false<br/>GoTo :EOF<br/><br/>:Print<br/>echo %filename% &gt;&gt; lol.txt<br/>echo %bool% &gt;&gt; rofl.txt<br/>GoTo :EOF<br/>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.<br/><br/>Thanks, Quote from: Grimbear13 on February 03, 2010, 11:26:23 AM</p><blockquote><br/>"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."<br/></blockquote> <br/>How did you create the inlist.txt   file?   Will  you post a few of the file names that are in the inlist file?<br/><br/>I tried to run your code but I'm not sure what is in the inlist  file?<br/><br/>ThanksI'm going to attempt to just give the entire package in a .zip.  I have been <a href="https://interviewquestions.tuteehub.com/tag/working-243555" style="font-weight:bold;" target="_blank" title="Click to know more about WORKING">WORKING</a> 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).<br/><br/>[Saving space, attachment deleted by admin] Quote from: Grimbear13 on February 03, 2010, 12:35:02 PM<blockquote>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).<br/></blockquote> <br/>The zip file worked.  And the batch file ran.<br/>Of course, only one file  was found on my machine.<br/><br/>( I don't understand the need for the color change? But it worked. )<br/><br/><strong>Input:</strong><br/><br/>Review&gt;type inlist<br/><br/>Roof.txt<br/>Ceiling.txt<br/>Ground.txt<br/>Floor.txt<br/>Noob.txt<br/>Beginner.txt<br/>Decent Player.txt<br/><br/><strong>Output:</strong><br/><br/>Review&gt;type Results<br/>ts.txt<br/>Roof.txt CHECK<br/>Ceiling.txt MISSING<br/>Ground.txt MISSING<br/>Floor.txt MISSING<br/>Noob.txt MISSING<br/>Beginner.txt MISSING<br/>Roof.txt CHECK<br/>Ceiling.txt MISSING<br/>Ground.txt MISSING<br/>Floor.txt MISSING<br/>Noob.txt MISSING<br/>Beginner.txt MISSING<br/>Roof.txt CHECK<br/>Ceiling.txt MISSING<br/>Ground.txt MISSING<br/>Floor.txt MISSING<br/>Noob.txt MISSING<br/>Beginner.txt MISSING<br/>Roof.txt CHECK<br/>Ceiling.txt MISSING<br/>Ground.txt MISSING<br/>Floor.txt MISSING<br/>Noob.txt MISSING<br/>Beginner.txt MISSING<br/><br/>C:\Documents and Settings\Bill Richardson\Desktop\QA Review\QA Review&gt;<br/>Rem "color 0A"  was REMed out.<br/>Rem "echo off" was allowed to execute to cut down on volume of output<br/><br/> Code: <a>[Select]</a>echo off<br/>rem  color 0A<br/><br/>for /F "tokens=*" %%I in (inlist) do (<br/>Call :ResetBool<br/>set filename=%%I<br/><br/>        for /R %%K in (.) do (<br/>                for %%J in (*) do (<br/>                set file=%%J<br/>                Call :ValidCheck<br/>                echo HAMSTERS!<br/>)<br/>                )<br/>        Call :Print<br/>)<br/>GoTo :EOF<br/><br/>:ValidCheck<br/>if %filename%==%file% (set bool=true)<br/>GOTO :EOF<br/><br/>:Print<br/>if %bool%==true (echo %filename% CHECK &gt;&gt; Results.txt<br/>)else (echo %filename% MISSING &gt;&gt; Results.txt )<br/>GOTO:EOF<br/><br/>:ResetBool<br/>set bool=false<br/>GoTo :EOF<br/>Grim:<br/><br/>Your code works.  The only change was to change the main for loop to local variables with "setlocal enabledelayedexpansion"<br/>( I also placed all your files from the inlist in a subfolder called "trial" ).  <br/><br/> Code: <a>[Select]</a>echo off<br/><br/>setlocal enabledelayedexpansion<br/>for /F "delims=" %%i in (inlist.txt) do (<br/>rem Call :ResetBool<br/>set filename=%%i<br/>echo filename=!filename!<br/><br/><br/>        for /R %%K in (.) do (<br/>                for %%J in (*) do (<br/>                set file=%%J<br/>                Call :ValidCheck<br/>                echo HAMSTERS!<br/>)<br/>                )<br/>        Call :Print<br/>)<br/>GoTo :EOF<br/><br/>:ValidCheck<br/>if %filename%==%file% (set bool=true)<br/>GOTO :EOF<br/><br/>:Print<br/>if %bool%==true (echo %filename% CHECK &gt;&gt; Results.txt<br/>)else (echo %filename% MISSING &gt;&gt; Results.txt )<br/>GOTO:EOF<br/><br/>:ResetBool<br/>set bool=false<br/>GoTo :EOF<br/><br/><br/><br/>C:\Review\type results.txt<br/>Roof.txt CHECK<br/>Ceiling.txt CHECK<br/>Ground.txt CHECK<br/>Floor.txt CHECK<br/>Noob.txt CHECK<br/>Beginner.txt CHECK<br/>DecentPlayer.txt CHECK<br/><br/><br/>Okay, when I get some free time at work today hopefully I'll get to play around with it a bit haha.  <a href="https://interviewquestions.tuteehub.com/tag/yeah-2343023" style="font-weight:bold;" target="_blank" title="Click to know more about YEAH">YEAH</a> 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.<br/><br/>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  )<br/><br/>Thanks Again hopefully I'll be able to get back with results.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.<br/><br/>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.<br/><br/>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?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.<br/><br/> Code: <a>[Select]</a>echo off<br/>color 0A<br/>::The preceding line turns off screen printing and sets the text color to Bright Green.<br/><br/>::Created by Scott Dean<br/><br/>::Begins to search through the inlist file to get the names of the files.<br/><br/>setlocal enabledelayedexpansion<br/>for /F "tokens=*" %%I in (inlist) do (<br/>::Calls the ResetBool function<br/>Call :ResetBool<br/>::Sets filename equal to the value found from the inlist<br/>set filename=%%I<br/><br/>::Calls the two nested loops.  It is broken out this way because batch files do not<br/>::update variables until a process is terminated(not researched found by trial and error.<br/>::Therefore when if the loops are left to run the variables are never updated and incorrect<br/>::output will be given.<br/> Call :LoopNest<br/>::Calls the Print function based on the bool variable's value will return MISSING or CHECK<br/> Call :Print<br/>)<br/>GoTo :EOF<br/><br/>::This function checks if the filename given from the inlist matches the <a href="https://interviewquestions.tuteehub.com/tag/current-240611" style="font-weight:bold;" target="_blank" title="Click to know more about CURRENT">CURRENT</a> file.<br/>:ValidCheck<br/>if %filename%==%file% (set bool=true)<br/>GOTO :EOF<br/><br/>::This function prints whether the file is missing or not.<br/>:Print<br/>if %bool%==true (echo %filename% CHECK &gt;&gt; Results.txt<br/>)else (echo %filename% MISSING &gt;&gt; Results.txt )<br/>GOTO:EOF<br/><br/>::Resets the value of the bool variable to be defaulted to false.<br/>:ResetBool<br/>set bool=false<br/>GoTo :EOF<br/><br/>::Pair of nested for's called from the first for loop.  These go into the subdirectories<br/>::and call the functions ValidCheck and BoolCheck. (May not go into subdirectories as of 02-08-2010)<br/>:LoopNest<br/>for /R %%K in (.) do (<br/> for %%J in (*) do (<br/> set file=%%J<br/> Call :ValidCheck<br/> Call :BoolCheck<br/> if %bool%==true (GOTO :EOF)<br/> )<br/> if %bool%==true (GOTO :EOF)<br/>)<br/>GOTO :EOF<br/><br/>::An attempt to break the loop cycle if the correct value is found, doesn't seem to work.<br/>:BoolCheck<br/>if %bool%==true (GOTO :EOF)when you have loops it's not really a good idea to use two colons to indicate a comment.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.:: within the body of the loop can cause problems, since : is really a label delimiter.<br/><br/>the only "legitimate" comment is REM, and really that's just an internal command that does nothing.</body></html>


Discussion

No Comment Found