0

Having an issue with the following issue with a batch file...

Problem: I have created a batch script to check if files are present in a folder location and if there are files present it will write the PC name (labeled asset) to a log file. However, after scanning 99 PCs it hangs for a about 20 seconds and then scans a few more and hangs. I need to figure out a way to have this run smoothly.

Performance shows that the processor does spike. I've tried calling it from another batch file with a low priority with the same results.

All PCs are named "ABC" followed by 4 numbers.

Full code is below because I do not know where the focus should be. If there is a more appropriate edit I would be happy to oblige.

cls
@echo off
echo Set range of asset tags to scan:

:min
echo:
set /p min="Enter lowest asset number to scan (number only): "
if not defined min goto :min
if %min% lss 1000 (
msg * Asset must be 4 digits
goto :min
)

if %min% gtr 5000 (
msg * Min asset out of range
goto :min
)

:max
echo:
set /p max="Enter highest asset number to scan (number only): "
if not defined max goto :max

if %max% gtr 5000 (
msg * Max asset out of range
goto :max
)

if %max% lss %min% (
msg * Max cannot be lower than min
goto :max
)

set /a max=%max%+1

@echo off
REM Count Logic
set count=%min%

REM sets date/time log stamp to consistent value. If done per line, the         seconds change will create multiple files.
set name=Scan_Results_%DATE:~-4%%DATE:~4,2%%DATE:~7,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6 ,2%%TIME:~10,2%

REM creates a date and time stamped log file from location batch file is run
echo Dictation Scan from %Date% %Time% > %name%.txt
@echo off


:loop
@echo off
set /a count=%count%
echo Asset tag being scanned: %count%
@echo off

REM "ABC" to count as string to inject in filepath
set asset=abc%count%

ping -n 1 %asset%.domain.com > NUL
IF ERRORLEVEL 0 (goto :scan) ELSE goto :No

:scan
REM IF LOGIC (If files are detected DO X or else Y [loop until end])
@echo off
for /F %%i in ('dir /b "\\%asset%.domain.com\C$\Program Files\Speech     Machines\SubSpace\temp\*.*"') do (

goto :Yes
)
goto :No

:Yes
echo writing asset to log file
echo %asset% >> %name%.txt
set /a count=%count%+1
goto :iteration

:No
set /a count=%count%+1
goto :iteration

:iteration
REM This is the highest asset tag number that you want to scan
if not #%count%#==#%max%# goto loop
goto final

:final
echo %found% >> %name%.txt
@echo SCANNING COMPLETE.
Pause
exit
DaveyLions
  • 73
  • 1
  • 10

1 Answers1

1

The logic in

ping -n 1 %asset%.domain.com > NUL
IF ERRORLEVEL 0 (goto :scan) ELSE goto :No

fails to detect offline machines. The if errorlevel n construct will be evaluated as true for any errorlevel value equal or greater than n, so, if errorlevel 0 will be true for any non negative errorlevel.

You can try with any of

ping -n 1 %asset%.domain.com > NUL
if errorlevel 1 ( goto :no ) else ( goto :scan )

ping -n 1 %asset%.domain.com > NUL
if not errorlevel 1 ( goto :scan ) else ( goto :no )

ping -n 1 %asset%.domain.com > NUL && goto :scan || goto :no

ping -n 1 %asset%.domain.com > NUL
if %errorlevel% equ 0 ( goto :scan ) else ( goto :no )

BUT, depending on the ip version and subnet of source and target machines, testing the ping errorlevel is not a secure way to test if the target machine is online (read here)

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I am mobile at the moment but I will review that once I get back. Thank you for the response – DaveyLions Apr 08 '15 at 19:26
  • Is there a way to clear the memory that's cached during the iteration? I feel like releasing the memory add the end of the loop with slow it slightly but won't cause the hang. – DaveyLions Apr 08 '15 at 19:28
  • @DaveyLions, as you are directly using a `dir` command against the share, the connection is created and automatically dropped. There is no need to release it yourself, but if you want to explicitly release it, you need to first use a `net use \\%asset%.domain.com` command to connect to the server and when finished a `net use \\%asset%.domain.com /delete /y`. – MC ND Apr 09 '15 at 06:36
  • Thank you for the explanation, I will make that change and see if there is a difference in behavior. It scans the first 100 very fast and then seems to peg the processor and hang for a but then continues and repeats the behavior. I'll see if this method improves performance. – DaveyLions Apr 09 '15 at 12:08
  • Thank you MC_ND. Modifying the ping script reduced the load on the system and I was able to scan 1000 assets in about 20-30 seconds without very minor pauses. All is well. – DaveyLions Apr 13 '15 at 18:49