共享计算机上多个用户的批处理脚本

时间:2018-09-25 18:09:17

标签: windows batch-file

我对编码了解甚少,但我已经整理了一批清洁工。

我不知道如何为共享计算机上的每个用户运行它。

我所拥有的如下:

cd C:\Users\%username%\AppData\Local
rmdir /S /Q Temp
cd C:\
del C:\Windows\Prefetch\*.* /Q/F/S
del C:\Windows\Temp\*.* /Q/F/S
del C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Recent Items\*.* /Q
del "C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\ilk2mwjz.default\cache2\*.*"  /s /f /q /a
del "C:\Users\%USERNAME%\AppData\Local\Google\Chrome\User Data\Default\cache\*.*" /s /f /q /a
del "C:\Users\%USERNAME%\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\*.*" /s /f /q /a

一开始我有for /d %%a in (C:\Users\*),但这似乎破坏了它。

1 个答案:

答案 0 :(得分:0)

似乎您要尝试从每个用户中删除文件,您可以简单地使用FOR语句CD到文件所在的正确目录。贝娄就是一个例子。

:search
FOR /D %%G IN ("C:\Users\*") DO IF EXIST "%%~fG\desktop\*.ico" (
    set correctDir=%%G\desktop
    goto foundFile
)
goto :eof

:foundFile
cd "%correctDir%"
del /S *.ico
goto :search

上面的脚本将从每个用户桌面删除所有.ico文件。它的工作原理是在每个桌面上搜索任何现有的.ico文件,并且第一个结果将CD移至该文件。循环将返回FOR语句,直到在用户桌面上不再找到.ico为止。代码的基础取自我对线程batch script to delete all icons from all users' desktops的原始但类似的响应。

使用此方法,我们可以为每个用户创建多个文件/文件夹。 确保以管理员身份运行此脚本,因为某些位置需要管理员权限才能删除对象!

@ECHO OFF

Rem These item's are not user specific so we can just remove them.
del C:\Windows\Prefetch\*.* /Q /F /S
del C:\Windows\Temp\*.* /Q /F /S

Echo Windows\Prefetch\*.* Was deleted for all users!
Echo Windows\Temp\*.* Was deleted for all users!
goto File1

Rem The following items will be removed from each users.
Rem First Object.
:File1
FOR /D %%G IN ("C:\Users\*") DO IF EXIST "%%~fG\AppData\Local\Temp\*.*" (
    rem Here we need to CD one less layer to delete the folder.
    set "correctDir=%%G\AppData\Local"
    goto Found1
)
Echo Local\Temp\*.* Was deleted on all users!
goto File2

:Found1
cd "%correctDir%"
rmdir /S /Q Temp
goto File1

Rem Second object.
:File2
FOR /D %%G IN ("C:\Users\*") DO IF EXIST "%%~fG\AppData\Roaming\Microsoft\Windows\Recent Items\*.*" (
    set "correctDir=%%G\AppData\Roaming\Microsoft\Windows\Recent Items"
    goto Found2
)
Echo Windows\Recent Items\*.* Was deleted on all users!
goto File3

:Found2
cd "%correctDir%"
del /Q *.*
goto File2

Rem Third object.
:File3
FOR /D %%G IN ("C:\Users\*") DO IF EXIST "%%~fG\AppData\Local\Mozilla\Firefox\Profiles\ilk2mwjz.default\cache2\*.*" (
    set "correctDir=%%G\AppData\Local\Mozilla\Firefox\Profiles\ilk2mwjz.default\cache2"
    goto Found3
)
Echo ilk2mwjz.default\cache2\*.* Was deleted on all users!
goto File4

:Found3
cd "%correctDir%"
del /S /F /Q /A *.*
goto File3

Rem Forth object.
:File4
FOR /D %%G IN ("C:\Users\*") DO IF EXIST "%%~fG\AppData\Local\Google\Chrome\User Data\Default\cache\*.*" (
    set "correctDir=%%G\AppData\Local\Google\Chrome\User Data\Default\cache"
    goto Found4
)
Echo Default\cache\*.* Was deleted on all users!
goto File5

:Found4
cd "%correctDir%"
del /S /F /Q /A *.*
goto File4

Rem Fifth object.
:File5
FOR /D %%G IN ("C:\Users\*") DO IF EXIST "%%~fG\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\*.*" (
    set "correctDir=%%G\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC"
    goto Found5
)
Echo Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\*.* Was deleted on all users!
goto Completed

:Found5
cd "%correctDir%"
del /S /F /Q /A *.*
goto File5

Rem Finished Tasks.
:Completed
echo All Objects removed from users!
pause

goto :eof

有关命令的基础知识,

  • FOR /D使用%%G变量遍历所有目录。
  • %%~fG扩展到%%G中目录的完整路径。
  • IF EXIST检查文件是否存在。
  • CD指定您要更改为父目录。
  • goto :eof退出脚本。