Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: delete unused folder and it subfolders  (Read 9148 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    Re: delete unused folder and it subfolders
    « Reply #15 on: February 08, 2019, 02:21:45 AM »


    Please try this test script. It won't do anything to your files. It will create a file called agedays.vbs in the same folder as the batch. Put the batch in the top folder. See above, I put it in the top folder called test. For the test run there is a folder called Jerry with no logfiles.txt.

    Tell me if it gives results.

    @echo off
    setlocal enabledelayedexpansion
    set maxdays=60
    echo Max file age %maxdays% days
    set "thisfolder=%cd%"
    echo wscript.echo DateDiff("d",wscript.arguments(0), Now) > agedays.vbs
    for /f "delims=" %%A in ('dir /b /ad') do (
       echo.
       set delfolder=NO
       echo check folder %%A
       if not exist "%%A\logfiles" echo NO LOGFILES FOLDER & set delfolder=YES
       cd "%%A"
       If exist "logfiles" (
          cd "logfiles"
          for /f "delims=" %%B in ('dir LOG*.txt /b /a-d /od /tc') do (
             set thisfile=%%B
             set thisdate=%%~tB
             for /f "delims=" %%C in ('cscript //nologo "%thisfolder%\agedays.vbs" "!thisdate!"') do set fileage=%%C
             )
             echo newest file !thisdate! !thisfile! !fileage! days
             if !fileage! gtr %maxdays% set delfolder=YES
             )
          if "!delfolder!"=="YES" echo DELETE FOLDER %%~dpnA & echo.
          cd /d "%thisfolder%"
       )


    This script don't know from where it takes date and time because I noticed that it doesn't take newest file.
    I make screenshot
    And also Sonny logfiles folder is empty and is not marked for delete.



    Max file age 60 days

    check folder George
    newest file 07.11.2018 20:51 LOGL_201902pe_082651.txt 93 days
    DELETE FOLDER D:\test\George


    check folder Jack
    newest file 01.02.2019 08:46 LOGL_201902pe_093433.txt 7 days

    check folder Jana
    newest file 15.01.2019 19:48 LOGL_201902pe_093433.txt 24 days

    check folder Joe
    newest file 02.02.2019 10:08 LOGL_201902pe_094150.txt 6 days

    check folder Miha
    newest file 03.02.2019 16:49 LOGL_201902pe_083034.txt 5 days

    check folder Silva
    newest file 24.01.2019 09:50 LOGL_201902pe_083034.txt 15 days

    check folder Sonny
    File Not Found
    newest file 24.01.2019 09:50 LOGL_201902pe_083034.txt 15 days




    Salmon Trout

    • Guest
    Re: delete unused folder and it subfolders
    « Reply #16 on: February 08, 2019, 09:57:04 AM »
    This script don't know from where it takes date and time because I noticed that it doesn't take newest file.

    You wrote:

    Quote from: Blisk
    these files are created and never modified, so it is file creation date.

    Please check the file dates carefully. Are you looking at creation date?

    DIR help:

    C:\>dir /?
    Displays a list of files and subdirectories in a directory.

    DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
      [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

      [drive:][path][filename]
                  Specifies drive, directory, and/or files to list.

      /A          Displays files with specified attributes.
      attributes   D  Directories                R  Read-only files
                   H  Hidden files               A  Files ready for archiving
                   S  System files               I  Not content indexed files
                   L  Reparse Points             O  Offline files
                   -  Prefix meaning not
      /B          Uses bare format (no heading information or summary).
      /C          Display the thousand separator in file sizes.  This is the
                  default.  Use /-C to disable display of separator.
      /D          Same as wide but files are list sorted by column.
      /L          Uses lowercase.
      /N          New long list format where filenames are on the far right.
      /O          List by files in sorted order.
      sortorder    N  By name (alphabetic)       S  By size (smallest first)
                   E  By extension (alphabetic)  D  By date/time (oldest first)
                   G  Group directories first    -  Prefix to reverse order
      /P          Pauses after each screenful of information.
      /Q          Display the owner of the file.
      /R          Display alternate data streams of the file.
      /S          Displays files in specified directory and all subdirectories.
      /T          Controls which time field displayed or used for sorting
      timefield   C  Creation
                  A  Last Access
                  W  Last Written

      /W          Uses wide list format.
      /X          This displays the short names generated for non-8dot3 file
                  names.  The format is that of /N with the short name inserted
                  before the long name. If no short name is present, blanks are
                  displayed in its place.
      /4          Displays four-digit years

    Switches may be preset in the DIRCMD environment variable.  Override
    preset switches by prefixing any switch with - (hyphen)--for example, /-W.
    [/tt]

    In my script:

    for /f "delims=" %%B in ('dir LOG*.txt /b /a-d /od /tc') do (

    This lists the LOG*.txt files, oldest first, newest last, in each subfolder called "logfiles" and takes the last one (the newest).

    That script was a TEST for you to tell me what it did. It does not yet have the feature to delete folder with no LOG*.txt.



    Salmon Trout

    • Guest
    Re: delete unused folder and it subfolders
    « Reply #17 on: February 08, 2019, 10:50:13 AM »
    Possibly you want the last-written date?

    E:\test\Filedate>dir /tw /od
     Volume in drive E is SATA3-01
     Volume Serial Number is 452C-48AE

     Directory of E:\test\Filedate

    12/03/2001  15:35               291 Install.bat
    26/01/2003  15:24               496 fotovak.bat
    08/07/2005  19:57               161 urlget.bat
    30/10/2009  20:45               515 foldersize2.bat
    30/01/2013  15:24                50 isfolder.bat
    26/06/2015  19:21               344 macriumList0002.bat
    14/01/2018  20:23               583 test1.bat
    20/09/2018  23:04               688 hybrid2.bat
    04/11/2018  09:56             1,474 test999.bat
    03/02/2019  12:44               135 process00001.bat
    08/02/2019  17:35    <DIR>          .
    08/02/2019  17:35    <DIR>          ..
                  10 File(s)          4,737 bytes
                   2 Dir(s)  623,047,106,560 bytes free

    E:\test\Filedate>for /f %A in ('dir /b /tw /od') do @echo %A
    Install.bat
    fotovak.bat
    urlget.bat
    foldersize2.bat
    isfolder.bat
    macriumList0002.bat
    test1.bat
    hybrid2.bat
    test999.bat
    process00001.bat

    E:\test\Filedate>for /f %A in ('dir /b /tw /od') do @echo %~tA %A
    12/03/2001 15:35 Install.bat
    26/01/2003 15:24 fotovak.bat
    08/07/2005 19:57 urlget.bat
    30/10/2009 20:45 foldersize2.bat
    30/01/2013 15:24 isfolder.bat
    26/06/2015 19:21 macriumList0002.bat
    14/01/2018 20:23 test1.bat
    20/09/2018 23:04 hybrid2.bat
    04/11/2018 09:56 test999.bat
    03/02/2019 12:44 process00001.bat

    E:\test\Filedate>for /f %A in ('dir /b /tw /od') do @set ldate=%~tA

    E:\test\Filedate>echo %ldate%
    03/02/2019 12:44

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: delete unused folder and it subfolders
      « Reply #18 on: February 08, 2019, 11:29:19 AM »
      Possibly you want the last-written date?

      E:\test\Filedate>dir /tw /od
       Volume in drive E is SATA3-01
       Volume Serial Number is 452C-48AE

       Directory of E:\test\Filedate

      12/03/2001  15:35               291 Install.bat
      26/01/2003  15:24               496 fotovak.bat
      08/07/2005  19:57               161 urlget.bat
      30/10/2009  20:45               515 foldersize2.bat
      30/01/2013  15:24                50 isfolder.bat
      26/06/2015  19:21               344 macriumList0002.bat
      14/01/2018  20:23               583 test1.bat
      20/09/2018  23:04               688 hybrid2.bat
      04/11/2018  09:56             1,474 test999.bat
      03/02/2019  12:44               135 process00001.bat
      08/02/2019  17:35    <DIR>          .
      08/02/2019  17:35    <DIR>          ..
                    10 File(s)          4,737 bytes
                     2 Dir(s)  623,047,106,560 bytes free

      E:\test\Filedate>for /f %A in ('dir /b /tw /od') do @echo %A
      Install.bat
      fotovak.bat
      urlget.bat
      foldersize2.bat
      isfolder.bat
      macriumList0002.bat
      test1.bat
      hybrid2.bat
      test999.bat
      process00001.bat

      E:\test\Filedate>for /f %A in ('dir /b /tw /od') do @echo %~tA %A
      12/03/2001 15:35 Install.bat
      26/01/2003 15:24 fotovak.bat
      08/07/2005 19:57 urlget.bat
      30/10/2009 20:45 foldersize2.bat
      30/01/2013 15:24 isfolder.bat
      26/06/2015 19:21 macriumList0002.bat
      14/01/2018 20:23 test1.bat
      20/09/2018 23:04 hybrid2.bat
      04/11/2018 09:56 test999.bat
      03/02/2019 12:44 process00001.bat

      E:\test\Filedate>for /f %A in ('dir /b /tw /od') do @set ldate=%~tA

      E:\test\Filedate>echo %ldate%
      03/02/2019 12:44


      yes this is the right thing.
      like your process00001.bat is the latest file, which should be cecked by script.
      I also tested it on my files in dir logfiles and I get the same result and this is ok.

      Quote
      these files are created and never modified, so it is file creation date.
      This is my fault because I have changed files for test and yes your script work as it should.
      Quote
      That script was a TEST for you to tell me what it did. It does not yet have the feature to delete folder with no LOG*.txt.

      I know it is just a test. But I have tested it on my PC where I have a test enviroment setup for testing this script, because I already try to make a script but failed to do so. That's why I didn't tested on my real files. sorry.

      Blisk

        Topic Starter


        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: delete unused folder and it subfolders
        « Reply #19 on: February 08, 2019, 11:33:46 AM »
        delete

        Salmon Trout

        • Guest
        Re: delete unused folder and it subfolders
        « Reply #20 on: February 08, 2019, 11:38:14 AM »
        If it is finding the dates correctly then I will do the next step.

        Blisk

          Topic Starter


          Intermediate

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: delete unused folder and it subfolders
          « Reply #21 on: February 08, 2019, 11:40:52 AM »
          If it is finding the dates correctly then I will do the next step.

          yes it is.
          only thing is logfiles folder always exist but sometime is empty
          your script assume it does not exist, it does but when it is empty than top folder (joe, miha, sonny...) is also marked for delete.

          Salmon Trout

          • Guest
          Re: delete unused folder and it subfolders
          « Reply #22 on: February 08, 2019, 12:37:45 PM »
          only thing is logfiles folder always exist but sometime is empty
          your script assume it does not exist, it does but when it is empty than top folder (joe, miha, sonny...) is also marked for delete.
          My script does not assume logfiles folder does not exist. The script checks if it exists. If it does not, top folder is marked for deletion.

          Now script will delete top folder if ANY of these is true:

          1. logfiles folder does not exist.

          2. logfiles folder exists, but does not have any LOG*.txt files. (Will delete top folder if other files exist)

          3. logfiles folder exists, has one or more LOG*.txt files, and newest is older than age limit (days).

          Please tell me if this is correct.










          Blisk

            Topic Starter


            Intermediate

            Thanked: 1
            • Experience: Familiar
            • OS: Windows 7
            Re: delete unused folder and it subfolders
            « Reply #23 on: February 09, 2019, 01:53:27 AM »
            My script does not assume logfiles folder does not exist. The script checks if it exists. If it does not, top folder is marked for deletion.

            Now script will delete top folder if ANY of these is true:

            1. logfiles folder does not exist.

            2. logfiles folder exists, but does not have any LOG*.txt files. (Will delete top folder if other files exist)

            3. logfiles folder exists, has one or more LOG*.txt files, and newest is older than age limit (days).

            Please tell me if this is correct.

            this is correct. exactly what I need.

            Salmon Trout

            • Guest
            Re: delete unused folder and it subfolders
            « Reply #24 on: February 09, 2019, 03:48:55 AM »
            Please try this on a COPY of your data!!! Now it can optionally ask for confirmation of each folder delete and it counts folder deletes

            @echo off
            setlocal enabledelayedexpansion

            REM change this to 0 for confirm mode
            set quietmode=1

            REM change this to set max file age (days)
            set maxdays=60

            if "%quietmode%"=="1" (
               set delcommand=rd /s /q
            ) else (
               set delcommand=rd /s
            )

            echo Max file age %maxdays% days
            set "thisfolder=%cd%"
            set foldersdeleted=0
            echo wscript.echo DateDiff("d",wscript.arguments(0), Now) > agedays.vbs
            for /f "delims=" %%A in ('dir /b /ad') do (
               set delfolder=NO
               echo check folder %%A
               if not exist "%%A\logfiles" echo NO LOGFILES FOLDER & set delfolder=YES
               cd "%%A"
               If exist "logfiles" (
                  cd "logfiles"
                  if not exist "LOG*.txt" echo NO LOG*.TXT & set delfolder=YES
                  if exist "LOG*.txt" (
                     for /f "delims=" %%B in ('dir LOG*.txt /b /a-d /od /tc') do (
                        set thisfile=%%B
                        set thisdate=%%~tB
                        for /f "delims=" %%C in ('cscript //nologo "%thisfolder%\agedays.vbs" "!thisdate!"') do set fileage=%%C
                        )
                        echo newest file !thisdate! !thisfile! !fileage! days
                        if !fileage! gtr %maxdays% set delfolder=YES
                  )
               )
               if "!delfolder!"=="YES" (
                  echo DELETE FOLDER %%~dpnA
                  cd /d "%thisfolder%"
                  %delcommand% "%%~dpnA" && echo *** Deleted *** && set /a foldersdeleted+=1
               )
               cd /d "%thisfolder%"
            )
            echo Script ended
            echo Folders deleted: %foldersdeleted%
            echo Press any key to exit
            pause > nul

            Blisk

              Topic Starter


              Intermediate

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Re: delete unused folder and it subfolders
              « Reply #25 on: February 09, 2019, 08:54:14 AM »
              Thank you, you are really great man, what can I do to repay you?
              Script works great!

              Salmon Trout

              • Guest
              Re: delete unused folder and it subfolders
              « Reply #26 on: February 09, 2019, 09:07:15 AM »
              All is good! Glad it worked!

              patio

              • Moderator


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
              Re: delete unused folder and it subfolders
              « Reply #27 on: February 09, 2019, 09:20:22 AM »
              Nice Job ! !
              " Anyone who goes to a psychiatrist should have his head examined. "

              Blisk

                Topic Starter


                Intermediate

                Thanked: 1
                • Experience: Familiar
                • OS: Windows 7
                Re: delete unused folder and it subfolders
                « Reply #28 on: February 09, 2019, 09:28:34 AM »

                Blisk

                  Topic Starter


                  Intermediate

                  Thanked: 1
                  • Experience: Familiar
                  • OS: Windows 7
                  Re: delete unused folder and it subfolders
                  « Reply #29 on: February 09, 2019, 09:32:14 AM »
                  All is good! Glad it worked!

                  I get an idea of one more script after this - for my job, I willi pay for that, but you have blocked PM.
                  should I open another topic or you are tired of me bothering you?