Page 1 of 1

Help with batch file

Posted: 27 Aug 2013 08:00
by Briandr
Hi,

I am trying to piece together two batch files. The more I look at this the more I get the impression something is not quite right. I am trying to avoid redundant code. I have three sections in the batch file. Cleanup, filemove1 and filemove2. Filemove1 and filemove2 should be activated based upon the system type. The cleanup section should only run once. Can someone tell me how this is wrong and how it should be? Thanks.

If [%ERRORLEVEL%] == [0] (
echo X86
cd %systemroot%\system32
If exist "CleanupCompleted.txt" exit
GOTO CLEANUP
) ELSE (
echo AMD64
cd %systemroot%\sysnative
If exist "CleanupCompleted.txt" exit
GOTO CLEANUP

)

:CLEANUP
REM Get the Altiris Agent install path
FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKLM\Software\Altiris\Altiris Agent" /V "installdir"') DO SET AgentDir=%%B set tempbat=%temp%\AgentClean.bat"
REM Create temporary batch file to execute while the agent restarts echo "%AgentDir%\aexagentutil" /stop > %tempbat% echo rmdir "%AgentDir%\TaskManagement\cache" /s /q >> %tempbat% echo rmdir "%AgentDir%\TaskManagement\status" /s /q >> %tempbat% echo rmdir "%AgentDir%\TaskManagement\statusXml" /s /q >> %tempbat%
REM echo rmdir "%AgentDir%\TaskManagement\lti" /s /q >> %tempbat%
echo ping localhost -n 30 >> %tempbat%
echo "%AgentDir%\aexagentutil" /start >> %tempbat% echo exit >> %tempbat% REM Executes temporary batch file start "" /MIN %tempbat%
Set RegVar=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegVar% 2>NUL | find /I /N "x86">NUL

:FILEMOVE1
COPY /Y "%~dp0\CleanupCompleted.txt" "%systemroot%\system32\CleanupCompleted.txt"
exit

:FILEMOVE2
%systemroot%\sysnative\cmd.exe /c copy /y "%~dp0\CleanupCompleted.txt" "%systemroot%\system32\CleanupCompleted.txt"
exit

Re: Help with batch file

Posted: 27 Aug 2013 08:53
by penpen
I'm not sure but maybe you're trying to do this:

Code: Select all

setlocal
:: ??? How do you initialize the ERRORLEVEL variable?
If [%ERRORLEVEL%] == [0] (
   echo X86
   cd %systemroot%\system32
   If exist "CleanupCompleted.txt" exit
   call :CLEANUP
   call :FILEMOVE1
) ELSE (
   echo AMD64
   cd %systemroot%\sysnative
   If exist "CleanupCompleted.txt" exit
   call :CLEANUP
   call :FILEMOVE2
)
goto :eof


:CLEANUP
REM Get the Altiris Agent install path
FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKLM\Software\Altiris\Altiris Agent" /V "installdir"') DO SET AgentDir=%%B
set tempbat="%temp%\AgentClean.bat"

REM Create temporary batch file to execute while the agent restarts
(
   echo "%AgentDir%\aexagentutil" /stop
   echo rmdir "%AgentDir%\TaskManagement\cache" /s /q
   echo rmdir "%AgentDir%\TaskManagement\status" /s /q
   echo rmdir "%AgentDir%\TaskManagement\statusXml" /s /q
   REM echo rmdir "%AgentDir%\TaskManagement\lti" /s /q
   echo ping localhost -n 30
   echo "%AgentDir%\aexagentutil" /start
   echo exit /b
) > %tempbat%

REM Executes temporary batch file
start "" /MIN %tempbat%

Set RegVar=HKLM\Hardware\Description\System\CentralProcessor\0
:: ??? What is the use of the next line?
REG.exe Query %RegVar% 2>NUL | find /I /N "x86">NUL

:: maybe you want to delete the temporary batch file, if yes remove the ::'s from the next line
:: del %tempbat%
exit /b


:FILEMOVE1
COPY /Y "%~dp0\CleanupCompleted.txt" "%systemroot%\system32\CleanupCompleted.txt"
exit /b

:FILEMOVE2
%systemroot%\sysnative\cmd.exe /c copy /y "%~dp0\CleanupCompleted.txt" "%systemroot%\system32\CleanupCompleted.txt"
exit /b

penpen

Re: Help with batch file

Posted: 28 Aug 2013 17:17
by Jess Selien
Strategicly placed pause commands can help you find when and where the code is at a certain time to catch where an extra loop is occurring. And to validate what a variable contains at a specific time in the script.

"how it should be?"
Do you have a specific question to an error message or output data?