MS-DOS command to delete all files except one












25















Is there an MS-DOS command that allows me to delete all files except one?



Consider as an example the following files:



a.001  
a.002
a.003
a.exe
a.c


Is there a command to delete all files except a.c?










share|improve this question















migrated from stackoverflow.com Feb 22 '10 at 19:19


This question came from our site for professional and enthusiast programmers.























    25















    Is there an MS-DOS command that allows me to delete all files except one?



    Consider as an example the following files:



    a.001  
    a.002
    a.003
    a.exe
    a.c


    Is there a command to delete all files except a.c?










    share|improve this question















    migrated from stackoverflow.com Feb 22 '10 at 19:19


    This question came from our site for professional and enthusiast programmers.





















      25












      25








      25


      5






      Is there an MS-DOS command that allows me to delete all files except one?



      Consider as an example the following files:



      a.001  
      a.002
      a.003
      a.exe
      a.c


      Is there a command to delete all files except a.c?










      share|improve this question
















      Is there an MS-DOS command that allows me to delete all files except one?



      Consider as an example the following files:



      a.001  
      a.002
      a.003
      a.exe
      a.c


      Is there a command to delete all files except a.c?







      ms-dos






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 7 '11 at 2:12









      3498DB

      15.7k114762




      15.7k114762










      asked Feb 22 '10 at 19:14









      nunosnunos

      2212821




      2212821




      migrated from stackoverflow.com Feb 22 '10 at 19:19


      This question came from our site for professional and enthusiast programmers.









      migrated from stackoverflow.com Feb 22 '10 at 19:19


      This question came from our site for professional and enthusiast programmers.
























          6 Answers
          6






          active

          oldest

          votes


















          34














          You can use the for and if commands to accomplish this:



          for %i in (*) do if not "%~i" == a.c del "%~i"


          This goes through the current directory, and compares each file name to a.c. If it doesn't match, the file is deleted.






          share|improve this answer


























          • +1 never would of thought of using this for a simple delete... Loads of good answers here!

            – William Hilsum
            Feb 22 '10 at 19:37






          • 4





            @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

            – Kevin
            Feb 23 '10 at 2:12






          • 4





            @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

            – Kevin
            Feb 2 '11 at 16:02








          • 1





            Better use del "%i"

            – Mugen
            Oct 20 '16 at 5:06






          • 1





            If you need the IF statement to be case insensitive, change it to IF /I.

            – jep
            Sep 20 '18 at 15:11



















          14














          You could set the file to read only before deleting everything



          attrib +r a.c
          del *.*
          attrib -r a.c





          share|improve this answer































            8














            No, there isn't. I'd make a directory, copy the important file into it, erase ., and move the file back. Then delete the temp file.



            mkdir temp
            move a.c temp
            erase *.*
            move temp* .
            rmdir temp





            share|improve this answer



















            • 1





              +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

              – William Hilsum
              Feb 22 '10 at 19:31





















            3














            FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f





            share|improve this answer



















            • 1





              what benefit are the [ ] ?

              – barlop
              Sep 7 '11 at 1:39











            • @barlop: %f could have spaces in the filename.

              – paradroid
              Sep 7 '11 at 3:12











            • @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

              – barlop
              Sep 7 '11 at 13:03













            • @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

              – paradroid
              Sep 7 '11 at 13:09











            • @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

              – barlop
              Sep 7 '11 at 13:35





















            1














            FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d





            share|improve this answer


























            • I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

              – LarsH
              Feb 10 '16 at 0:19



















            0














            For speed, I use delen:



            delen /! a.c


            TCC/LE also has a more powerful del command:



            del /[!a.c] *





            share|improve this answer
























            • del /[!a.c] * yelds Invalid switch - "[!a.c]".

              – karlphillip
              Jan 5 '12 at 20:08











            • @karlphillip: It works fine, "in TCC/LE".

              – paradroid
              Jan 6 '12 at 9:14











            • Aha! Didn't see that comment before, thanks bro.

              – karlphillip
              Jan 6 '12 at 11:50











            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f112136%2fms-dos-command-to-delete-all-files-except-one%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            34














            You can use the for and if commands to accomplish this:



            for %i in (*) do if not "%~i" == a.c del "%~i"


            This goes through the current directory, and compares each file name to a.c. If it doesn't match, the file is deleted.






            share|improve this answer


























            • +1 never would of thought of using this for a simple delete... Loads of good answers here!

              – William Hilsum
              Feb 22 '10 at 19:37






            • 4





              @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

              – Kevin
              Feb 23 '10 at 2:12






            • 4





              @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

              – Kevin
              Feb 2 '11 at 16:02








            • 1





              Better use del "%i"

              – Mugen
              Oct 20 '16 at 5:06






            • 1





              If you need the IF statement to be case insensitive, change it to IF /I.

              – jep
              Sep 20 '18 at 15:11
















            34














            You can use the for and if commands to accomplish this:



            for %i in (*) do if not "%~i" == a.c del "%~i"


            This goes through the current directory, and compares each file name to a.c. If it doesn't match, the file is deleted.






            share|improve this answer


























            • +1 never would of thought of using this for a simple delete... Loads of good answers here!

              – William Hilsum
              Feb 22 '10 at 19:37






            • 4





              @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

              – Kevin
              Feb 23 '10 at 2:12






            • 4





              @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

              – Kevin
              Feb 2 '11 at 16:02








            • 1





              Better use del "%i"

              – Mugen
              Oct 20 '16 at 5:06






            • 1





              If you need the IF statement to be case insensitive, change it to IF /I.

              – jep
              Sep 20 '18 at 15:11














            34












            34








            34







            You can use the for and if commands to accomplish this:



            for %i in (*) do if not "%~i" == a.c del "%~i"


            This goes through the current directory, and compares each file name to a.c. If it doesn't match, the file is deleted.






            share|improve this answer















            You can use the for and if commands to accomplish this:



            for %i in (*) do if not "%~i" == a.c del "%~i"


            This goes through the current directory, and compares each file name to a.c. If it doesn't match, the file is deleted.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 9 at 17:06









            rautamiekka

            135312




            135312










            answered Feb 22 '10 at 19:29









            KevinKevin

            2,5861129




            2,5861129













            • +1 never would of thought of using this for a simple delete... Loads of good answers here!

              – William Hilsum
              Feb 22 '10 at 19:37






            • 4





              @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

              – Kevin
              Feb 23 '10 at 2:12






            • 4





              @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

              – Kevin
              Feb 2 '11 at 16:02








            • 1





              Better use del "%i"

              – Mugen
              Oct 20 '16 at 5:06






            • 1





              If you need the IF statement to be case insensitive, change it to IF /I.

              – jep
              Sep 20 '18 at 15:11



















            • +1 never would of thought of using this for a simple delete... Loads of good answers here!

              – William Hilsum
              Feb 22 '10 at 19:37






            • 4





              @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

              – Kevin
              Feb 23 '10 at 2:12






            • 4





              @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

              – Kevin
              Feb 2 '11 at 16:02








            • 1





              Better use del "%i"

              – Mugen
              Oct 20 '16 at 5:06






            • 1





              If you need the IF statement to be case insensitive, change it to IF /I.

              – jep
              Sep 20 '18 at 15:11

















            +1 never would of thought of using this for a simple delete... Loads of good answers here!

            – William Hilsum
            Feb 22 '10 at 19:37





            +1 never would of thought of using this for a simple delete... Loads of good answers here!

            – William Hilsum
            Feb 22 '10 at 19:37




            4




            4





            @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

            – Kevin
            Feb 23 '10 at 2:12





            @Wil, when you start using for regularly you come up with all kinds of crazy scenarios for it. :)

            – Kevin
            Feb 23 '10 at 2:12




            4




            4





            @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

            – Kevin
            Feb 2 '11 at 16:02







            @Mike, you want something like "for %i in (A:SomePath*) do if not %~nxi == a.c del %i" Note that the path ends in *, to get the files in that folder, and that the comparison is against %~nxi, the name with no path. For destructive for loops like this, it's a good idea to do "for ... do echo %i" to see what files will be affected before running the "for ... do if ... del %i" command.

            – Kevin
            Feb 2 '11 at 16:02






            1




            1





            Better use del "%i"

            – Mugen
            Oct 20 '16 at 5:06





            Better use del "%i"

            – Mugen
            Oct 20 '16 at 5:06




            1




            1





            If you need the IF statement to be case insensitive, change it to IF /I.

            – jep
            Sep 20 '18 at 15:11





            If you need the IF statement to be case insensitive, change it to IF /I.

            – jep
            Sep 20 '18 at 15:11













            14














            You could set the file to read only before deleting everything



            attrib +r a.c
            del *.*
            attrib -r a.c





            share|improve this answer




























              14














              You could set the file to read only before deleting everything



              attrib +r a.c
              del *.*
              attrib -r a.c





              share|improve this answer


























                14












                14








                14







                You could set the file to read only before deleting everything



                attrib +r a.c
                del *.*
                attrib -r a.c





                share|improve this answer













                You could set the file to read only before deleting everything



                attrib +r a.c
                del *.*
                attrib -r a.c






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 22 '10 at 19:21









                feiht thieffeiht thief

                24115




                24115























                    8














                    No, there isn't. I'd make a directory, copy the important file into it, erase ., and move the file back. Then delete the temp file.



                    mkdir temp
                    move a.c temp
                    erase *.*
                    move temp* .
                    rmdir temp





                    share|improve this answer



















                    • 1





                      +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

                      – William Hilsum
                      Feb 22 '10 at 19:31


















                    8














                    No, there isn't. I'd make a directory, copy the important file into it, erase ., and move the file back. Then delete the temp file.



                    mkdir temp
                    move a.c temp
                    erase *.*
                    move temp* .
                    rmdir temp





                    share|improve this answer



















                    • 1





                      +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

                      – William Hilsum
                      Feb 22 '10 at 19:31
















                    8












                    8








                    8







                    No, there isn't. I'd make a directory, copy the important file into it, erase ., and move the file back. Then delete the temp file.



                    mkdir temp
                    move a.c temp
                    erase *.*
                    move temp* .
                    rmdir temp





                    share|improve this answer













                    No, there isn't. I'd make a directory, copy the important file into it, erase ., and move the file back. Then delete the temp file.



                    mkdir temp
                    move a.c temp
                    erase *.*
                    move temp* .
                    rmdir temp






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 22 '10 at 19:18









                    David PfefferDavid Pfeffer

                    3622415




                    3622415








                    • 1





                      +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

                      – William Hilsum
                      Feb 22 '10 at 19:31
















                    • 1





                      +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

                      – William Hilsum
                      Feb 22 '10 at 19:31










                    1




                    1





                    +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

                    – William Hilsum
                    Feb 22 '10 at 19:31







                    +1 crude and technically @Feiht thief says a better way of doing it (so also gave +1 there), but for speed, this is the way I would do it.

                    – William Hilsum
                    Feb 22 '10 at 19:31













                    3














                    FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f





                    share|improve this answer



















                    • 1





                      what benefit are the [ ] ?

                      – barlop
                      Sep 7 '11 at 1:39











                    • @barlop: %f could have spaces in the filename.

                      – paradroid
                      Sep 7 '11 at 3:12











                    • @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

                      – barlop
                      Sep 7 '11 at 13:03













                    • @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

                      – paradroid
                      Sep 7 '11 at 13:09











                    • @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

                      – barlop
                      Sep 7 '11 at 13:35


















                    3














                    FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f





                    share|improve this answer



















                    • 1





                      what benefit are the [ ] ?

                      – barlop
                      Sep 7 '11 at 1:39











                    • @barlop: %f could have spaces in the filename.

                      – paradroid
                      Sep 7 '11 at 3:12











                    • @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

                      – barlop
                      Sep 7 '11 at 13:03













                    • @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

                      – paradroid
                      Sep 7 '11 at 13:09











                    • @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

                      – barlop
                      Sep 7 '11 at 13:35
















                    3












                    3








                    3







                    FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f





                    share|improve this answer













                    FOR %f IN (*.*) DO IF NOT [%f]==[a.c] DEL /Q %f






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 22 '10 at 19:31









                    Carlos GutiérrezCarlos Gutiérrez

                    248149




                    248149








                    • 1





                      what benefit are the [ ] ?

                      – barlop
                      Sep 7 '11 at 1:39











                    • @barlop: %f could have spaces in the filename.

                      – paradroid
                      Sep 7 '11 at 3:12











                    • @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

                      – barlop
                      Sep 7 '11 at 13:03













                    • @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

                      – paradroid
                      Sep 7 '11 at 13:09











                    • @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

                      – barlop
                      Sep 7 '11 at 13:35
















                    • 1





                      what benefit are the [ ] ?

                      – barlop
                      Sep 7 '11 at 1:39











                    • @barlop: %f could have spaces in the filename.

                      – paradroid
                      Sep 7 '11 at 3:12











                    • @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

                      – barlop
                      Sep 7 '11 at 13:03













                    • @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

                      – paradroid
                      Sep 7 '11 at 13:09











                    • @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

                      – barlop
                      Sep 7 '11 at 13:35










                    1




                    1





                    what benefit are the [ ] ?

                    – barlop
                    Sep 7 '11 at 1:39





                    what benefit are the [ ] ?

                    – barlop
                    Sep 7 '11 at 1:39













                    @barlop: %f could have spaces in the filename.

                    – paradroid
                    Sep 7 '11 at 3:12





                    @barlop: %f could have spaces in the filename.

                    – paradroid
                    Sep 7 '11 at 3:12













                    @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

                    – barlop
                    Sep 7 '11 at 13:03







                    @paradroid no that won't help for that if [4 r]==[4 r] echo equal I've seen similar done by some, like with . instead of . I could guess at why, but i'd rather hear it from somebody that does it.

                    – barlop
                    Sep 7 '11 at 13:03















                    @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

                    – paradroid
                    Sep 7 '11 at 13:09





                    @barlop: I have no idea what you just typed there, but the square brackets work just like quote marks would.

                    – paradroid
                    Sep 7 '11 at 13:09













                    @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

                    – barlop
                    Sep 7 '11 at 13:35







                    @paradroid C:>if [4 r]==[4 r] echo abc <ENTER> gives result "r]==[4 was unexpected at this time." Whereas C:>if "4 r"=="4 r" echo abc gives result abc. So what makes you think they in any way work like quote marks would? Even C:>if [a b]==[a b] echo abc <-- does not work gives similar error. b]==[a was unexpected at this time. So what makes you think they work like quotes?

                    – barlop
                    Sep 7 '11 at 13:35













                    1














                    FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d





                    share|improve this answer


























                    • I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

                      – LarsH
                      Feb 10 '16 at 0:19
















                    1














                    FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d





                    share|improve this answer


























                    • I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

                      – LarsH
                      Feb 10 '16 at 0:19














                    1












                    1








                    1







                    FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d





                    share|improve this answer















                    FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %app_path%^|find /v "%file%"') DO Del /q %%a %%b %%c %%d






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 6 '11 at 23:37









                    Simon Sheehan

                    7,632124268




                    7,632124268










                    answered Sep 6 '11 at 23:31









                    DarthDarth

                    111




                    111













                    • I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

                      – LarsH
                      Feb 10 '16 at 0:19



















                    • I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

                      – LarsH
                      Feb 10 '16 at 0:19

















                    I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

                    – LarsH
                    Feb 10 '16 at 0:19





                    I think this would probably be helpful, if it were explained what it does. It looks like it traverses a whole folder tree, which is what I want. %app_path% and %file% are the root of the tree to traverse, and the file to avoid deleting, respectively. What is the ^, and why are we passing four tokens per file to the Del command?

                    – LarsH
                    Feb 10 '16 at 0:19











                    0














                    For speed, I use delen:



                    delen /! a.c


                    TCC/LE also has a more powerful del command:



                    del /[!a.c] *





                    share|improve this answer
























                    • del /[!a.c] * yelds Invalid switch - "[!a.c]".

                      – karlphillip
                      Jan 5 '12 at 20:08











                    • @karlphillip: It works fine, "in TCC/LE".

                      – paradroid
                      Jan 6 '12 at 9:14











                    • Aha! Didn't see that comment before, thanks bro.

                      – karlphillip
                      Jan 6 '12 at 11:50
















                    0














                    For speed, I use delen:



                    delen /! a.c


                    TCC/LE also has a more powerful del command:



                    del /[!a.c] *





                    share|improve this answer
























                    • del /[!a.c] * yelds Invalid switch - "[!a.c]".

                      – karlphillip
                      Jan 5 '12 at 20:08











                    • @karlphillip: It works fine, "in TCC/LE".

                      – paradroid
                      Jan 6 '12 at 9:14











                    • Aha! Didn't see that comment before, thanks bro.

                      – karlphillip
                      Jan 6 '12 at 11:50














                    0












                    0








                    0







                    For speed, I use delen:



                    delen /! a.c


                    TCC/LE also has a more powerful del command:



                    del /[!a.c] *





                    share|improve this answer













                    For speed, I use delen:



                    delen /! a.c


                    TCC/LE also has a more powerful del command:



                    del /[!a.c] *






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 7 '11 at 3:05









                    paradroidparadroid

                    19.2k958100




                    19.2k958100













                    • del /[!a.c] * yelds Invalid switch - "[!a.c]".

                      – karlphillip
                      Jan 5 '12 at 20:08











                    • @karlphillip: It works fine, "in TCC/LE".

                      – paradroid
                      Jan 6 '12 at 9:14











                    • Aha! Didn't see that comment before, thanks bro.

                      – karlphillip
                      Jan 6 '12 at 11:50



















                    • del /[!a.c] * yelds Invalid switch - "[!a.c]".

                      – karlphillip
                      Jan 5 '12 at 20:08











                    • @karlphillip: It works fine, "in TCC/LE".

                      – paradroid
                      Jan 6 '12 at 9:14











                    • Aha! Didn't see that comment before, thanks bro.

                      – karlphillip
                      Jan 6 '12 at 11:50

















                    del /[!a.c] * yelds Invalid switch - "[!a.c]".

                    – karlphillip
                    Jan 5 '12 at 20:08





                    del /[!a.c] * yelds Invalid switch - "[!a.c]".

                    – karlphillip
                    Jan 5 '12 at 20:08













                    @karlphillip: It works fine, "in TCC/LE".

                    – paradroid
                    Jan 6 '12 at 9:14





                    @karlphillip: It works fine, "in TCC/LE".

                    – paradroid
                    Jan 6 '12 at 9:14













                    Aha! Didn't see that comment before, thanks bro.

                    – karlphillip
                    Jan 6 '12 at 11:50





                    Aha! Didn't see that comment before, thanks bro.

                    – karlphillip
                    Jan 6 '12 at 11:50


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f112136%2fms-dos-command-to-delete-all-files-except-one%23new-answer', 'question_page');
                    }
                    );

                    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







                    Popular posts from this blog

                    How do I know what Microsoft account the skydrive app is syncing to?

                    When does type information flow backwards in C++?

                    Grease: Live!