Board logo

标题: [文件操作] 批处理运行越久越慢怎么办? [打印本页]

作者: 857185187    时间: 2020-10-13 18:07     标题: 批处理运行越久越慢怎么办?

开始运行的时候速度还可以,随着运行时间越久就越慢
看任务管理器显示,内存占用一直在稳定增加,好像CMD不会自动释放内存
有办法解决么?
作者: Batcher    时间: 2020-10-13 23:37

回复 1# 857185187


    请把完整代码发出来看下如何优化
作者: peterboy666    时间: 2020-10-14 02:27

以前遇过读取文件
1 到 N 列 越读越慢
最后写小程序解决
作者: 857185187    时间: 2020-10-14 18:33

回复 2# Batcher
  1. @echo off
  2. Setlocal EnableDelayedExpansion
  3. set "LOGFILE=%~dpn0.LOG"
  4. Echo !date:~0,10! !time! --START CreateSHA1>>"!LOGFILE!"
  5. certutil /? 1>nul 2>nul
  6. if errorlevel 1 goto :exit
  7. wmic /? 1>nul 2>nul
  8. if errorlevel 1 goto :exit
  9. For /r "D:\" %%a in (*.*) do (
  10. set fail=1
  11. echo %%a
  12. set "currentfilename=%%a"
  13. set "SIZE=%%~za"
  14. set "NXNAME=%%~nxa"
  15. Call :Process
  16. )
  17. Echo !date:~0,10! !time! --END CreateSHA1>>"!LOGFILE!"
  18. exit /b
  19. :Process
  20. Call :GetSHA1
  21. if fail==0 exit /b
  22. Call :GetVersion
  23. Call :CreateFile
  24. exit /b
  25. :GetSHA1
  26. set SHA1=
  27. set "InFile=!currentfilename!"
  28. For /F "skip=1 delims=" %%b in ('certutil -hashfile "!InFile!" SHA1') do (
  29.   set "SHA1=%%b"
  30.   if "!SHA1:~0,8!"=="CertUtil" Call :OutputLog GetSHA1 1 "!SHA1:CertUtil:=!" & exit /b
  31.   set "SHA1=!SHA1: =!"
  32.   ECHO SHA1:!SHA1!
  33.   Exit /b
  34. )
  35. EXIT /B
  36. :GetVersion
  37. set filename=!currentfilename:\=\\!
  38. For /F "skip=1 delims=" %%c in ('wmic datafile where "name='!filename!'" get version') do (
  39.   set "Version=%%c"
  40.   if not "!Version:~0,1!"==" " (
  41.    echo Ver:!Version!
  42.    exit /b
  43.   ) else (
  44. rem echo GetVersion Failed
  45.    exit /b
  46.   )
  47. )
  48. EXIT /B
  49. :CreateFile
  50. echo !NXNAME! Ver:!Version! SIZE:!SIZE! SHA1:!SHA1! >>%~dp0!NXNAME!.SHA1
  51. if not exist "%~dp0!NXNAME!.SHA1" Call :OutputLog CreateFile 1
  52. for %%z in ("%~dp0!NXNAME!.SHA1") do ( if %%~zz==0 Call :OutputLog CreateFile 1 "Invalid Data" & exit /b)  
  53. exit /b
  54. :OutputLog
  55. set PARAMETERS=
  56. set PARAMETERS=%*
  57. if "!PARAMETERS!"=="" echo !date:~0,10! !time! --!currentfilename! OutputLog Failed No Parameters >>"!LOGFILE!" & exit /b
  58. if !errorlevel!==0 (
  59.   set Fail=1
  60.   if %1==Rename Echo !date:~0,10! !time! --!currentfilename! Rename to !filesrename! Succeeded>>"!LOGFILE!"
  61.   if %1==Decompression Echo !date:~0,10! !time! --!currentfilename! Decompression Succeeded>>"!LOGFILE!"
  62. ) else (
  63.   set Fail=0
  64.   if %1==Rename Echo !date:~0,10! !time! --!currentfilename! Rename to !filesrename! Failed ErrorCode=!errorlevel!>>"!LOGFILE!"
  65.   if %1==Decompression Echo !date:~0,10! !time! --!currentfilename! Decompression Failed ErrorCode=!errorlevel!>>"!LOGFILE!"
  66. )
  67. if "%2"=="0" (
  68.   set Fail=1
  69.   if %1==Getdate Echo !date:~0,10! !time! --!currentfilename! Get File Time Succeeded>>"!LOGFILE!"
  70. )
  71. if "%2"=="1" (
  72.   set Fail=0
  73.   if %1==Getdate Echo !date:~0,10! !time! --!currentfilename! Get File Time Failed %~3>>"!LOGFILE!"
  74.   if %1==CreateFile Echo !date:~0,10! !time! --!currentfilename! CreateFile Failed %~3>>"!LOGFILE!"
  75.   if %1==GetSHA1 Echo !date:~0,10! !time! --!currentfilename! GetSHA1 Failed %~3>>"!LOGFILE!"
  76. )
  77. exit /b
  78. :Exit
  79. echo Fatal error,try to change the platform,press any key to exit his program
  80. pause >nul
  81. exit /b
复制代码

作者: Batcher    时间: 2020-10-14 18:44

回复 4# 857185187


请参考Q-01观察一下在哪一步花费的时间比较长:
https://mp.weixin.qq.com/s/6lbb97qUOs1sTyKJfN0ZEQ




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2