Help me make a batch file to clean temp files

Discussion in 'Scripting' started by Phoenix, Oct 29, 2014.

  1. Phoenix

    Phoenix MDL Expert

    May 30, 2014
    1,037
    436
    60
    Can someone tell me what text I need to type to create a batch file that would delete the following folders because I have fixed all my event log errors so far with the exception of the indexing error:



    Upon doing some search, I found that CCleaner may be behind this so I wanna try a format without ever using CCleaner

    PS: Re-building the Index doesn't help

    What I want the batch file to delete is the following:

    1) C:\Windows\Temp (delete all the files and folders in that folder but not the folder itself)

    2) C:\Users\MYUSERNAME\AppData\Local\Temp (delete all the files and folders in that folder but not the folder itself)

    3) C:\Windows\SoftwareDistribution\Download (delete all the files and folders in that folder but not the folder itself)

    Thanks
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    7,156
    21,909
    240
    #2 abbodi1406, Oct 29, 2014
    Last edited by a moderator: Apr 20, 2017
    Code:
    DEL /S /F /Q "%temp%\*" 1>nul 2>nul
    DEL /S /F /Q %systemroot%\temp\* 1>nul 2>nul
    DEL /S /F /Q %systemroot%\SoftwareDistribution\Download\* 1>nul 2>nul
    for /f %%i in ('"dir /s /b /ad "%temp%"" 2^>nul') do RD /S /Q %%i 1>nul 2>nul
    for /f %%i in ('"dir /s /b /ad %windir%\temp" 2^>nul') do RD /S /Q %%i 1>nul 2>nul
    for /f %%i in ('"dir /s /b /ad %windir%\SoftwareDistribution\Download" 2^>nul') do RD /S /Q %%i 1>nul 2>nul
    
     
  3. Phoenix

    Phoenix MDL Expert

    May 30, 2014
    1,037
    436
    60
    Awesome!!!!!!!!!!!!!!!!!!!! Thanks a lot man!!! I would've never been to figure this out!! you are a champ! Tested and working like a charm!

    :worthy:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. boscru6469

    boscru6469 MDL Member

    Jan 9, 2012
    243
    60
    10
    #4 boscru6469, Oct 29, 2014
    Last edited by a moderator: Apr 20, 2017
    It did not clean C:\Users\MYUSERNAME\AppData\Local\Temp
     
  5. Phoenix

    Phoenix MDL Expert

    May 30, 2014
    1,037
    436
    60

    Strange, I just copied a file there and it DID clean it, although there are a few folders and files but when I tried to delete them manually I couldn't so they are in use that's why

    try copying a temp file in there like a pic or text file to verify you are not missing out on the locked files because the scraipt works perfectly here and there is no reason it shouldn't work for you

    I don't wanna use CCleaner as it causes my Indexing to get corrupted and I have read about this issue online that's why I requested this script and finally I have it! no more CCleaner!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. boscru6469

    boscru6469 MDL Member

    Jan 9, 2012
    243
    60
    10
    That was it. I put a few odd files with different extensions and cleaned them out. I can deal with a few oddball system files/folders being left behind. thanks
     
  7. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    7,156
    21,909
    240
    #7 abbodi1406, Oct 30, 2014
    Last edited by a moderator: Apr 20, 2017
    If you want the whole script i use:
    Code:
    @echo off
    title .
    color 1F
    openfiles >nul 2>&1
    if %errorlevel% NEQ 0 goto :UACPrompt
    goto :gotAdmin
    
    :UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~fs0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /b
    
    :gotAdmin
    DEL /S /F /Q "%temp%\*" 1>nul 2>nul
    DEL /S /F /Q %systemroot%\temp\* 1>nul 2>nul
    DEL /S /F /Q %systemroot%\inf\*.log 1>nul 2>nul
    DEL /S /F /Q %systemroot%\Logs\CBS\* 1>nul 2>nul
    DEL /S /F /Q %systemroot%\Logs\DPX\* 1>nul 2>nul
    DEL /S /F /Q %systemroot%\Logs\DISM\* 1>nul 2>nul
    DEL /S /F /Q %systemroot%\Microsoft.NET\Framework\v2.0.50727\*.log 1>nul 2>nul
    DEL /S /F /Q %systemroot%\Microsoft.NET\Framework\v4.0.30319\*.log 1>nul 2>nul
    if exist %systemroot%\SysWOW64\cmd.exe DEL /S /F /Q %systemroot%\Microsoft.NET\Framework64\v2.0.50727\*.log 1>nul 2>nul
    if exist %systemroot%\SysWOW64\cmd.exe DEL /S /F /Q %systemroot%\Microsoft.NET\Framework64\v4.0.30319\*.log 1>nul 2>nul
    DEL /S /F /Q %systemroot%\SoftwareDistribution\Download\* 1>nul 2>nul
    RD /S /Q "%LocalAppData%\Microsoft\Windows\WER\ReportQueue" 1>nul 2>nul
    for /f %%i in ('"dir /s /b /ad "%temp%"" 2^>nul') do RD /S /Q %%i 1>nul 2>nul
    for /f %%i in ('"dir /s /b /ad %windir%\SoftwareDistribution\Download" 2^>nul') do RD /S /Q %%i 1>nul 2>nul
    for /f %%i in ('"dir /s /b /ad %windir%\temp" 2^>nul') do RD /S /Q %%i 1>nul 2>nul
    
     
  8. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,473
    626
    60
    These files are probably still in use...
     
  9. Smorgan

    Smorgan Glitcher

    Mar 25, 2010
    1,854
    1,029
    60
    Why are you guys even doing this when you can get windows to clear the temp folder via a registry edit like so:

    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" /v "LastAccess" /d "3" /t "REG_DWORD" /f
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    12
    10
    #10 Kamrul08, Oct 30, 2014
    Last edited by a moderator: Apr 20, 2017
    Could you explain the command 1>nul 2>nul and dir /s /b /ad pls?
     
  11. Phoenix

    Phoenix MDL Expert

    May 30, 2014
    1,037
    436
    60
    #11 Phoenix, Oct 30, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    what's the difference between this and the first batch code I got? What do you mean if I want the full script? :confused:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    12
    10
    1st command will run with standard user (for Win7 or later) that may not delete %systemroot%\SoftwareDistribution\Download\ but 2nd command will run with administrative mode. 2nd command also included some extra tasks.
     
  13. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    7,156
    21,909
    240
    #13 abbodi1406, Dec 3, 2014
    Last edited by a moderator: Apr 20, 2017
    sorry for very late replay :D

    Nothing really, the second is the full script i have to easily double-click, and have "extra" entries

    you also can save it as C:\ProgramData\CleanTemp.cmd, and add this reg to make available in Desktop Background right-click menu:
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\DesktopBackground\Shell\Clean]
    @="Clean Temp"
    "icon"="%systemroot%\\system32\\imageres.dll,84"
    "Position"="Bottom"
    "HasLUAShield"=""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\DesktopBackground\Shell\Clean\command]
    @=hex(2):63,00,6d,00,64,00,2e,00,65,00,78,00,65,00,20,00,2f,00,63,00,20,00,25,\
      00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,44,00,61,00,74,00,61,00,25,00,\
      5c,00,43,00,6c,00,65,00,61,00,6e,00,54,00,65,00,6d,00,70,00,2e,00,63,00,6d,\
      00,64,00,00,00
    "IsolatedCommand"=hex(2):63,00,6d,00,64,00,2e,00,65,00,78,00,65,00,20,00,2f,00,63,00,20,00,25,\
      00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,44,00,61,00,74,00,61,00,25,00,\
      5c,00,43,00,6c,00,65,00,61,00,6e,00,54,00,65,00,6d,00,70,00,2e,00,63,00,6d,\
      00,64,00,00,00
    
    
     
  14. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    7,156
    21,909
    240
    1>nul 2>nul to suppress any output (result or error)
    dir /s /b /ad to list sub-directories only, which needs to be removed using RD /S /Q
     
  15. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,283
    18,090
    340
    #15 s1ave77, Dec 3, 2014
    Last edited by a moderator: Apr 20, 2017
    Huih ... nice, more useful contex(menu) :good3:. May i ask how you gathered the hex values :eek:?

    Interesting to see the REG after conversion to BAT :cool2::

    Code:
    reg add "HKLM\SOFTWARE\Classes\DesktopBackground\Shell\Clean" /ve /t REG_SZ /d "Clean Temp" /f
    reg add "HKLM\SOFTWARE\Classes\DesktopBackground\Shell\Clean" /v "icon" /t REG_SZ /d "%%systemroot%%\system32\imageres.dll,84" /f
    reg add "HKLM\SOFTWARE\Classes\DesktopBackground\Shell\Clean" /v "Position" /t REG_SZ /d "Bottom" /f
    reg add "HKLM\SOFTWARE\Classes\DesktopBackground\Shell\Clean" /v "HasLUAShield" /t REG_SZ /d "" /f
    reg add "HKLM\SOFTWARE\Classes\DesktopBackground\Shell\Clean\command" /ve /t REG_EXPAND_SZ /d "cmd.exe /c %%ProgramData%%\CleanTemp.cmd" /f
    reg add "HKLM\SOFTWARE\Classes\DesktopBackground\Shell\Clean\command" /v "IsolatedCommand" /t REG_EXPAND_SZ /d "cmd.exe /c %%ProgramData%%\CleanTemp.cmd" /f
    
    
    Using RegConvert by wtarkan. Not sure about the doubled percent signs in programdata/systemroot variable .

    EDIT: Although code looks weird, it's only the confusing REG syntax. Test shows it performs same action like the REG script.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    7,156
    21,909
    240
    AFAIR, by adding the entries manually in regedit, then exporting the Key
    i guess i followed Win7 reg tweaks scheme for My Computer context :D

    yes, doubled percent signs is required to preserve the variable state (using one sign will add it as explicit path C:\Windows\system32)

    btw, DX Tool has nice features, including converting Reg to Inf and Inf to Reg
     
  17. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,283
    18,090
    340
    #17 s1ave77, Dec 3, 2014
    Last edited by a moderator: Apr 20, 2017
    OK.

    Variable syntax as usual, stupid me, and that after writing a kilometer write to file code in batch :doh:.

    Nice tool :good3:. Little overkill but looks interesting. Like the RegConvert to easily and fast convert REG to BAT via command line switches, no more REG files, only CMD - yay. It also offers a GUI if wanted.

    Apropos weird REG syntax, it needs to mention the EMPEROR OF WEIRD REG TRICKS feat. MR Jinje. Although the script works flawlessly when started by (double)clicking, it fails when importing the REG via CMD, as the actual command seems to be infinite, it needed several Breaks to run.

    There is a nice soultion by Mr Jinje for Context Menu Entry to Restart Explorer.

    Example for cascaded context Menu 'TOOLS' with 'Restart Explorer by Mr Jinje':

    CMD:
    Code:
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "MUIVerb" /t REG_SZ /d "TOOLS" /f
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "SubCommands" /t REG_SZ /d "Restart_Explorer" /f
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "Icon" /t REG_SZ /d "imageres.dll,220" /f
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "Position" /t REG_SZ /d "top" /f
    
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Restart_Explorer" /ve /t REG_SZ /d "Restart Explorer" /f
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Restart_Explorer" /v "icon" /t REG_SZ /d "imageres.dll,-5339" /f
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Restart_Explorer\Command" /ve /t REG_SZ /d "cmd /c start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta -command \"$PAYLOAD = ^
    'H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/In7j5Dd^
    Onq2X07aolumrvGmzuv39T9+tyqrO6/QX4+s39XX6i7+n343Nd9//mWrdbi/XZflL0mnWTufpL07R+ng2235zvcpT/vdpfl4sCwb+e35M36+bYnmRvr5u2nxxGP45PqnKMmdEmvHn+TKvi2m3yav1si0W^
    +fhs2eZ1tXqd15fFNG8O0fEyW+TNKpvmZhi/cULor9aTspim0zJrmtTgzt987/Wb4zfzOs9m38fr33talmeLVVW3Wx+tm7y+tzeeleVHo/R13j7Pmva0rqs6/Sxt63V+5/sWMPXU0o/8HSG0TAmvl22dP^
    iuWs+/S/6urraatgb/81ZwAjVEafPimaMv8Dg/he3Xeruvlo/SLrG7mWXncbH21XGTL7CKfgZrjJ1VVovP3RTbEckJQ0pdV036RNw3B3lK0599dzkbpuli26RfNxciM5uplVmcL+2fJfwrCIREuq2KWvq^
    BB32ECn74rWkPxLbTXSRSiEwH0t/HrMs9XW3s7Ozteq6dFdrEkHItpM35ZVzTLzfg1pnXro1yBjvN3+Ud45ZfYETIKYceEiTe+N3V2TYTxZuij1/O8LH9/fEHfEw3B04DqU8i8Okp33t1/sm+IMf4pYs^
    PgD0bnlxBCH/+ejBfNVU9yHj1SIuE/bkPTtn1cFlmTvjrtSeFvnLw6/X8AvD8vw6cDAAA=';^
    $data = [System.Convert]::FromBase64String($PAYLOAD);$ms = New-Object System.IO.MemoryStream;$ms.Write($data, 0, $data.Length);$ms.Seek(0,0) ^| Out-Null;$cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress);$sr = New-Object System.IO.StreamReader($cs);$t = $sr.readtoend();Invoke-Expression $t;\"" /f
    
    

    http://forums.mydigitallife.net/attachment.php?attachmentid=32401&stc=1
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    12
    10
    Thanks a lot.