Use .bat file to recursively loop through folders and get hold of .class files

Posted by user320550 on Stack Overflow See other posts from Stack Overflow or by user320550
Published on 2010-04-19T16:50:40Z Indexed on 2010/04/19 16:53 UTC
Read the original article Hit count: 278

Filed under:

HI all,

This is what i'm trying to do. I have a .bat file which takes in a argument which is nothing but a folder name. What i do first is go one level up (cd ..). Now in this directory i have 3 folders and each folders have sub-folders and have .class files in them.

What i want to do is recursively loop through the folders and get hold of the .class files. Once this is done i want to echo the target folder of the .class file as well as echo the name of the.class file.

So c:\temp\potter\myclass.class. I would echo out c:\temp\potter\ and myclass.

I'm able to do this by writing a separate bat file which works. But when i integrate this with the recursive function it seems to break.

This is what i'm doing:

:: call the junit classes... and save the results

echo step 3...

cd %1

cd ..

for /r %%a in (*.class) do set Var=%%a

echo Full file location %Var%

for %%i in ("%Var%") do Set CF=%%~dpi 

Set CF=%CF:~0,-1%

:LOOP

If "%CF:~-1,1%"=="\" GoTo :DONE

Set CF=%CF:~0,-1%

GoTo :LOOP

:DONE

Set CF=%CF:~0,-1%

echo Folder Location %CF%

::cd %CF%

For %%j in ("%Var%") Do Set name=%%~nxj

:: -6 because of Quotations

Set name=%name:~0,-6%

echo File Name %name%

echo step 3 complete...

However i only get the output of one directory, while i have multiple directories having .class files.

This is the output i get:

step 3...

Full file location C:\NKCV\Project\MyActivities\6_Selenium\htmlTestCasesConve rted2JUnit\iexplore\flow2\testCase_app2.class

Folder Location C:\NKCV\Project\MyActivities\6_Selenium\htmlTestCasesConverte d2JUnit\iexplore\flow2

File Name testCase_app2

step 3 complete...

missing argument!

usage htmltestCaseLocation

for eg., "C:\NKCV\Project\MyActivities\6_Selenium\htmlTestCases"

Could anyone please let me know whats wrong here?

Thanks.

© Stack Overflow or respective owner

Related posts about batch-file