Batch file behaved unexpectedly on Win10
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.
windows-10 batch batch-file
add a comment |
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.
windows-10 batch batch-file
1
Right click the batch script and run as administrator.... Also consider usingdel /q /s %systemroot%Prefetch*.*
and maybe adding some conditional logic to ensure thecd
is set in the right folder before thedel
command.
– Pimp Juice IT
Feb 10 at 17:09
add a comment |
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.
windows-10 batch batch-file
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.
windows-10 batch batch-file
windows-10 batch batch-file
edited Feb 10 at 17:09
Josh
asked Feb 10 at 16:59
JoshJosh
32
32
1
Right click the batch script and run as administrator.... Also consider usingdel /q /s %systemroot%Prefetch*.*
and maybe adding some conditional logic to ensure thecd
is set in the right folder before thedel
command.
– Pimp Juice IT
Feb 10 at 17:09
add a comment |
1
Right click the batch script and run as administrator.... Also consider usingdel /q /s %systemroot%Prefetch*.*
and maybe adding some conditional logic to ensure thecd
is set in the right folder before thedel
command.
– Pimp Juice IT
Feb 10 at 17:09
1
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
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
add a comment |
2 Answers
2
active
oldest
votes
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.
C:>d:
D:>cd c:temp
D:>c:
C:temp>cd /d d:games
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 usecd /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
add a comment |
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:UsersnameDesktop>cd D:WINDOWSPrefetch
C:UsersnameDesktop>del /q /s *.*
After the first command the current drive is still C:
, while the current directory of drive D:
was changed to D:WINDOWSPrefetch
, therefor the second command will delete C:UsersnameDesktop
, not D:WINDOWSPrefetch
.
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%*.*
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1404185%2fbatch-file-behaved-unexpectedly-on-win10%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
C:>d:
D:>cd c:temp
D:>c:
C:temp>cd /d d:games
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 usecd /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
add a comment |
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.
C:>d:
D:>cd c:temp
D:>c:
C:temp>cd /d d:games
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 usecd /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
add a comment |
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.
C:>d:
D:>cd c:temp
D:>c:
C:temp>cd /d d:games
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%."
)
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.
C:>d:
D:>cd c:temp
D:>c:
C:temp>cd /d d:games
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%."
)
edited Feb 10 at 22:06
answered Feb 10 at 17:09
LPChipLPChip
36.3k55487
36.3k55487
Thank you, permissions must have been the issue..
– Josh
Feb 10 at 17:53
You should always usecd /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
add a comment |
Thank you, permissions must have been the issue..
– Josh
Feb 10 at 17:53
You should always usecd /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
Thank you, permissions must have been the issue..
– Josh
Feb 10 at 17:53
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
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
@DavidPostill good point. I've edited the answer to include a section explaining about the /d parameter. :)
– LPChip
Feb 10 at 22:07
add a comment |
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:UsersnameDesktop>cd D:WINDOWSPrefetch
C:UsersnameDesktop>del /q /s *.*
After the first command the current drive is still C:
, while the current directory of drive D:
was changed to D:WINDOWSPrefetch
, therefor the second command will delete C:UsersnameDesktop
, not D:WINDOWSPrefetch
.
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%*.*
add a comment |
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:UsersnameDesktop>cd D:WINDOWSPrefetch
C:UsersnameDesktop>del /q /s *.*
After the first command the current drive is still C:
, while the current directory of drive D:
was changed to D:WINDOWSPrefetch
, therefor the second command will delete C:UsersnameDesktop
, not D:WINDOWSPrefetch
.
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%*.*
add a comment |
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:UsersnameDesktop>cd D:WINDOWSPrefetch
C:UsersnameDesktop>del /q /s *.*
After the first command the current drive is still C:
, while the current directory of drive D:
was changed to D:WINDOWSPrefetch
, therefor the second command will delete C:UsersnameDesktop
, not D:WINDOWSPrefetch
.
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%*.*
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:UsersnameDesktop>cd D:WINDOWSPrefetch
C:UsersnameDesktop>del /q /s *.*
After the first command the current drive is still C:
, while the current directory of drive D:
was changed to D:WINDOWSPrefetch
, therefor the second command will delete C:UsersnameDesktop
, not D:WINDOWSPrefetch
.
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%*.*
answered Feb 10 at 17:24
RalfFriedlRalfFriedl
1,224247
1,224247
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1404185%2fbatch-file-behaved-unexpectedly-on-win10%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 thecd
is set in the right folder before thedel
command.– Pimp Juice IT
Feb 10 at 17:09