Batch file to perform start, run, %TEMP% and delete all - batch-file

looking for a way to perform the following:
Start > Run > "%TEMP% > Delete everything (skipping any conflicts).
so Far I have
#echo off
start %TEMP%
DEL *.*
I suppose I could use CD to get to the folder, the thing I'm wondering is if there are any instances where it cannot delete an a dialog box comes up, I want to skip these.
Thanks for the help!
Liam

del won't trigger any dialogs or message boxes. You have a few problems, though:
start will just open Explorer which would be useless. You need cd to change the working directory of your batch file (the /D is there so it also works when run from a different drive):
cd /D %temp%
You may want to delete directories as well:
for /d %%D in (*) do rd /s /q "%%D"
You need to skip the question for del and remove read-only files too:
del /f /q *
so you arrive at:
#echo off
cd /D %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *

The following batch commands are used to delete all your temp, recent and prefetch files on your System.
Save the following code as "Clear.bat" on your local system
*********START CODE************
#ECHO OFF
del /s /f /q %userprofile%\Recent\*.*
del /s /f /q C:\Windows\Prefetch\*.*
del /s /f /q C:\Windows\Temp\*.*
del /s /f /q %USERPROFILE%\appdata\local\temp\*.*
/Below command to Show the folder after deleted files
Explorer %userprofile%\Recent
Explorer C:\Windows\Prefetch
Explorer C:\Windows\Temp
Explorer %USERPROFILE%\appdata\local\temp
*********END CODE************

If you want to remove all the files in the %TEMP% folder you could just do this:
del %TEMP%\*.* /f /s /q
That will remove everything, any file with any extension (*.*) and do the same for all sub-folders (/s), without prompting you for anything (/q), it will just do it, including read only files (/f).
Hope this helps.

#echo off
RD %TEMP%\. /S /Q
::pause
explorer %temp%
This batch can run from anywhere.
RD stands for Remove Directory but this can remove both folders and files which available to delete.

cd C:\Users\%username%\AppData\Local
rmdir /S /Q Temp
del C:\Windows\Prefetch*.* /Q
del C:\Windows\Temp*.* /Q
del C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent Items*.* /Q
pause

#echo off
del /s /f /q %windir%\temp\*.*
rd /s /q %windir%\temp
md %windir%\temp
del /s /f /q %windir%\Prefetch\*.*
rd /s /q %windir%\Prefetch
md %windir%\Prefetch
del /s /f /q %windir%\system32\dllcache\*.*
rd /s /q %windir%\system32\dllcache
md %windir%\system32\dllcache
del /s /f /q "%SysteDrive%\Temp"\*.*
rd /s /q "%SysteDrive%\Temp"
md "%SysteDrive%\Temp"
del /s /f /q %temp%\*.*
rd /s /q %temp%
md %temp%
del /s /f /q "%USERPROFILE%\Local Settings\History"\*.*
rd /s /q "%USERPROFILE%\Local Settings\History"
md "%USERPROFILE%\Local Settings\History"
del /s /f /q "%USERPROFILE%\Local Settings\Temporary Internet Files"\*.*
rd /s /q "%USERPROFILE%\Local Settings\Temporary Internet Files"
md "%USERPROFILE%\Local Settings\Temporary Internet Files"
del /s /f /q "%USERPROFILE%\Local Settings\Temp"\*.*
rd /s /q "%USERPROFILE%\Local Settings\Temp"
md "%USERPROFILE%\Local Settings\Temp"
del /s /f /q "%USERPROFILE%\Recent"\*.*
rd /s /q "%USERPROFILE%\Recent"
md "%USERPROFILE%\Recent"
del /s /f /q "%USERPROFILE%\Cookies"\*.*
rd /s /q "%USERPROFILE%\Cookies"
md "%USERPROFILE%\Cookies"

#echo off
del /s /f /q c:\windows\temp\*.*
rd /s /q c:\windows\temp
md c:\windows\temp
del /s /f /q C:\WINDOWS\Prefetch
del /s /f /q %temp%\*.*
rd /s /q %temp%
md %temp%
deltree /y c:\windows\tempor~1
deltree /y c:\windows\temp
deltree /y c:\windows\tmp
deltree /y c:\windows\ff*.tmp
deltree /y c:\windows\history
deltree /y c:\windows\cookies
deltree /y c:\windows\recent
deltree /y c:\windows\spool\printers
del c:\WIN386.SWP
cls

Just use
del /f /q C:\Users\%username%\AppData\Local\temp
And it will work.
Note: It will delete the whole folder however, Windows will remake it as it needs.

Related

Deleting files and folders in a folder with batch

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"

del /s /q "file location" isn't working

del /S /Q "C:\TEST\TESTFOLDER\"
isn't actually deleting TESTFOLDER.
Del command is used for deleting files. If you want to delete folder, you should use rmdir with /s /q switches to force delete the folder and all it's files without prompt
rmdir /S /Q "C:\TEST\TESTFOLDER\"

Delete files and subfolders in the folders "tmp" and "cache"

I want to use this script below to clean out "tmp" and "cache" folders in websites like "C:\Storage\Websites\Site1".
It tries to delete files and subfolders in "C:\Storage\Websites\Site1\tmp" and "C:\Storage\Websites\Site1\cache".
Which is right, but it also tries to delete files and subfolders in, for example, "C:\Storage\Websites\Site1\MySpecialLittleProgram\tmp" and, for example, "C:\Storage\Websites\Site1\MySpecialLittleProgram\cache".
Which is wrong. It should only clean up the "tmp" and "cache" folder in the root of the website and not in other subfolders.
If I delete the /s parameter in 'dir /a:d /b /s tmp cache' it will not find anything.
How can I do this part?
(I have deleted the /q parameter in the file deleting part and the folder removing part if anyone copies my script.)
#echo off
call:CleanUp "C:\Storage\Websites"
echo.&pause&goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:CleanUp
IF EXIST %~1 (
cd /d %~1
FOR /f "tokens=*" %%i in ('dir /a:d /b /s tmp cache') DO (
echo %%i
::DELETING FILES I FOLDERS AND SUBFOLDERS
del %%i /s
::DELETING NOW EMPTY FOLDERS AND SUBFOLDERS
FOR /D %%p IN ("%%i\*.*") DO rmdir "%%p" /s
)
)
goto:eof
UPDATE:
I updated my code to be (it is working now):
#echo off
call:CleanUp "C:\Storage\Web"
call:CleanUp "C:\Storage\Web-IIS"
goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:CleanUp
IF EXIST %~1 (
cd /d %~1
FOR /f "tokens=*" %%i in ('dir /a:d /b') DO (
IF EXIST %%i\tmp (
del %%i\tmp /s /q
FOR /D %%p IN ("%%i\tmp\*.*") DO rmdir "%%p" /s /q
)
IF EXIST %%i\cache (
del %%i\cache /s /q
FOR /D %%p IN ("%%i\cache\*.*") DO rmdir "%%p" /s /q
)
)
)
goto:eof
This should remove the files in those two locations:
#echo off
del "C:\Storage\Websites\Site1\tmp\*.*" /a /s
del "C:\Storage\Websites\Site1\cache\*.*" /a /s
From your comment, this may be what you need to do: remove the echo keyword after testing it to see the commands on the console that would be executed.
#echo off
cd /d "C:\Storage\Websites"
for /d %%a in (*) do (
for %%b in (tmp cache) do (
pushd "%%~fa\%%b" 2>nul && (echo rd /s /q "%%~fa\%%b" 2>nul & popd)
)
)
pause
I suggest to use rmdir or rd for this task:
rd "C:\Storage\Websites\Site1\tmp" /S /Q
md "C:\Storage\Websites\Site1\tmp"
rd "C:\Storage\Websites\Site1\cache" /S /Q
md "C:\Storage\Websites\Site1"
Command rd with options /S for all subdirectories and /Q for quiet deletes also tmp and cache, but those 2 directories can be easily recreated using command md although I'm quite sure that this would not be really necessary here as the application creating tmp and cache would do it also automatically.
If that is not what you want, please show as directory listings with files and folders in one of the two directories before cleanup and after cleanup.

Bat script to delete files with specific extention

I am trying to write a bat script which will delete every file with a specific extension (for example '.log') from all drives, including external ones which are attached.
For example, if no external drives are attached the script will remove all '.log' files under C:\ and other mounted partitions at that time.
If there are external drives attached, the script will remove all '.log' files under all the drives.
[Not essential] It would be really nice, if the script could preserve .log files larger than 1MB for example. Also, it would be awesome if the script could automatically detect new attached drives and delete '.log' files from them too.
I know I am asking a lot, but your knowledge has impressed me many many times!
Thank you very much in advance!
You just need a for loop to do this (taking 09stephenb's code as the guts :))
#echo off
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do (
echo INFO: Removeing all log files from %%x\
del %%x\*.log /s /f /q
)
echo INFO: Done
pause
Is this what you want.
#echo off
pushd C:\
echo removeing all log files
del \*.log /s /f /q
echo done & pause
exit
It will search for all files with a .log extension. Just make sure you don't need any of them. You will need to run the batch file as a Admin. Replace pushd C:\ with pushd [your drive]:\.
To use all drives:
#echo off
echo removeing all log files
pushd A:\
del \*.log /s /f /q
pushd B:\
del \*.log /s /f /q
pushd C:\
del \*.log /s /f /q
pushd D:\
del \*.log /s /f /q
pushd E:\
del \*.log /s /f /q
pushd F:\
del \*.log /s /f /q
pushd G:\
del \*.log /s /f /q
pushd H:\
del \*.log /s /f /q
pushd I:\
del \*.log /s /f /q
pushd J:\
del \*.log /s /f /q
pushd K:\
del \*.log /s /f /q
pushd L:\
del \*.log /s /f /q
pushd M:\
del \*.log /s /f /q
pushd N:\
del \*.log /s /f /q
pushd O:\
del \*.log /s /f /q
pushd P:\
del \*.log /s /f /q
pushd Q:\
del \*.log /s /f /q
pushd R:\
del \*.log /s /f /q
pushd S:\
del \*.log /s /f /q
pushd T:\
del \*.log /s /f /q
pushd U:\
del \*.log /s /f /q
pushd V:\
del \*.log /s /f /q
pushd W:\
del \*.log /s /f /q
pushd X:\
del \*.log /s /f /q
pushd Y:\
del \*.log /s /f /q
pushd Z:\
del \*.log /s /f /q
echo done & pause
exit

DO was unexpected at this time. error 255

I have a batch script that cleans out all user's cache files.
REM Set file locations for temp/history/cookies files.
SET SRC1=C:\Users
SET SRC2=AppData\Local\Microsoft\Windows\Temporary Internet Files
SET SRC3=AppData\Local\Microsoft\Windows\History
SET SRC4=AppData\Local\Temp
SET SRC5=AppData\Roaming\Microsoft\Windows\Cookies
SET SRC6=AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5
REM begin cleaning internet files
echo cleaning temporary internet files
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC2%\*.*") DO RMDIR /S /Q "%%Y"
REM begin cleaning history files
echo cleaning history
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC3%\*.*") DO RMDIR /S /Q "%%Y"
REM begin cleaning windows temp files
echo cleaning windows temp files
FOR /D %%X IN ("%SRC1%\*") DO FOR /D %%Y IN ("%%X\%SRC4%\*.*") DO RMDIR /F /S /Q "%%Y"
FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC3%\*.*") DO DEL /F /S /Q "%%Y"
FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC4%\*.*") DO DEL /F /S /Q "%%Y"
REM begin cleaning Cookies folder
echo cleaning Cookies
FOR /D %%X IN ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC5%\*.*") DO DEL /F /S /Q "%%Y"
REM del C:\temp folder
FOR /D %%X IN ("%windir%\temp\*") DO RMDIR /F /S /Q "%%X"
DEL /f /s /q "%windir%\temp"
REM Del Temp Internet Files
FOR /D %%x in ("%SRC6%\*") DO FOR %%Y DO DEL /F /S /Q "%%Y"
FOR /D %%x in ("%SRC1%\*") DO FOR %%Y IN ("%%X\%SRC6%\*.*") DO DEL /F /S /Q "%%Y"
:END
When running this via psexec, I am getting the following error:
DO was unexpected at this time.
batch file exited on PCNAME with error code 255.
On other Windows 7 and XP machines, I'm able to run this and I get an error that states it was successful.
This is the first time I've seen this error but I can't seem to narrow down the error.
Can anyone lend a second pair of eyes?
See the lines below in your code.
...
REM Del Temp Internet Files
FOR /D %%x in ("%SRC6%\*") DO FOR %%Y DO DEL /F /S /Q "%%Y"
...
You are missing the IN (...) part of your nested FOR loop.
Hence: DO was unexpected at this time.

Resources