1

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 . . .
Mofi
  • 46,139
  • 17
  • 80
  • 143
Wolvee Youtube
  • 11
  • 1
  • 1
  • 2
  • Open a command prompt window and run `set user` or just `set` and look on the output environment variables with their values. You mixed `USERNAME` with `USERPROFILE`. `USERPROFILE` contains already `C:\Users` and therefore `C:\Users\%USERPROFILE%` is simply wrong. `%TEMP%` would be more accurate here. – Mofi Mar 15 '22 at 15:37

1 Answers1

0

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143