简体   繁体   English

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

[英]Batch Script for Multiple Users on Shared Computer

I know very little about coding but I've cobbled together a cleaning batch. 我对编码知之甚少,但是我整理了一批清洁工。

I cannot figure out how to run it for every user on a shared computer. 我无法弄清楚如何为共享计算机上的每个用户运行它。

What I have is as follows: 我所拥有的如下:

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

I had for /d %%a in (C:\\Users\\*) at the beginning, but that seems to break it. 一开始我for /d %%a in (C:\\Users\\*)中有for /d %%a in (C:\\Users\\*) ,但这似乎打破了它。

Appearing you're trying to remove files from each user, you can simply use an FOR statement to CD to the correct directory the file exists in. Bellow is an example. 似乎您要尝试从每个用户中删除文件,您可以简单地使用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

The script above will remove all .ico files from each users desktop. 上面的脚本将从每个用户桌面删除所有.ico文件。 It works by searching each desktop for any existing .ico files and the first result it will CD to it. 它通过搜索每个桌面的任何现有.ico文件和第一个结果它将CD吧。 The loop will return to the FOR statement until it finds no more .ico 's on users desktops. 循环将返回到FOR语句,直到在用户桌面上找不到更多的.ico为止。 The basis of the code was taken from my original but similar response on the thread - batch script to delete all icons from all users' desktops . 该代码的基础取自我对线程批处理脚本的原始但类似的响应,该脚本从所有用户的桌面删除所有图标

Using this method, we can do multiple files/folders for each user. 使用这种方法,我们可以为每个用户创建多个文件/文件夹。 Be sure to run this script as administrator as some locations require admin access to remove Objects! 请确保以管理员身份运行此脚本,因为某些位置需要管理员权限才能删除对象!

@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 command basics, 对于命令基础,

  • FOR /D iterates over all directories using the %%G variable. FOR /D使用%%G变量遍历所有目录。
  • %%~fG expands to the full path to the directory in %%G . %%~fG扩展到%%G目录的完整路径。
  • IF EXIST checks if a file exists. IF EXIST检查文件是否存在。
  • CD specifies that you want to change to the parent directory. CD指定您要更改到父目录。
  • goto :eof exits the script. goto :eof退出脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM