| 1. |
Solve : Searching for files within subdirectories? |
|
Answer» Sorry, this probably has been answered before but,
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? ThanksI'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] Quote from: Grimbear13 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). 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> 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 if %bool%==true (echo %filename% CHECK >> Results.txt )else (echo %filename% MISSING >> Results.txt ) GOTO:EOF :ResetBool set bool=false GoTo :EOF 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 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 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.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?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. 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)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. the only "legitimate" comment is REM, and really that's just an internal command that does nothing. |
|