Windows command to delete all files except the specified list of files which may contain spaces in their...
I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
windows batch-file cmd
|
show 2 more comments
I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
windows batch-file cmd
Is DOS a requirement? Powershell or even WSL will make your life much easier.
– kabanus
Nov 15 '18 at 17:59
Are you really still using "DOS" or are you referring to the command line in Windows (which has nothing to do with MS-DOS)
– a_horse_with_no_name
Nov 15 '18 at 18:03
@kabanus I am referring to command line in windows. Even I can use Powershell if required.
– MIM
Nov 15 '18 at 18:09
@a_horse_with_no_name well, it does share many of the basic commands. Not sure the fact it is just a look alike matters to the specific question.
– kabanus
Nov 15 '18 at 18:15
@kabanus: From thedostag info: "DO NOT USE THIS TAG FOR QUESTIONS ABOUT THE WINDOWS COMMAND PROMPT!" (theforloop shown in the question wouldn't even run in "DOS")
– a_horse_with_no_name
Nov 15 '18 at 18:17
|
show 2 more comments
I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
windows batch-file cmd
I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
windows batch-file cmd
windows batch-file cmd
edited Nov 15 '18 at 22:12
Mark Setchell
91.4k781184
91.4k781184
asked Nov 15 '18 at 17:38
MIMMIM
1441619
1441619
Is DOS a requirement? Powershell or even WSL will make your life much easier.
– kabanus
Nov 15 '18 at 17:59
Are you really still using "DOS" or are you referring to the command line in Windows (which has nothing to do with MS-DOS)
– a_horse_with_no_name
Nov 15 '18 at 18:03
@kabanus I am referring to command line in windows. Even I can use Powershell if required.
– MIM
Nov 15 '18 at 18:09
@a_horse_with_no_name well, it does share many of the basic commands. Not sure the fact it is just a look alike matters to the specific question.
– kabanus
Nov 15 '18 at 18:15
@kabanus: From thedostag info: "DO NOT USE THIS TAG FOR QUESTIONS ABOUT THE WINDOWS COMMAND PROMPT!" (theforloop shown in the question wouldn't even run in "DOS")
– a_horse_with_no_name
Nov 15 '18 at 18:17
|
show 2 more comments
Is DOS a requirement? Powershell or even WSL will make your life much easier.
– kabanus
Nov 15 '18 at 17:59
Are you really still using "DOS" or are you referring to the command line in Windows (which has nothing to do with MS-DOS)
– a_horse_with_no_name
Nov 15 '18 at 18:03
@kabanus I am referring to command line in windows. Even I can use Powershell if required.
– MIM
Nov 15 '18 at 18:09
@a_horse_with_no_name well, it does share many of the basic commands. Not sure the fact it is just a look alike matters to the specific question.
– kabanus
Nov 15 '18 at 18:15
@kabanus: From thedostag info: "DO NOT USE THIS TAG FOR QUESTIONS ABOUT THE WINDOWS COMMAND PROMPT!" (theforloop shown in the question wouldn't even run in "DOS")
– a_horse_with_no_name
Nov 15 '18 at 18:17
Is DOS a requirement? Powershell or even WSL will make your life much easier.
– kabanus
Nov 15 '18 at 17:59
Is DOS a requirement? Powershell or even WSL will make your life much easier.
– kabanus
Nov 15 '18 at 17:59
Are you really still using "DOS" or are you referring to the command line in Windows (which has nothing to do with MS-DOS)
– a_horse_with_no_name
Nov 15 '18 at 18:03
Are you really still using "DOS" or are you referring to the command line in Windows (which has nothing to do with MS-DOS)
– a_horse_with_no_name
Nov 15 '18 at 18:03
@kabanus I am referring to command line in windows. Even I can use Powershell if required.
– MIM
Nov 15 '18 at 18:09
@kabanus I am referring to command line in windows. Even I can use Powershell if required.
– MIM
Nov 15 '18 at 18:09
@a_horse_with_no_name well, it does share many of the basic commands. Not sure the fact it is just a look alike matters to the specific question.
– kabanus
Nov 15 '18 at 18:15
@a_horse_with_no_name well, it does share many of the basic commands. Not sure the fact it is just a look alike matters to the specific question.
– kabanus
Nov 15 '18 at 18:15
@kabanus: From the
dos tag info: "DO NOT USE THIS TAG FOR QUESTIONS ABOUT THE WINDOWS COMMAND PROMPT!" (the for loop shown in the question wouldn't even run in "DOS")– a_horse_with_no_name
Nov 15 '18 at 18:17
@kabanus: From the
dos tag info: "DO NOT USE THIS TAG FOR QUESTIONS ABOUT THE WINDOWS COMMAND PROMPT!" (the for loop shown in the question wouldn't even run in "DOS")– a_horse_with_no_name
Nov 15 '18 at 18:17
|
show 2 more comments
1 Answer
1
active
oldest
votes
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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%2fstackoverflow.com%2fquestions%2f53325084%2fwindows-command-to-delete-all-files-except-the-specified-list-of-files-which-may%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
add a comment |
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
add a comment |
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
answered Nov 15 '18 at 19:08
SquashmanSquashman
8,99831933
8,99831933
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
add a comment |
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
The 1st option worked for me. I have not tried 2nd option as this requires separate file creation.
– MIM
Nov 16 '18 at 6:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53325084%2fwindows-command-to-delete-all-files-except-the-specified-list-of-files-which-may%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
Is DOS a requirement? Powershell or even WSL will make your life much easier.
– kabanus
Nov 15 '18 at 17:59
Are you really still using "DOS" or are you referring to the command line in Windows (which has nothing to do with MS-DOS)
– a_horse_with_no_name
Nov 15 '18 at 18:03
@kabanus I am referring to command line in windows. Even I can use Powershell if required.
– MIM
Nov 15 '18 at 18:09
@a_horse_with_no_name well, it does share many of the basic commands. Not sure the fact it is just a look alike matters to the specific question.
– kabanus
Nov 15 '18 at 18:15
@kabanus: From the
dostag info: "DO NOT USE THIS TAG FOR QUESTIONS ABOUT THE WINDOWS COMMAND PROMPT!" (theforloop shown in the question wouldn't even run in "DOS")– a_horse_with_no_name
Nov 15 '18 at 18:17