使用命令或批处理Cleanmgr

时间:2018-11-03 13:05:34

标签: batch-file command-line windows-10 command-prompt

我正在尝试为Windows 10构建维护脚本。此Shell或命令行应一次执行多个维护操作。 我在StackOverflow上找不到这种类型的批处理演示,因此我在这里共享。

2 个答案:

答案 0 :(得分:0)

它使用Windows 10 Clean Manager cleanmgr.exe 它创建一个清洁配置文件,编号为777,已注册,可以使用cleanmgr / sagerun:777

进行调用。

您必须使用管理员权限启动此批处理才能修改注册表。

:: Disk cleanup
echo 1/10 ^| Windows is cleaning his mess ...

REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Content Indexer Cleaner" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Microsoft_Event_Reporting_2.0_Temp_Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
::REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Remote Desktop Cache Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\ServicePack Cleanup" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files" /v StateFlags0777  /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Sync Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\WebClient and WebPublisher Cache" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender" /v StateFlags0777 /d 2 /t REG_DWORD /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f    
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files" /v StateFlags0777 /t REG_DWORD /d 00000002 /f
cleanmgr /sagerun:777
echo OK
echo:

由于我在StackOverflow上找不到任何帮助来构建此脚本,因此,我分享了我的工作。

然后您可以链接其他系统维护操作,例如,系统文件检查sfc.exe。

要开始使用,请将此脚本复制并粘贴到空白记事本中,并另存为scriptName.cmd

:: System integrity
echo 2/10 ^| Windows file checking starting ...
%SystemRoot%\System32\sfc.exe /scannow
if not %ERRORLEVEL%==0 (
   echo Erreurs détectées sur le disque !
   echo Démarrage de la réparation du système ...
   Dism /Online /Cleanup-Image /RestoreHealth
) else (
  echo OK
)
echo:

答案 1 :(得分:0)

我已经在这里添加了您的想法,包括一些使用“数组”(包含多个用引号和逗号分隔的字符串的变量)的循环。

第一个名为“ VOLUME_LOCATIONS”的“数组”包含您在上面共享的内容(不带“回收站”)。

尽管已将脚本设置为捕获此处存在的所有子项: HKLM \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Explorer \ VolumeCaches

可以通过预定义的名为“ OMITTED_LOCATIONS”的“数组”来减少名为“ DYNAMIC_ARRAY_VOLUMES”的“数组”。 这种动态方法考虑了将来可能添加的新卷/位置。

@echo off
REM This script loops through an array of volume cache locations in the registry and sets a flag
REM This replicates what is done via GUI by the command [ cleanmgr /sageset ] and then executes via [ cleanmgr /sagerun:# ]
REM This script requires to be run as an administrator - for Windows > Server 2008 (Or have cleanmgr added)
cls
setlocal EnableDelayedExpansion
SET FLAG=StateFlags0777 && SET REG_VALUE=00000002
SET REG_LOC=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches
Set "NUM_ENTRIES=0" && Set "LAST_ENTRY=0"
SET VOLUME_LOCATIONS=("Active Setup Temp Folders", "Content Indexer Cleaner", "Downloaded Program Files", "Internet Cache Files", "Memory Dump Files", "Microsoft_Event_Reporting_2.0_Temp_Files", "Offline Pages Files", "Old ChkDsk Files", "Previous Installations", "Remote Desktop Cache Files", "ServicePack Cleanup", "Setup Log Files", "System error memory dump files", "System error minidump files", "Temporary Files", "Temporary Setup Files", "Temporary Sync Files", "Update Cleanup", "Upgrade Discarded Files", "WebClient and WebPublisher Cache", "Windows Defender", "Windows Error Reporting Archive Files", "Windows Error Reporting Queue Files", "Windows Error Reporting System Archive Files", "Windows Error Reporting System Queue Files", "Windows ESD installation files", "Windows Upgrade Log Files")
REM To dynamically create an array of ALL entries, then presumably omitting those undesired:
for /F "delims=" %%i IN ('reg query "%REG_LOC%"') do set /a "NUM_ENTRIES+=1"
echo There are %NUM_ENTRIES% entries in the registry.
FOR /F "delims=" %%G IN ('reg query "%REG_LOC%"') do (
set /a "LAST_ENTRY+=1"
Set DYNAMIC_ARRAY_VOLUMES=!DYNAMIC_ARRAY_VOLUMES!, "%%~nxG"
Set DYNAMIC_ARRAY_VOLUMES[%%~nxG]=!LAST_ENTRY!
if !LAST_ENTRY! == 1 Set DYNAMIC_ARRAY_VOLUMES="%%~nxG"
if !LAST_ENTRY! == %NUM_ENTRIES% GOTO :ARRAY_BUILT
)
:ARRAY_BUILT
echo The following would be an all inclusive list of locations:
echo. && echo (%DYNAMIC_ARRAY_VOLUMES%) && echo.
REM The following array and 'for loop' sets and removes entries wished to omitted from cleanmanager cleanup
REM SET OMITTED_LOCATIONS=("Recycle Bin", "Windows Upgrade Log Files")
SET OMITTED_LOCATIONS=("")
REM For loop repeated to handle the last entry, if applicable, as it should only have a preceeding comma and space
for %%q in %OMITTED_LOCATIONS% do SET DYNAMIC_ARRAY_VOLUMES=!DYNAMIC_ARRAY_VOLUMES:%%q, =!
for %%q in %OMITTED_LOCATIONS% do SET DYNAMIC_ARRAY_VOLUMES=!DYNAMIC_ARRAY_VOLUMES:, %%q=!
echo The following would be a reduced list of locations: && echo (%DYNAMIC_ARRAY_VOLUMES%) && echo.

echo Adding registry values via array and loop, use the following line instead of the one after - if attempting dynamically
for %%i in (%DYNAMIC_ARRAY_VOLUMES%) do (
REM for %%i in %VOLUME_LOCATIONS% do (
ECHO REG ADD "%REG_LOC%\%%~i" /v %FLAG% /t REG_DWORD /d %REG_VALUE% /f
REG ADD "%REG_LOC%\%%~i" /v %FLAG% /t REG_DWORD /d %REG_VALUE% /f
)
echo. && echo The disk space for C: BEFORE running: && echo.  && fsutil volume diskfree c: && echo.
ECHO Running cleanmgr against the specified volume locations
ECHO "cleanmgr /sagerun:777"
cleanmgr /sagerun:777
echo. && echo The disk space for C: AFTER running: && echo.  && fsutil volume diskfree c: && echo.
REM To verify what exists for VolumeCaches in the registry, or to see changes before/after, run:
REM reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches
REM To return all the subkey values, add the '/s' argument, as seen below:
REM reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches /s
echo 
REM pause
exit /b
REM exit

这里最具挑战性的部分是解释创建和使用“数组”中的引号和定界。我猜有更好的方法,但这似乎可行。 另外,在较旧的服务器上,您可能需要启用'Desktop Experience'以使cleanmgr.exe重新回到Windows \ System32文件夹中,从而跳过整个循环...

为什么要批处理,为什么要使用cleanmgr.exe?

  • 我的公司通常不在服务器上enable Powershell

  • Cleanmgr结合了Microsoft清理磁盘和 存在重大差异,而不仅仅是删除 临时/缓存位置。具体来说,我了解当前/最近 数据在删除过程中被忽略了,possibly ~7+days old