Page 1 of 1

Create a batch to loop through files, run command & move

Posted: 03 Feb 2010 14:32
by Threep
Hello, I'm trying to create what should be a simple batch file, but I'm having a really hard time, any help would be appreciated.

Basically what I want the batch file to do is loop through all .mib files in a directory, then run a command "mcompile" on each of the files. If mcompile passes then it needs to run another command on it mxmib - a on the cfg file that the mcompile command will make. However, if mcompile fails, I want to move the mib file that caused the failure to another folder.

Here's what I've tried:

Code: Select all

@echo off

for /f %%a IN ('dir /b netapp*.mib') do (

call echo %%a
rem whoever write mcompile didnt return an error code, god damn
call Mcompile %%a
echo %ERRORLEVEL%
rem I've found out that mcompile seems to always return 0, regardless of if it produced an error or not, this is the problem

rem this takes the original name and makes it a .cfg instead of .mib
set source=%%a
echo %source%
set cfgName=%source:~0,-4%.cfg
echo %cfgName%

rem here is where I would do the next part, but haven't since I can't get the part to move error files working

)



So I'm not sure how I can tell if the file failed to compile in the loop. on standard output it displays a message that has the string "error" in it, but I can't figure out how to redirect the output the call command makes into a variable, which maybe I could then use to search for the string "error" within it.

Does anyone have any ideas of what I could try?

Re: Create a batch to loop through files, run command & move

Posted: 03 Feb 2010 15:12
by aGerman
Threep

Your entire Batch is just one FOR loop written as a block. For your command line interpreter it looks like a single command line. Note that the expansion of variables occurs only one time during the run time of a command line. Thats why you can't see any changes of the %errorlevel% variable. Also for %source% and %cfgName% you should not see any changes.

IMHO for your example it could be better to call a sub-routine for each found file. Use %%a as parameter and you will find it as %1.
Figure out.

Code: Select all

@echo off
for /f %%a IN ('dir /b netapp*.mib') do call :sub "%%a"
pause
goto :eof

:sub
echo %~1

call Mcompile %1
echo %ERRORLEVEL%
rem I've found out that mcompile seems to always return 0, regardless of if it produced an error or not, this is the problem

rem this takes the original name and makes it a .cfg instead of .mib
set source=%~1
echo %source%
set cfgName=%source:~0,-4%.cfg
echo %cfgName%

rem here is where I would do the next part, but haven't since I can't get the part to move error files working

goto :eof


Regards
aGerman

Re: Create a batch to loop through files, run command & move

Posted: 03 Feb 2010 16:08
by avery_larry
This might get you going for finding the error and continuing on:

Code: Select all

@echo off

for /f %%a IN ('dir /b netapp*.mib') do call :process %%a

rem rest of your code can go here.
goto :eof

:process
echo %1
call Mcompile %1 | find /i "error" && move %1 "\failure\folder" && goto :eof

rem this takes the original name and makes it a .cfg instead of .mib
set source=%1
echo %source%
set cfgName=%source:~0,-4%.cfg
echo %cfgName%

rem Continue your code here.
goto :eof


Re: Create a batch to loop through files, run command & move

Posted: 04 Feb 2010 07:39
by Threep
Thanks, I got it working. The problem was that I didn't realize that the for loop was being run as one line, so the errorlevel wasn't set properly as a result because I checked it before it got a chance to get set. Using the subs fixed it.