1.

Solve : delete unused folder and it subfolders?

Answer»

I have trouble to search for unused folders and delete it.
Idea is to search for a log file in subfolder and if it is older than 2 months or that log file is MISSING than top folder and all under it should be deleted.
Here on picture we can see how it looks and Joe have log file from 2018 which is older than 2 months so folder Joe must be deleted and all under it.
Until george and Jack must stay.
Here are only 3 for example in real I have 100 folders like that.Have anyone some idea how to do it with batch?still nothing, nobody can't help me?  The problem is not stated clearly.

(1) What does 'older than 2 months' mean. For example today is 6 February 2019. Do you mean

created (or modified?):

before 6 December 2018?
more than 60 days ago?
before January?
something ELSE?

You said '2018 is older than 2 months ago'. 5 December 2018 is less than 2 months ago by one meaning of 'ago'.

(2) Is the file to be checked always in a subfolder 1 down from the top of each tree (George, Jack, Joe etc)  called logfiles?

(3) Is it always called logfile.txt?

Extra question.... can you run .vbs scripts on the system?

Hi. Thank you for asking.
What does 'older than 2 months' mean
It means older than 2 months (60 days for example), it means all what is older than 60 days.
15 December 2018 is not older than 60 days (regarding today so that folder will stay. This is not so important even if file is 62 days old or 55 days old doesn't matter. It is just somekind of milestone how old it is to delete top folder.
Is the file to be checked always in a subfolder 1 down from the top of each tree (George, Jack, Joe etc)  called logfilesYes
Is it always called logfile.txt? it is like this LOGL_201902sr_200756.txt FIRST part of file is the same, I write fev examples of that below. Each folder contains about 200 of these files.
only thing here with LOGL_201902sr_200756.txt is that sometimes it can happend that this file is missing ( I mean all that files are missing), it is not present in folder in this case top folder should be deleted too.
In real life this file logfile.txt  are many files in logfiles folder like this below.
LOGL_201902sr_200756.txt
LOGL_201902sr_184156.txt
LOGL_201902sr_183141.txt
LOGL_201902sr_162522.txt
LOGL_201902sr_150501.txt


Quote from: Salmon Trout on February 06, 2019, 01:03:27 PM

The problem is not stated clearly.

(1) What does 'older than 2 months' mean. For example today is 6 February 2019. Do you mean

created (or modified?):

before 6 December 2018?
more than 60 days ago?
before January?
something else?

You said '2018 is older than 2 months ago'. 5 December 2018 is less than 2 months ago by one meaning of 'ago'.

(2) Is the file to be checked always in a subfolder 1 down from the top of each tree (George, Jack, Joe etc)  called logfiles?

(3) Is it always called logfile.txt?

Extra question.... can you run .vbs scripts on the system?
I forget to say these files are about 80 of them differend date and time.
So script should find the latest one and if that file is older than 60 days than top folder and all under it should be deleted and also if there is none of that files in folder, it should delete top folder and all under it.
LOGL_201902sr_200756.txt
LOGL_201902sr_184156.txt
LOGL_201902sr_183141.txt
LOGL_201902sr_162522.txt
LOGL_201902sr_150501.txt
Is this for your job? Quote from: Salmon Trout on February 07, 2019, 09:51:54 AM
Is this for your job?
no this is private related, why?Sometimes we get "write me a script please" requests on here, and I am pretty sure some of them are for people who need it for their work, so we write the script and the OP gets paid for it.

Anyhow, do you want to use the file creation date or last modified date?

Quote from: Salmon Trout on February 07, 2019, 11:16:45 AM
Sometimes we get "write me a script please" requests on here, and I am pretty sure some of them are for people who need it for their work, so we write the script and the OP gets paid for it.

Anyhow, do you want to use the file creation date or last modified date?

What a crap of people making money on people good will to help. I glady pay for a COFFE to anyone which helps me, but never think of selling this kind of someones else job. On my job I also never get paid for any scripts.

Anyhow, do you want to use the file creation date or last modified date?
You mean of these files? LOGL_201902sr_200756.txt
these files are created and never modified, so it is file creation date.1. Can you run vbs scripts?

2. What should happen if logfiles subfolder is not there?



Quote from: Salmon Trout on February 07, 2019, 11:38:34 AM
1. Can you run vbs scripts?

2. What should happen if logfiles subfolder is not there?

if it is possible I rather run batch, because if I need I can change a bit as much my knowledge allow me.

If there is no subfolder or file in it than also it should be deleted.I just want to use an external vbscript function to calculate the file age in days, because there is no native way of doing that in batch. You will see when I show you a test batch script in a short while.


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%"
   )
 

My output:

Max file age 60 days

check folder Jack
newest file 17/01/2019 20:21 LOGL_201902sr45341M.txt 21 days

check folder Jane
newest file 03/02/2019 12:39 LOGL_201902sr19909M.txt 4 days

check folder Jerry
NO LOGFILES FOLDER
DELETE FOLDER E:\test\Jerry


check folder Jim
newest file 03/02/2019 12:39 LOGL_201902sr23088M.txt 4 days

check folder Joe
newest file 13/11/2002 17:27 LOGL_201902sr35804M.txt 5930 days
DELETE FOLDER E:\test\Joe


check folder John
newest file 03/02/2019 12:39 LOGL_201902sr16730M.txt 4 days Coffee ? ?...that's it ? ? ?


Discussion

No Comment Found