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.
Related
Here I am trying to execute a batch script which deletes subfolders under folder "updates" in all remote machines. List of servers are passed as input
(serverlist.txt)
echo off
for /f %%l in (C:\deleteauto\delrem\serverlist.txt) do
if exist C:\updates goto sub
if not exist C:\updates goto nofile
:sub
del /f /q "C:\updates\*.*"
for /d %%d in ("C:\updates\*.*") do rmdir /s /q "%%d"
echo folder is deleted in %%l >>c:\finaloutput.txt
:nofile
echo No folders in %%l >>c:\final output.txt
Can someone please help in rectifying the errors. Script executed but outputs nothing.
Try this code:
#echo off
for /f "tokens=*" %%l in (C:\deleteauto\delrem\serverlist.txt) do (
if exist "\\%%l\C$\updates" (
pushd "\\%%l\C$\updates"
del /f /q "*."
for /d %%d in ("*.") do rmdir /s /q "%%d"
echo Folder is deleted on server: %%l>>finaloutput.txt
popd
) else (
echo Folder does not exist on server: %%l>>finaloutput.txt
)
)
Here's an example of how I may tackle the task:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SvrLst=C:\deleteauto\delrem\serverlist.txt"
If Not Exist "%SvrLst%" GoTo :EOF
(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%SvrLst%"
) Do PushD "%%~G\C$\updates" 2>NUL && (
RD /S /Q . 2>NUL
Echo Instruction to empty directory applied on %%~G & PopD
) || Echo Directory path not found on %%~G) 1>"C:\finaloutput.txt"
You will note that I have not stated that the directory was emptied. Just because an instruction was applied, does not mean that it was successful at doing so. Unless you check that the \C$\updates directory is completely empty afterwards, you should not imply that it is.
Errors were rectified by #Fire Fox , Thank you! Modified slightly & it worked as expected.
Code:
#echo off
for /f "tokens=*" %%l in (C:\deleteauto\delrem\serverlist.txt) do (
if exist "\\%%l\C$\updates" (
pushd "\\%%l\C$\updates"
del /f /q "\\%%l\C$\updates\*.*"
for /d %%d in ("\\%%l\C$\updates\*.*") do rmdir /s /q "%%d"
echo Folder is deleted on server: %%l>>C:\finaloutput.txt
popd
) else (
echo Folder does not exist on server: %%l>>C:\finaloutput.txt
)
)
this deletes .unwanted directories from qbittorrent downloads. it works great but the esthetic side effect that I can't shake is that it echoes the file not found error if none are found.
for /f "delims=" %%u in ('dir .unwanted /a:d /b /s') do rmdir /s /q "%%u" 2>nul && if defined v%~n0 echo deleted "%%u"
Better idea is to use /D /R than /F in the For loop for Directories. Check For /? documentation.
for /d /r %%u in (.unwanted) do rmdir /s /q "%%u" 2>nul & echo deleted %%u
This is assuming you are running this from the directory of execution.
It will only echo when the directory is deleted without the ""
We have multiple workstations that we need to do this on, and would like to batch script it (PowerShell is not an option for this particular task).
On each PC, we need to kill a specific process ("univmgr.exe"), then delete all subfolders and files from the following parent directory: C:\DRS\TEMP, except one subfolder named "DGNUser10" (as well as excluding everything inside the DGNUser10 subfolder).
I'm able to get my test script to work when I run it locally on a single test machine:
#echo off
taskkill /f /im univmgr.exe /t
pushd "C:\DRS\TEMP" || exit /B 1
for /D %%D in ("*") do (
if /I not "%%~nxD"=="DGNUser10" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
popd
But when I try to target a text file list of computers (list.txt), something is horribly wrong with my syntax, but I haven't yet been able to figure out what it is or how to fix it... Here's the current script I've been tweaking and testing:
:Start
cls
#echo off
for /f "tokens=*" %%A in (list.txt) do (
taskkill /f /im univmgr.exe /t
for /D %%D in ("C:\DRS\TEMP") do (
if /I not "%%~nxD"=="DGNUser10" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
)
Goto End
:End
Thanks to Squashman's gentle and spot-on leading, I was able to figure out what I needed to modify in my code in order to get the script working!
Here's the updated script:
:Start
cls
#echo off
for /f "tokens=*" %%A in (list.txt) do (
taskkill /s \\%%A /f /im univmgr.exe /t
pushd "\\%%A\c$\DRS\TEMP" || exit /B 1
for /D %%D in ("*") do (
if /I not "%%~nxD"=="DGNUser10" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
popd
)
Goto End
:End
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.
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