LABEL Being Processed without GOTO or CALL
Posted: 22 Jun 2018 17:50
I have small batch that will try to map a migration folder on remote computers. My :ERRORMAPDRIVE label is being processed even it was not called or told to goto. I tested with 1 valid IP, it was successfully mapped, but the batch somehow still process the label making my error log is inaccurate.
Can someone tell me what is wrong with my code? The label is not supposed to be processed as a normal batch unless I specify goto in the first if statement, correct?
Update:
Can someone tell me what is wrong with my code? The label is not supposed to be processed as a normal batch unless I specify goto in the first if statement, correct?
Update:
Full batch uploaded, basically i want the batch to:
1. Map E:\Migration on a remote machine, if there's no migration in E:\, try mapping D:\Migration.
2. Check if files exist
3. Direct file check messages to Log.txt and error messages to Error.txt
Problem:
1. When both map drive attempt fails, batch continues to check if files exist
2. When goto is used, the batch logs the error message in error.txt even when the first map drive is successful.
Code: Select all
REM Start Loop to read IPs
:START
for /F "tokens=1" %%B in (.\IPList.txt) do call :MAPDRIVE %%B
REM End Loop to read IPs
REM Start Complete Task
echo.
REM net use %Drive% /del 2>nul
echo File Check Complete!!
echo %date:~-10%%time:~0,8%:*** FileCheck Complete!! >> Logs.txt
goto :eof
REM End Complete Task
REM Start Delete if Exist
:MAPDRIVE
if exist %drive% (
echo.
echo %drive% drive exist on this computer, deleting %drive% drive
echo.
net use %drive% /del
)
REM End Delete if Exist
REM Start On Screen Display Message
echo *** Mapping %RDriveE%: drive on remote machine %1 ***
echo %date:~-10%%time:~0,8%:*** Mapping %RDriveE% drive on remote machine %1 *** >> Logs.txt
REM End On Screen Display Message
REM Start MAP Network Drive
net use L: \\%1\%RDriveE%$\%MG% /u:%1\%username% %pwd% >nul 2>&1
if errorlevel 1 (
GOTO RETRY
) else (
echo Successfully Mapped Drive %RDriveE%: On %1
)
:RETRY
echo.
echo %MG% folder doesn't exist in drive %RDriveE%: of the remote machine
echo Finding %MG% folder in the drive %RDdriveD%: instead...
echo.
net use L: \\%1\%RDdriveD%$\%MG% /u:%1\%username% %pwd% >nul 2>&1
if errorlevel 1 (
GOTO ERRORMAPDRIVE
) Else (
echo Successfully Mapped Drive %RDdriveD% On %1
)
REM End of Map Network Drive
REM For Loop
echo *** Checking files on %1 ***
echo *** Checking files on %1 *** >> Logs.txt
echo.
echo. >> Logs.txt
REM Start Check Migration Folder
for %%A in ("%gho%" "%GSha%" "%PrepPCI%" "%FileCopy%") Do (
timeout /t 1 >nul
if exist "%Drive%\%%~A" (
echo %%~A exists in the %MG% Folder
echo %date:~-10%%time:~0,8%:%%~A exists in the %MG% Folder >> Logs.txt
) else (
echo %%~A does not exists in the %MG% Folder
echo %date:~-10%%time:~0,8%:%%~A does not exists in the %MG% Folder >> Logs.txt
)
)
REM End Check Migration Folder
REM Start Check PCI Folder
for %%A in ("%IM%" "%ES%" "%PAS%") Do (
timeout /t 1 >nul
if exist "%Drive%\%PCI%\%%~A" (
echo %%~A exists in the %PCI% Folder
echo %date:~-10%%time:~0,8%:%%~A exists in the %PCI% Folder >> Logs.txt
) else (
echo %%~A does not exists in the %PCI% Folder
echo %date:~-10%%time:~0,8%:%%~A does not exists in the %PCI% Folder >> Logs.txt
)
)
REM End Check PCI Folder
REM Start Check POST Folder
for %%A in ("%NLM%" "%RSS%" "%Clean%" "%PCIS%" "%ESET%") Do (
timeout /t 1 >nul
if exist "%Drive%\%POS%\%%~A" (
echo %%~A exists in the %POS% Folder
echo %date:~-10%%time:~0,8%:%%~A exists in the %POS% Folder >> Logs.txt
) else (
echo %%~A does not exists in the %POS% Folder
echo %date:~-10%%time:~0,8%:%%~A does not exists in the %POS% Folder >> Logs.txt
)
)
REM End Check POST Folder
REM Start Check PRE Folder
for %%A in ("%DualServer%" "%Proxy%" "%SHA2%") Do (
timeout /t 1 >nul
if exist "%Drive%\%PRE%\%%~A" (
echo %%~A exists in the %PRE% Folder
echo %date:~-10%%time:~0,8%:%%~A exists in the %PRE% Folder >> Logs.txt
) else (
echo %%~A does not exists in the %PRE% Folder
echo %date:~-10%%time:~0,8%:%%~A does not exists in the %PRE% Folder >> Logs.txt
)
)
REM End Check PRE Folder
REM Start ERRORMAPDRIVE
:ERRORMAPDRIVE
echo Error mapping drive on %1
echo %date:~-10% %time:~0,8%:Error mapping drive on %1! Possible firewall issue. Check station status in RSS and deploy firewall fix. >> Error.txt
REM End ERRORMAPDRIVE