I created the following .bat file to clear the prefetch and temp folders of Windows 10:

cd %systemroot%\Prefetch
del /q /s *.*
cd %temp%
del /q /s *.*

Now, on my system, running the file did exactly what it's supposed to, no matter from where I executed it. However, on a different system (also Win10), executing the script from the desktop deleted the contents of everything in the desktop folder (C:\Users\\Desktop).

Right now I'm completely clueless how this is possible. I would be thankful for any explanations. Also, I assume there's no efficient way to restore the data deleted that way?

Thank you.

EDIT: I realize that running the script from the Desktop when the first folder doesn't exist calls the delete on the desktop directory, however the folders do exist.

  • 1
    Right click the batch script and run as administrator.... Also consider using del /q /s %systemroot%\Prefetch\*.* and maybe adding some conditional logic to ensure the cd is set in the right folder before the del command. – Pimp Juice IT Feb 10 at 17:09

There are a few possibilities here. %temp% could be setup to actually be the users's desktop folder. Another reason could be that on the first command, the user does not have permission and as such doesn't change the path.

If the script is being run from the desktop, it will not change folder and as such it deletes those files.

If you want to protect your script from this kind of problem, build in an IF statement to check if %cd% is the same as the folder you actually want.

Also, note that changing the directory with cd will not also change the drive unless you append /d. Here's what it looks like without the /d and with the /d.


D:\>cd c:\temp


C:\temp>cd /d d:\games


As you can see, without /d it changes the directory but not the drive. You can type d: and c: to switch, but with /d in a script you are always certain the drive is changed too.

That said, your script would look like this:

@echo off

cd /d %systemroot%\Prefetch

IF %cd%==%systemroot%\Prefetch (
    del /q /s *.*
) ELSE (
    echo "The script was unable to switch to the folder %systemroot%\Prefetch."

cd /d %temp%

IF %cd%==%temp%  (
    del /q /s *.*
) ELSE (
    echo "The script was unable to switch to the folder %temp%."
  • Thank you, permissions must have been the issue.. – Josh Feb 10 at 17:53
  • You should always use cd /d ... to ensure batch files work as expected on systems with multiple drives. – DavidPostill Feb 10 at 21:34
  • @DavidPostill good point. I've edited the answer to include a section explaining about the /d parameter. :) – LPChip Feb 10 at 22:07

You should note that the cd command only changes the directory, it doesn't change the current drive. While Unix has only one current directory, DOS/Windows has one current directory for each drive. So if your initial directory and the target directory are on different drives, you will get exactly that effect.

C:\Users\name\Desktop>cd D:\WINDOWS\Prefetch
C:\Users\name\Desktop>del /q /s *.*

After the first command the current drive is still C:, while the current directory of drive D: was changed to D:\WINDOWS\Prefetch, therefor the second command will delete C:\Users\name\Desktop, not D:\WINDOWS\Prefetch.

The command may also fail because the directory doesn't exist.

A simple solution is to not split the command in two parts, instead use a single command:

del /q /s %systemroot%\Prefetch\*.*
del /q /s %temp%\*.*

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.