Hello,

I'm trying to create a batch file that will delete & remake temporary directories, but it isn't working. There are two directories that I want to do this two, and the second one doesn't work. here is the code:

@echo off
rd c:\windows\TEMP /s /q
md C:\windows\TEMP

rd C:\Documents and Settings\christ\Local Settings\Temp /s /q
md C:\Documents and Settings\christ\Local Settings\Temp

Are the spaces in the directory the problem? If so, how do i fix it?

Thanks,
Chris

Dani AI

Generated

A few practical notes that solve both the “spaces in the path” symptom and the “locked/recurring BITxx files” problem.

As showed, deleting temp folders can fail for two reasons: the path contains spaces and some files are held open by running processes. was right that quoting paths fixes the space issue; a more robust approach is to use environment variables so the script works for any account and Windows version, and to delete the contents of the Temp folders (files + subfolders) rather than removing and remaking the folder itself.

Example batch (save as .bat and run elevated):

@echo off
setlocal

echo Clearing user temp: "%TEMP%"
del /f /s /q "%TEMP%\*" >nul 2>&1
for /d %%D in ("%TEMP%\*") do rd /s /q "%%D" >nul 2>&1

echo Clearing system temp: "%windir%\Temp"
del /f /s /q "%windir%\Temp\*" >nul 2>&1
for /d %%D in ("%windir%\Temp\*") do rd /s /q "%%D" >nul 2>&1

endlocal

For : recurring or undeletable BITxx files mean some process is creating or holding them. Troubleshooting steps, in order:

  • Run Process Explorer (Sysinternals) as admin, press Ctrl+F and search for the filename/prefix to find which process holds the handle; close the handle or stop the process.
  • Use Process Monitor (ProcMon) to watch CreateFile events in the Temp folder to identify the writer.
  • If a handle cannot be closed, delete in Safe Mode or schedule removal at boot (files locked by drivers/services are often removable before they start).
  • Temporary culprits: antivirus, background updaters, browser plugins, or a misbehaving app that keeps re-creating temp files.

Cautions: always run the script with appropriate privileges, test on a non-critical account first, and avoid deleting other users’ Temp folders while they’re logged on. If unsure which process is responsible, ProcMon + Process Explorer will find the source reliably.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

yes, use quotes

This will work as long as the TEMP dir is empty or it is not locked by another process.
Any solution for such scenario? I have "BITxx" files (where xx are random digits) that keep multiplying and growing in the TEMP dir. I can delete ALL of them except for ONE. Once I do that, I see more files being recreated. Please advise.

Hello,

I'm trying to create a batch file that will delete & remake temporary directories, but it isn't working. There are two directories that I want to do this two, and the second one doesn't work. here is the code:

@echo off
rd c:\windows\TEMP /s /q
md C:\windows\TEMP

rd C:\Documents and Settings\christ\Local Settings\Temp /s /q
md C:\Documents and Settings\christ\Local Settings\Temp

Are the spaces in the directory the problem? If so, how do i fix it?

Thanks,
Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.