【问题标题】:Process hangs when checking if files are present across domain PCs with batch file使用批处理文件检查域 PC 中是否存在文件时进程挂起
【发布时间】:2015-04-08 18:07:14
【问题描述】:

在批处理文件中遇到以下问题...

问题:我创建了一个批处理脚本来检查文件夹位置中是否存在文件,如果存在文件,它会将 PC 名称(标记为资产)写入日志文件。但是,在扫描 99 台 PC 后,它会挂起大约 20 秒,然后再扫描几台并挂起。我需要想办法让它顺利运行。

性能表明处理器确实达到峰值。我试过从另一个低优先级的批处理文件中调用它,结果相同。

所有 PC 都命名为“ABC”,后跟 4 个数字。

下面是完整的代码,因为我不知道焦点应该在哪里。如果有更合适的编辑,我很乐意提供帮助。

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

【问题讨论】:

    标签: batch-file cmd


    【解决方案1】:

    中的逻辑

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

    无法检测到离线机器。对于任何等于或大于 n 的错误级别值,if errorlevel n 构造将被评估为真,因此,对于任何非负错误级别,if errorlevel 0 将被评估为真。

    你可以尝试任何一个

    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 )
    

    但是,根据源和目标机器的 ip 版本和子网,测试 ping 错误级别并不是测试目标机器是否在线的安全方法(阅读here

    【讨论】:

    • 我现在是移动的,但一旦我回来我会回顾一下。感谢您的回复
    • 有没有办法清除迭代过程中缓存的内存?我想释放内存添加循环的结尾稍微慢一点,但不会导致挂起。
    • @DaveyLions,当您直接对共享使用dir 命令时,会创建连接并自动断开连接。不需要自己释放,但是如果要显式释放,需要先使用net use \\%asset%.domain.com命令连接服务器,完成后使用net use \\%asset%.domain.com /delete /y
    • 谢谢你的解释,我会做出改变,看看行为是否有差异。它非常快速地扫描前 100 个,然后似乎与处理器挂钩并挂起,但随后继续并重复该行为。我会看看这种方法是否能提高性能。
    • 谢谢 MC_ND。修改 ping 脚本减少了系统的负载,我能够在大约 20-30 秒内扫描 1000 个资产,而没有非常小的停顿。一切都很好。
    猜你喜欢
    • 2013-04-27
    • 2011-05-25
    • 2015-07-09
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    相关资源
    最近更新 更多