Computer Hope
Microsoft => Microsoft DOS => Topic started by: Blisk on February 02, 2019, 03:06:19 AM
-
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 logfiles? Yes
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
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?
-
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?
-
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?
-
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.
-
(https://images2.imgbox.com/86/fe/ZSA2NUnl_o.jpg)
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 ? ? ?
-
(https://images2.imgbox.com/86/fe/ZSA2NUnl_o.jpg)
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
-
This script don't know from where it takes date and time because I noticed that it doesn't take newest file.
You wrote:
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.
-
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
-
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.
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.
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.
-
delete
-
If it is finding the dates correctly then I will do the next step.
-
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.
-
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.
-
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.
-
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
-
Thank you, you are really great man, what can I do to repay you?
Script works great!
-
All is good! Glad it worked!
-
Nice Job ! !
-
Nice Job ! !
genious job
-
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?
-
Start a new topic and I will look at it, and so can everybody else.
-
I only get PMs from administrators. I disabled PMs for everyone else exactly because I kept getting "write me a script" requests. I work hard during the day at a busy legal office, and sometimes I find it relaxing to write a quick script in the evening. That last one I did used up maybe 2 hours in total. I will not write, for free, scripts that are meant for work purposes. I won't write them for money either. I already have a job. When you write a script that someone is using for a work or business related purpose, they are going to hassle you night and day! I once wrote a VBscript for someone who had an eBay business. It was to print mailing labels from an Excel spreadsheet, and he was always emailing me for improvements.
-
If I was you I do the same. There is so many people which wants to abuse your good will to help.
I do the same but on other fileds. I am lame about programming and scripts and still learning. I hope I have knowledge like you at least 20%.
Do what you feel it is right anything elseis a burden.
I am really gratefull for all what someone do for me for free and I am trying to "pay back" as much I can if not to the person which helped than to someone which needs help. Because I believe good is returning with good.
And that makes wordl differend - better
thank you
-
Make your new question. Let's see it.
-
today this script deleted all folders and files and it should not.
I don't know what happend because log files was todays log files in all folders.
??? ??? ???