Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

batch-file

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 doesn't seem to recursively loop.

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.

like image 435
user320550 Avatar asked Dec 10 '22 16:12

user320550


1 Answers

Really you should use Ant or Maven to run JUnit tests, or do it the pure JUnit way and organize all of your tests into Suites and run the suite via the JUnit command line, but in order to iterate all files in a batch file, just use the for command like this:

for /R %%i in (*.class) do echo "%%i"

Just replace the echo with a call that does something you need. Note you need to quote the variable in case there is a space in the path name.

like image 171
Yishai Avatar answered May 28 '23 14:05

Yishai