Questions

DOS - How to delete all files and subdirectories in a directory.

+
0 Votes
Locked

DOS - How to delete all files and subdirectories in a directory.

CharlieSpencer
Okay, not really DOS, but from a Windows Server command prompt.

I'm trying to remember how to delete all files, subdirectories, and subdirectory contents from a specific directory from a command prompt. We have a "catch all" folder for short-term file sharing and I want to delete the contents nightly, but not the folder itself, using a scheduled command procedure.

Unfortunately I'm the poster boy for the American CRS Foundation and have forgotten how to do this. RMDIR won't take a wildcard. DELTREE is apparently dead and gone. I'm currently using

DEL /F /Q /S C:\Directory\*.*

but that leaves the empty subdirs in place.

Help. Thanks.
  • +
    1 Votes
    scott_heath

    Not really, but it sounded like a good idea.

    Anyway, type "rd <path> /s /q"

    Enjoy the nachos.

    +
    0 Votes
    CharlieSpencer

    The parent folder is used for temporary storage for files too big for e-mail attachments, holding scanned images before they're entered into our document management system, etc. Subdirectories with various names are created throughout the day. Those names are not the same from one day to the next, so I can't predict what the path will be. One day it may be

    C:\Directory\Sub1

    the next day

    C:\Directory\SubdirectoryA

    then

    C:\Directory\SubDirGreen\SubSubDirOrange

    I want to leave C:\Directory each night but lose the subs.

    +
    0 Votes
    scott_heath

    But I'm going to offer it anyway!!!

    REM ***** My Crazy Batch File
    dir *. /b > deleteme.txt
    for /f %%a in (deleteme.txt) do (
    rd %%a
    )
    del deleteme.txt
    REM ***** Muhahahahahaha

    So anyway, as long as the directory doesn't have a .anything on the end of it your good. I realize this is stupid, but trust me I can get stoopiderer. I can show you a vbscript that uses the folders collection of the Scripting.FileSystemObject to do this as well. Or we can get totally crunked and use WMI to do this!!!

    P.S. - I know I used the word "crunked" wrong, but I've been dying to use it.

    P.P.S - I think I've had to much sugar today.

    P.P.P.S - Eat nachos.

    +
    2 Votes
    Tig2

    I cheated. I Googled. I am also a CRS poster child...


    Remove a Directory and All Subdirectories

    SUMMARY: Use these Windows XP and DOS command line parameters with the RMDIR command to remove all subdirectories.

    The Windows XP DOS command RMDIR removes a directory from your system and has the following syntax:

    RMDIR directory_name

    For example, to remove directory C:\BLAH, enter:

    RMDIR C:\BLAH

    Note that you cannot be inside the directory that you are deleting; else you will be prompted with the message "The process cannot access the file because it is being used by another process".

    Normally the RMDIR command will not remove a directory if it is empty. However, if you add the /S parameter, all files and directories underneath the specified directory will be removed. USE WITH CAUTION! You will be prompted with an "Are you sure" question that lets you back out in case you entered the wrong directory name.

    If you need to perform this task in a batch file, you can add the /Q parameter to the end. This will delete the specified directory, all files, and all subdirectories, WITHOUT ANY WARNING.

    For example, the following command will remove directory C:\blah and all subdirectories and files contained therein. No prompt will be displayed.

    RMDIR c:\blah /s /q

    If you wish you can use RD as an abbreviation for RMDIR.

    +
    0 Votes
    CharlieSpencer

    The subdirectory names change daily.

    +
    0 Votes
    IC-IT

    to the end of the script.

    rd c:\directory\ /s /q
    md c:\directory

    +
    0 Votes
    CharlieSpencer

    How do I assign the correct security settings to the new directory? I'm talking NT security, not share security. I'm looking through the help for the NET command, but that doesn't seem to be it.

    +
    0 Votes
    IC-IT

    Add that and tweak to the needed security settings.

    +
    0 Votes
    ThumbsUp2

    Is it possible to create another level of subdir that you can delete nightly?

    For example, you have c:\directory which you want to leave and just delete what's under it, and that changes on a daily basis. Can you create c:\directory\temp and have everyone put their "stuff" inside of temp which can be deleted nightly and recreated new every day? You could then use an if statement to see if temp exists, if it does, deltree c:\directory\temp *.* ... then an if statement to check that it does not exist and if not, recreate it new.

    +
    0 Votes
    CharlieSpencer

    That's similar to his suggestion, but there's still the issue of how to recreate the security settings.

    Also, the downside to this is all of our network scanners are programmed to use C:\Directory. If I get desperate, I could reset them to C:\Directory\Temp. And your way the security might not be an issue since it would pick up the settings from the parent C:\Directory. The problem then becomes one of educating users to use \Temp and not \Directory; I don't know if they'll bother, and can't see how to enforce it. There's no way to lock them out of \Directory without having different security settings than \Temp, and then we're back where we started.

    +
    0 Votes
    rahouseholder

    I have had to copy old external DOS commands to use on 2000 and XP before. They still work, you just have to find them. If you have a PC running 98 or a 98 CD, just copy deltree to the server you need to use it on. You can also find copies on the internet available for download. I urge caution when downloading. Use a good virus scanner.

    +
    1 Votes
    aeriform

    Put the following in a batch file. If you're running this from the command line, use single '%' characters rather than '%%' for batch files.

    DEL /F /Q /S C:\Directory\*
    for /D %%i in ("C:\Directory\*") do RD /S /Q "%%i"

  • +
    1 Votes
    scott_heath

    Not really, but it sounded like a good idea.

    Anyway, type "rd <path> /s /q"

    Enjoy the nachos.

    +
    0 Votes
    CharlieSpencer

    The parent folder is used for temporary storage for files too big for e-mail attachments, holding scanned images before they're entered into our document management system, etc. Subdirectories with various names are created throughout the day. Those names are not the same from one day to the next, so I can't predict what the path will be. One day it may be

    C:\Directory\Sub1

    the next day

    C:\Directory\SubdirectoryA

    then

    C:\Directory\SubDirGreen\SubSubDirOrange

    I want to leave C:\Directory each night but lose the subs.

    +
    0 Votes
    scott_heath

    But I'm going to offer it anyway!!!

    REM ***** My Crazy Batch File
    dir *. /b > deleteme.txt
    for /f %%a in (deleteme.txt) do (
    rd %%a
    )
    del deleteme.txt
    REM ***** Muhahahahahaha

    So anyway, as long as the directory doesn't have a .anything on the end of it your good. I realize this is stupid, but trust me I can get stoopiderer. I can show you a vbscript that uses the folders collection of the Scripting.FileSystemObject to do this as well. Or we can get totally crunked and use WMI to do this!!!

    P.S. - I know I used the word "crunked" wrong, but I've been dying to use it.

    P.P.S - I think I've had to much sugar today.

    P.P.P.S - Eat nachos.

    +
    2 Votes
    Tig2

    I cheated. I Googled. I am also a CRS poster child...


    Remove a Directory and All Subdirectories

    SUMMARY: Use these Windows XP and DOS command line parameters with the RMDIR command to remove all subdirectories.

    The Windows XP DOS command RMDIR removes a directory from your system and has the following syntax:

    RMDIR directory_name

    For example, to remove directory C:\BLAH, enter:

    RMDIR C:\BLAH

    Note that you cannot be inside the directory that you are deleting; else you will be prompted with the message "The process cannot access the file because it is being used by another process".

    Normally the RMDIR command will not remove a directory if it is empty. However, if you add the /S parameter, all files and directories underneath the specified directory will be removed. USE WITH CAUTION! You will be prompted with an "Are you sure" question that lets you back out in case you entered the wrong directory name.

    If you need to perform this task in a batch file, you can add the /Q parameter to the end. This will delete the specified directory, all files, and all subdirectories, WITHOUT ANY WARNING.

    For example, the following command will remove directory C:\blah and all subdirectories and files contained therein. No prompt will be displayed.

    RMDIR c:\blah /s /q

    If you wish you can use RD as an abbreviation for RMDIR.

    +
    0 Votes
    CharlieSpencer

    The subdirectory names change daily.

    +
    0 Votes
    IC-IT

    to the end of the script.

    rd c:\directory\ /s /q
    md c:\directory

    +
    0 Votes
    CharlieSpencer

    How do I assign the correct security settings to the new directory? I'm talking NT security, not share security. I'm looking through the help for the NET command, but that doesn't seem to be it.

    +
    0 Votes
    IC-IT

    Add that and tweak to the needed security settings.

    +
    0 Votes
    ThumbsUp2

    Is it possible to create another level of subdir that you can delete nightly?

    For example, you have c:\directory which you want to leave and just delete what's under it, and that changes on a daily basis. Can you create c:\directory\temp and have everyone put their "stuff" inside of temp which can be deleted nightly and recreated new every day? You could then use an if statement to see if temp exists, if it does, deltree c:\directory\temp *.* ... then an if statement to check that it does not exist and if not, recreate it new.

    +
    0 Votes
    CharlieSpencer

    That's similar to his suggestion, but there's still the issue of how to recreate the security settings.

    Also, the downside to this is all of our network scanners are programmed to use C:\Directory. If I get desperate, I could reset them to C:\Directory\Temp. And your way the security might not be an issue since it would pick up the settings from the parent C:\Directory. The problem then becomes one of educating users to use \Temp and not \Directory; I don't know if they'll bother, and can't see how to enforce it. There's no way to lock them out of \Directory without having different security settings than \Temp, and then we're back where we started.

    +
    0 Votes
    rahouseholder

    I have had to copy old external DOS commands to use on 2000 and XP before. They still work, you just have to find them. If you have a PC running 98 or a 98 CD, just copy deltree to the server you need to use it on. You can also find copies on the internet available for download. I urge caution when downloading. Use a good virus scanner.

    +
    1 Votes
    aeriform

    Put the following in a batch file. If you're running this from the command line, use single '%' characters rather than '%%' for batch files.

    DEL /F /Q /S C:\Directory\*
    for /D %%i in ("C:\Directory\*") do RD /S /Q "%%i"