简体   繁体   中英

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.

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.

: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. It works by searching each desktop for any existing .ico files and the first result it will CD to it. The loop will return to the FOR statement until it finds no more .ico 's on users desktops. 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.
  • %%~fG expands to the full path to the directory in %%G .
  • IF EXIST checks if a file exists.
  • CD specifies that you want to change to the parent directory.
  • goto :eof exits the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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