Announcement

Collapse
No announcement yet.

Deleting the files and directories of an directory without deleting this root directo

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting the files and directories of an directory without deleting this root directo

    Hi

    With the Command Prompt in Windows 8.x, which command should I use to delete all files and all subdirectories of a directory without deleting this root directory?

    Thanks

    Bye
    balubeto

  • #2
    Re: Deleting the files and directories of an directory without deleting this root dir

    Click HERE.
    1 1 was a racehorse.
    2 2 was 1 2.
    1 1 1 1 race 1 day,
    2 2 1 1 2

    Comment


    • #3
      Re: Deleting the files and directories of an directory without deleting this root dir

      Is it really that hard?
      1 1 was a racehorse.
      2 2 was 1 2.
      1 1 1 1 race 1 day,
      2 2 1 1 2

      Comment


      • #4
        Re: Deleting the files and directories of an directory without deleting this root dir

        Originally posted by balubeto View Post
        Hi

        With the Command Prompt in Windows 8.x, which command should I use to delete all files and all subdirectories of a directory without deleting this root directory?

        Thanks

        Bye
        Same way as in Windows 7.x, 5.x and even earlier

        Tom Jones
        MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
        PhD, MSc, FIAP, MIITT
        IT Trainer / Consultant
        Ossian Ltd
        Scotland

        ** Remember to give credit where credit is due and leave reputation points where appropriate **

        Comment


        • #5
          Re: Deleting the files and directories of an directory without deleting this root dir

          I have tried in this way:

          Code:
           del /f /s /q "<directory_path>\*"
          but it does not work because all files are deleted but its nested subdirectories are not deleted. Why?

          Thanks

          Bye
          balubeto

          Comment


          • #6
            Re: Deleting the files and directories of an directory without deleting this root dir

            Originally posted by balubeto View Post
            I have tried in this way:

            Code:
             del /f /s /q "<directory_path>\*"
            but it does not work because all files are deleted but its nested subdirectories are not deleted. Why?
            Because that's what you told it to do. del will only delete files, never directories:

            del /s directory\* = "delete files matching the wildcard * in directory and any subdirectories"

            To delete directories, empty or otherwise, try this instead: for /d %a in (<directory_path>\*) do rd /s /q "%a"

            The rd command doesn't accept wildcards, so a for loop is necessary. Remember to use double percentage signs (%%a instead of %a) if you use for loops in a batch file.

            Of course, rd won't delete any files from the root directory, so an additional del /f /q <directory_path>\* will be needed to get rid of those files as well.

            Comment


            • #7
              The simplest way of doing it:

              cd "C:\Users\abrewer\Desktop\cache"
              rd /s /q .

              An error message will be shown when it fails to delete the parent directory, but otherwise it will work perfectly.

              Alternatively, something like:

              cd "C:\Users\abrewer\Desktop\cache"
              del * /q
              for /D %%i in (*) do rd /s /q "%%i"

              then this might work. Remember to use only single percent signs if you're running from the command line rather than in a batch file.

              Comment

              Working...
              X