1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

batch file to delete the oldest folders, please help

Discussion in 'DOS/Other' started by Amino, Nov 18, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    hi. i need a batch file that can check if there are more than 2 folders in specified directory and if so, delete only the oldest one and check again. the task i'm trying to solve is keep at least 2 backups, even if they are obsolete.
    so far i've seen examples that remove only files older than x days, but that's not exactly the case here. thanks in advance.
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Code:
    for /f "skip=2 tokens=*" %%A in ('dir /ad /b /o-d') do rmdir /s /q %%A
     
  3. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    Thank you very much, it does exactly what i need. while testing i found little problem with folder names containing spaces but easily corrected by adding double quotes to the last variable like this:
    Code:
    for /f "skip=2 tokens=*" %%A in ('dir /ad /b /o-d') do rmdir /s /q "%%A"
     
  4. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    I found problem passing the parameter with specified directory in which i want this script to run. y default it runs in user homedir and using CD or CHDIR is not an option. is it possible without those commands? i tried calling it with parameter like
    Code:
    script.bat c:\needed_dir
    and changed %%A to %%1, but it didn't succeed.
     
  5. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Two options.
    1. Use PUSHD before the For Loop
    2. Change the DIR command in the For loop to use the command line input.
     
  6. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    rmdir doesn't understand relative path and tries to delete found oldest folder in home dir.
    now i'm trying to run this:
    Code:
    for /f "skip=2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q %%A
     
  7. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    You need to use PUSHD before the for loop.

    If you were using the /S switch with the DIR command and traversing sub directories then you wouldn't have to use pushd.
     
  8. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    now my boss wants to pass number of skipped folders as a parameter along with target directory. i tried using SET but did not succeed. how to do it correctly?
    current version is
    Code:
    for /f "skip=2 tokens=*" %%A in ('dir %1 /s /ad /b /o-d') do rmdir /s /q "%%A"
     
  9. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    managed myself. misused %, now works correctly.
     
  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    If your problem is solved it is common courtesy to post your final code and then use the Thread Tools to mark your problem solved.
     
  11. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    found a problem with subfolders. if they exist inside folders i want to delete they are deleted instead of folders containing them. i figured this is from the key /s in dir command, but without it rmdir somehow doesn't get full path, only correct folder name and tries to delete this folder from where the script is run, which is obviously not there. and i'm NOT allowed to use CD. how to pass full path to rmdir instead of only name?
    current version:
    Code:
    for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%%A"
     
  12. Amino

    Amino Thread Starter

    Joined:
    Nov 18, 2011
    Messages:
    8
    again, managed myself. inserted path from %1 to rmdir's %%A:
    Code:
    for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%1\%%A"
    script is safe to run without any number of parameters so misclick will not destroy your data.
     
  13. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Like I said earlier. Use the PUSHD command before your FOR loop.

    While I guess you could technically say the script is safe to run without parameters I never rely on that thought process. I make my scripts as bullet proof as possible.

    Code:
    If "%~1"=="" Goto _Usage
    If "%~2"=="" Goto _Usage
    for /f "skip=%2 tokens=*" %%A in ('dir %1 /ad /b /o-d') do rmdir /s /q "%1\%%A"
    :_Usage
    Echo.This batch file requires two command line parameters.
    Echo.The first parameter is the directory path.
    Echo.The second parameter is the number of directories to skip
    I would even do a test on the second parameter to make sure it is a number and not the directory path in case they put one before the other. A lot of programming is about validating the user input.
     
  14. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Another issue you are going to have is if some one needs to check a directory path with a space in it.
     
  15. mares88

    mares88

    Joined:
    Dec 12, 2011
    Messages:
    1
    Hi can anyone help me with similar script / with modification of this one ? It is supposed to do the same {delete the oldest folder } but I need 5 folders to be kept ... not sure why but i cant figure out how to :-]
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - batch file delete
  1. niedernsill
    Replies:
    3
    Views:
    426
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1027343

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice