I have been trying to create a batch file to wipe changes made on a computer, put simply: The company I work for hire out laptops, we image most of the laptops upon return to our warehouse. We have just purchased some new high spec laptops which are only being used on special occasions, we would like the ability to run a batch file that basically deletes any files added to the machine/clears the internet history etc.
I have run a few scripts but not found anything that works for all of the above, effectively we want to be able to run a batch file that restores the laptops and settings from a previous date or has the ability to wipe all files added after a certain date.
Can this be done?
We have used a similar file to wipe Laptops of a different spec which I have added below, this doesn't work on the high spec ones mentioned above.
#echo off
if exist "%USERPROFILE%\Documents" (
rd /s /q "%USERPROFILE%\Documents"
md "%USERPROFILE%\Documents"
) else (
rd /s /q "%USERPROFILE%\My Documents"
md "%USERPROFILE%\My Documents"
)
if exist "C:\A-R" (
rd /s /q "C:\A-R"
md "C:\A-R"
) else (
rd /s /q "C:\A-R"
md "C:\A-R"
)
if exist "C:\A-S" (
rd /s /q "C:\A-S"
md "C:\A-S"
) else (
rd /s /q "C:\A-S"
md "C:\A-S"
)
echo Deleting Temporary Internet Files
del /q /f /s "%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*"
echo deleted!
echo Deleting Downloads Folder Files
del /q /f /s "%USERPROFILE%\Downloads\*.*"
echo deleted!
echo Deleting History
del /q /f /s "%USERPROFILE%\AppData\Local\Microsoft\Windows\History\*.*"
echo Deleting Flash Player Temp Files
del /q /f /s "%USERPROFILE%\AppData\Roaming\Macromedia\Flash Player\*.*"
echo deleted!
echo Deleting Profile Temp Files
del /q /f /s "%USERPROFILE%\AppData\Local\Temp\*.*"
echo deleted!
echo Deleting User Office Recent Files
del /q /f /s "%USERPROFILE%\AppData\Roaming\Microsoft\Office\Recent\*.*"
echo deleted!
echo Deleting User Office TMP Files
del /q /f /s "%USERPROFILE%\AppData\Roaming\Microsoft\Office\*.tmp"
echo deleted!
start C:\EmptyRB.exe param1/F /Q
start C:\EmptyRB.exe param1/F /Q
shutdown.exe -s -t 00
exit
Thanks
If they are not administrators they cannot change system settings. Therefore just delete their user account. Done.
A batchfile to do this
net user <username> /delete
you'll have to delete their profile folder (the GUI will do this for you) if using the command line, (although you can wait a few months and do them in batches)
rd "c:\users\<username>" /s /q
to recreate a new user
net user <username> <password> /add
the folder gets created at first logon.
Related
I was trying to delete C:\Users\%USERPROFILE%\AppData\Local\Temp
and it shows the error:
The filename, directory name, or volume label syntax is incorrect.
This is the code:
#Echo off
del /s /q "C:\Windows\Temp\*.*"
del /s /q "C:\Windows\Prefetch\*.*"
del /s /q "C:\Users\%USERPROFILE%\AppData\Local\Temp\*.*"
for /d %%p in ("C:\Windows\Prefetch\*.*") do rmdir "%%p" /s /q
for /d %%p in ("C:\Windows\Temp\*.*") do rmdir "%%p" /s /q
for /d %%p in ("C:\Users\%USERPROFILE%\AppData\Local\Temp\*.*") do rmdir "%%p" /s /q
ipconfig /flushdns
pause
This is the output on running the batch file:
The filename, directory name, or volume label syntax is incorrect.
Windows IP Configuration
Successfully flushed the DNS Resolver Cache.
Press any key to continue . . .
The solution is quite simple on using this code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\reg.exe QUERY "HKLM\System\CurrentControlSet\Control\Session Manager" /V PendingFileRenameOperations >nul 2>nul && goto NoDelInfo
pushd "%SystemRoot%\Temp" 2>nul && ( rd /Q /S "%SystemRoot%\Temp" 2>nul & popd )
pushd "%SystemRoot%\Prefetch" 2>nul && ( rd /Q /S "%SystemRoot%\Prefetch" 2>nul & popd )
pushd "%TEMP%" 2>nul && ( rd /Q /S "%TEMP%" 2>nul & popd )
%SystemRoot%\System32\ipconfig.exe /flushdns
goto EndBatch
:NoDelInfo
echo There are pending file rename operations.
echo/
echo Please first restart Windows and then run "%~nx0" once again.
echo/
:EndBatch
endlocal
pause
For a full explanation of the three command lines to clear the three folders see:
How to delete files/subfolders in a specific directory at the command prompt in Windows?
The directories for temporary files should never be cleared on pending file/folder rename operations currently registered done by Windows on next start to complete an installation or an uninstall using perhaps files currently stored in one of the two directories for temporary files.
See also the Wikipedia article about the predefined Windows Environment Variables displayed with their values on running set in a command prompt window.
In general it is better to run as administrator the Disk Cleanup tool of Windows as it can delete much more files no longer needed as this batch file.
The plan was to uninstall flash player and cleanup the folders for every user on the device.
%CD%\uninstall_flash_player.exe -uninstall
IF EXIST "C:\Windows\SysWOW64\Macromed\Flash"(
rd /q /s "C:\Windows\SysWOW64\Macromed\Flash"
)
for /d %%a in (C:\Users\*) do (
IF EXIST "%%a\AppData\Roaming\Adobe\Flash Player"(
rd /q /s "%%a\AppData\Roaming\Adobe\Flash Player"
)
IF EXIST "%%a\AppData\Roaming\Macromedia\Flash Player"(
rd /q /s "%%a\AppData\Roaming\Macromedia\Flash Player"
)
)
pause
Problem is the folders don't get deleted but the uninstall went just fine.
I'm trying to remove all files and folders in a directory because I want to make a program to switch the files of two folders to be shown in another folder. I know there are threads like this, but I couldn't find any answer where the folder itself isn't deleted.
And I can't just make the folder after deleting because I don't know which and how many folders are in this directory.
That's the code:
#echo off
title Change forge version
::1.8 Mods are selected and will now be replaced
for /F %%i in ('dir /b "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods\*.*"') do (
echo 1.7 mods will now be copied into mods folder
echo and 1.8 mods will be copied back to 1.8 folder
echo Press a key to continue
pause >nul
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\mods" "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\mods" /Q /S
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods" "C:\Users\Florian\AppData\Roaming\.minecraft\mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods" /Q /S
goto END
)
::1.7.10 mods are selected and will now be replaced
for /F %%i in ('dir /b "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods\*.*"') do (
echo 1.8 mods will now be copied into mods folder
echo and 1.7 mods will now be copied back into 1.7 folder
echo Press a key to continue
pause >nul
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\mods" "C:\Users\Florian\AppData\Roaming\.minecraft\1.7_mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\mods" /Q /S
xcopy /e /s "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods" "C:\Users\Florian\AppData\Roaming\.minecraft\mods"
del "C:\Users\Florian\AppData\Roaming\.minecraft\1.8_mods" /Q /S
goto END
)
:END
echo press any key to close
pause >nul
And I need that at del the subfolders are also deleted.
Thanks.
To delete a directory with all subdirectories use rd /s /q "c:\directory"
I use batch file commands to delete the temp files in the system. The command works OK.
This code, works normally, but there is a flaw:
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
cd c:\temp
del /F /s /q *.* >c:\DelTempLog.txt
rd /s /q %systemdrive%\$Recycle.bin >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Installer\$PatchCache$\*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Installer\$PatchCache$
del /F /s /q *.* >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Temp*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Temp
del /F /s /q *.* >c:\DelTempLog.txt
del /q /s %tmp% >c:\DelTempLog.txt
Today I faced an exception where c:\temp folder did not exist on the server.
It deleted half of the files under c:\windows\system32.
I want to add an IF command after changing the DIR before deleting anything.
Also, please advise me how to do logging activity in a better way.
At an elementary level if you specify the full path on the command line then it cannot delete files from anywhere else.
del /F /s /q "c:\temp\*.*?"
There is also no need to change the directory before issuing the command.
The ? suppresses a prompt that asks if you are sure that you want to delete all files.
How about, before your batch-as-it-stands, you try
md c:\temp 2>nul
if not exist c:\temp\. echo No c:\temp!&goto :eof
#echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=c:\deltemplog.txt"
(for %%a in (
"c:\temp" "c:\windows\temp" "%temp%"
"%systemdrive%\$Recycle.bin" "C:\Windows\Installer\$PatchCache$"
) do if not exist "%%~a\" (
echo [ ERROR ]: "%%~a" does not exist
) else pushd "%%~a" && (
echo [ pushd ]: changed to "%%~a"
echo rmdir . /s /q
popd
) || (
echo [ ERROR ]: Failed to change to "%%~a"
)
) > "%logFile%"
For each folder in the list, change to it and if the command did not fail, remove the current folder (this will remove the content, not the folder, as it is the current one).
The rmdir commands are only echoed. If the output (in the log file) is correct, remove the echo command that prefixes rmdir
My preference is to use the %CD% built-in value, then try to move to the folder and see if it worked, as in:
set CURDIR=%CD%
pushd C:\Temp
if '%CD%'=='%CURDIR%' (
echo Failed to move to C:\Temp
) else (
[code to do your deletions]
)
popd
I prefer to push and pop a directory rather than a hard CD to it, simply because then you can always get back to where you were without having to know where that was.
I'm trying to make a program that will delete some files and perfrom rutine maintance on a computer by just clickin on one file. I'm testing it as I'm going along and realized that it's not deleting the folders. I want to delete everything within the folders but not the folders themselves. Here is my code so far:
#echo off
title SYSTEM Optimiation
echo Deleting Temp Folder
del /q /f "C:\Documents and Settings\%username%\Local Settings\TEMP"
echo.
echo DONE
echo.
echo Deleting Download folder
del /q /f "C:\Documents and Settings\%username%\My Documents\Downloads"
echo.
echo DONE
echo.
echo.
echo Hit any key to exit.
pause >nul
Try using wildcards and the /s switch on del:
del /q /s /f "%userprofile%\My Documents\Downloads\*"
but this will probably leave directories inside intact, but empty. Another option would be the quite explicit:
for /d /r "%userprofile%\My Documents\Downloads" %%x in (*) do rd /s /q "%%x"
for /r "%userprofile%\My Documents\Downloads" %%x in (*) do del /f "%%x"
Here much simpler than above. Current directory will be locked and therefore will not be deleted with others.
cd %Temp% && rmdir /s /q .