Batch to intelligently delete files and folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Batch to intelligently delete files and folders

#1 Post by jbseventeen » 13 Nov 2014 19:08

I'm struggling to create a batch script that performs two basic functions for files and folders in a specific directory:

1. If C:\Logs\*.* [Modified-Date]>=7 days
1a. rename *.* to *-%date%.* #where %date% is the files [Modified Date] + 30 days.
1b. move renamed file to C:\Old-Logs\
2. If C:\Old-Logs\*.* [Modified-Date]>=30 days
2a. delete the file or folder

e.g.
C:\Logs\Log.txt with a Modified Date of 01.01.2015
1a. Should be renamed on 07.01.2015 to "Log-30.01.2015.txt"
1b. Should be moved to C:\Old-Logs\ after (1a)
2a. Should be deleted on 30.01.2015

I'm finding it hard to learn how to use for and forfiles together to do this effectively. Some help would be greatly appreciated!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to intelligently delete files and folders

#2 Post by foxidrive » 14 Nov 2014 05:29

I hesitate to start on this task without clarification about why you are operating on files marked 2015,
and why +30 days is useful, because it is going to put the date in the future as well.

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#3 Post by jbseventeen » 14 Nov 2014 09:21

It was just an example to keep it from being confusing. Talk about FAIL lol.

In reality, today's files and folders should be renamed and moved in 7 days and then deleted in 30 days, tomorrow's in 8 / 31 days from today and so on.

The script would run once every day during the scheduled shutdown/restart, looking for files to rename/move after 7 days since modification, and also for files to delete after 30 days without modification.

The files/folders are renamed to point to the date of deletion as a reminder to users.

Hope this clears things up!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to intelligently delete files and folders

#4 Post by foxidrive » 14 Nov 2014 13:47

jbseventeen wrote:The files/folders are renamed to point to the date of deletion as a reminder to users.


I don't know what the above means.

You better start again and give actual details about the real task,
as over the years we've learned that fake details about a task leads to long threads about why the script fails,
and what the error messages are and how the script behaves, and the script usually has to be written again when the real details are revealed.

If you want help to write the script yourself then you can ask pointed questions about the code.

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#5 Post by jbseventeen » 15 Nov 2014 00:21

My experience on most forums has been "put things simply or don't get answered" but fair enough. I can see how the details are important here.

I maintain several PC's that are run by other users. Pretty much all of these users maintain their files poorly and 'abandon' unnecessary files in several locations.

Now every time a machine needs to be reformatted (most of the time due to virus infections that are all too frequent), a backup folder is created to contain all of the old files, wherther necessary or not, from pertinent locations. This has resulted in several backups within backups within backups and so on, that are mostly filled with unneeded files.

It has got to the point that most of these machines now require 10GB to 40+GB just for "backups". Most of these machines only have 80GB hard drives that are unlikely to be replaced unless they fail so space is quite important. This is where the script would help by keeping unneeded files from making it to the backup phase.

The script would run on each machine, in the area(s) identified as "work" folders (mostly but not always desktop and my documents). The script should:

1. Check "\Desktop\" for files or folders that have not been modified in 7 days (excluding shortcuts and the "Desktop\Marked-for-deletion\" folder)
1a. Create a sub folder within "\Desktop\" called "\Desktop\Marked-for-deletion\" if that folder does not already exist.
1b. Rename the files that meet the criterion to "filename-del-on-DD-MM-YYYY.extension"
1c. Move them to "\Desktop\Marked-for-deletion\", and finally
2. Delete files from the "\Desktop\Marked-for-deletion\" folder after 30 days.
(NOTE: If excluding the marked-for-deletion folder itself from the script is not possible, it could always be placed elsewhere)

The machines are scheduled to shut down at various times every day which is why I would run the script at shutdown. I would modify only the locations on the script based on where the user likes to "work".

To be honest, after going through several tutorials I still haven't a clue on what the actual code should look like. If you were willing to write the script for me, I'd greatly appreciate it but if not, a basic outline of the script is all I'm looking for.
Or just some help with how to append -del-on-DD-MM-YYYY to the end of a file or folder name, where DD-MM-YYYY is the modified date + 30 days.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to intelligently delete files and folders

#6 Post by foxidrive » 15 Nov 2014 07:27

I haven't tested this: the code only echos commands and information to the screen at the moment.

This line is where the file search folder should occur:

pushd "%userprofile%\desktop" && (

and this line is the folder where the files should end up:

set "target=%userprofile%\desktop\Marked-for-deletion"


The moved files are designed to use the filename format "Due-to-be-deleted-on YYYYMMDD Original filename.ext"
as it is a predictable format that can be parsed to find out when the files should be deleted.

Hopefully you can test it on copies of your files, in test folders, and figure out which bits need to be altered to get the commands working.

It uses WSH to get the dates in a reliable format.

Code: Select all

@echo off
:: Gets date today,
:: date 8 days ago (Robocopy uses UTC date/time so add some leeway),
:: date 30 days from now,
:: and set TAB character

set day=0
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "today=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

echo today is "%today%"

set day=-8
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "day8=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

echo 8 days ago was "%day8%"

set day=+30
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "day30=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

echo 30 days from now is "%day30%"

pause

:: Robocopy - lists modified file dates in folder tree using UTC and sorted by date

:: this is the holding folder for the backups

    set "target=%userprofile%\desktop\Marked-for-deletion"
    md "%target%" 2>nul


:: this folder is where the files are searched, excluding the target folder and *.lnk

    pushd "%userprofile%\desktop" && (
     
    for /f "tokens=1-3,4,* delims=/-%tab% " %%a in (
        'robocopy "%cd%" "%cd%" "*" /xd "%target%" /xf "*.lnk" /s /is /nocopy /nc /ns /ts /fp /np /ndl /njh /njs /xjd /r:0 /w:0 /l ^| sort '
      ) do (
            if %%a%%b%%c LSS %day8% echo date="%%a%%b%%c" File="%%e" - COMMAND: move "%%e" "%target%\Due-to-be-deleted-on %day30% %%~nxe"
            if %%a%%b%%c GEQ %day8% echo skipping the newer files & goto :skip
           )   
    )
    :skip
pause


echo Removing files which have been present for 30 days

pushd "%target%" && (
   for /f "delims=" %%a in ('dir "Due-to-be-deleted-on *" /b /s /a-d ') do (
      for /f "tokens=2" %%b in ("%%~nxa") do if %%b LSS %today% echo COMMAND: del "%%a" as %%b is before today %today%
   )
)
pause     

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Batch to intelligently delete files and folders

#7 Post by Samir » 15 Nov 2014 09:49

jbseventeen wrote:My experience on most forums has been "put things simply or don't get answered" but fair enough. I can see how the details are important here.

I maintain several PC's that are run by other users. Pretty much all of these users maintain their files poorly and 'abandon' unnecessary files in several locations.

Now every time a machine needs to be reformatted (most of the time due to virus infections that are all too frequent), a backup folder is created to contain all of the old files, wherther necessary or not, from pertinent locations. This has resulted in several backups within backups within backups and so on, that are mostly filled with unneeded files.

It has got to the point that most of these machines now require 10GB to 40+GB just for "backups". Most of these machines only have 80GB hard drives that are unlikely to be replaced unless they fail so space is quite important. This is where the script would help by keeping unneeded files from making it to the backup phase.

The script would run on each machine, in the area(s) identified as "work" folders (mostly but not always desktop and my documents). The script should:

1. Check "\Desktop\" for files or folders that have not been modified in 7 days (excluding shortcuts and the "Desktop\Marked-for-deletion\" folder)
1a. Create a sub folder within "\Desktop\" called "\Desktop\Marked-for-deletion\" if that folder does not already exist.
1b. Rename the files that meet the criterion to "filename-del-on-DD-MM-YYYY.extension"
1c. Move them to "\Desktop\Marked-for-deletion\", and finally
2. Delete files from the "\Desktop\Marked-for-deletion\" folder after 30 days.
(NOTE: If excluding the marked-for-deletion folder itself from the script is not possible, it could always be placed elsewhere)

The machines are scheduled to shut down at various times every day which is why I would run the script at shutdown. I would modify only the locations on the script based on where the user likes to "work".

To be honest, after going through several tutorials I still haven't a clue on what the actual code should look like. If you were willing to write the script for me, I'd greatly appreciate it but if not, a basic outline of the script is all I'm looking for.
Or just some help with how to append -del-on-DD-MM-YYYY to the end of a file or folder name, where DD-MM-YYYY is the modified date + 30 days.
I know this isn't a batch answer to your problem, but it sounds like these are older machines running xp. If so, windows steadystate may be your savior. It's fairly robust against viruses and malware since nothing saved on the local system is kept upon logout or reboot, and then you can simply have users save files to a file server that you can easily manage with work folders for each user. Another option would be to use xp embedded (or in win7 embedded) thin clients which have enough horsepower for regular daily tasks in an office program, and again are completely impervious to virus or malware.

I had horrible problems with viruses and malware until I dumped all our regular PCs for thin clients and steadystate based systems. Now, I don't even have virus or malware software installed. Just a reboot a day keeps the baddies away...

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#8 Post by jbseventeen » 15 Nov 2014 14:19

@foxidrive
Can't thank you enough! Will test this shortly. PM me a donation link if you have one.

@Samir
I built a server for just this reason a couple of years ago but it is more or less a paperweight. Some of the users can just about type so I'm not surprised. Will look into steady state though since some of these viruses seem to be voluntarily installed. Thanks!

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#9 Post by jbseventeen » 15 Nov 2014 16:54

Kudos to you that is one mother of a complicated script! I stared at it for a good 2 hours trying to decipher it before realizing it is beyond me.

The test script worked flawlessly in every scenario I could throw at it.
The correct files would be moved.
The correct files would be skipped.
Files with the right prefix would be deleted.
And several combinations.

I do see one potential issue that I had not thought of before :
1. Files in the "marked-for-deletion" folder that are simply renamed without the prefix would not get deleted.
2. Moving non-prefixed files into the marked-for-deletion folder grants them immunity.

If I've worked out the logic correctly, the solution could be to remove the folder exclusion, while adding the ability to identify already renamed files by the prefix (so as to prevent re-renaming of files).

Hope this makes sense as I've been staring at the screen for too long.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to intelligently delete files and folders

#10 Post by foxidrive » 15 Nov 2014 22:19

jbseventeen wrote:I do see one potential issue that I had not thought of before :
1. Files in the "marked-for-deletion" folder that are simply renamed without the prefix would not get deleted.
2. Moving non-prefixed files into the marked-for-deletion folder grants them immunity.


I'm glad that it is working - I'd been awake for 36 hours and wasn't sure which side was up by the time I'd finished. :D

One solution to files that are added/or are inside to the folder and that don't have a prefix, is to make the script
add a standard prefix for 30 days to those files, when it runs.

Would that solve this issue?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Batch to intelligently delete files and folders

#11 Post by Samir » 15 Nov 2014 22:48

jbseventeen wrote:@Samir
I built a server for just this reason a couple of years ago but it is more or less a paperweight. Some of the users can just about type so I'm not surprised. Will look into steady state though since some of these viruses seem to be voluntarily installed. Thanks!
My users are the same. :lol:

I literally have just 5 things on the start menu for them and have mapped a drive letter to their work folder so they can save files. I always keep this drive letter the same in case someone asks where a file was saved to. Steadystate is awesome. Too bad they abandoned it with win7, although you can do almost everything with tweaks. But there's also the commercial program deep freeze which is awesome at locking down a system. 8)

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#12 Post by jbseventeen » 16 Nov 2014 02:38

@foxidrive
If I understood you correctly, you mean keeping the current folder exclusion, while adding a routine for files in the marked-for-deletion folder. That would do the trick, sure.

@Samir
Glad I'm not alone. It is a lot of work but it could work. Will have to do a lot of research and testing first though.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to intelligently delete files and folders

#13 Post by foxidrive » 16 Nov 2014 04:12

jbseventeen wrote:@foxidrive
If I understood you correctly, you mean keeping the current folder exclusion, while adding a routine for files in the marked-for-deletion folder. That would do the trick, sure.


Yes, replace this section at the bottom and it should do that:

Code: Select all

echo Removing files which have been present for 30 days

pushd "%target%" && (
   for /f "delims=" %%a in ('dir "Due-to-be-deleted-on *" /b /s /a-d ') do (
      for /f "tokens=2" %%b in ("%%~nxa") do if %%b LSS %today% echo COMMAND: del "%%a" as %%b is before today %today%
   )

echo Adding the filename prefix with a 30 day expiry date to all files without the prefix:

   for /f "delims=" %%a in ('dir /b /s /a-d ^|find /i /v "Due-to-be-deleted-on " ') do (
      ren "%%a" "Due-to-be-deleted-on %day30% %%~nxa"
   )
)
pause   

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#14 Post by jbseventeen » 16 Nov 2014 16:49

Hmm...not quite since it even renames files <7 days old.

I've been trying to tinker with it myself but unfortunately i suck at it. Brace yourselves
(10 changes numbered and encased in ####):

Code: Select all

@echo off
:: Gets date today,
:: date 8 days ago (Robocopy uses UTC date/time so add some leeway),
:: date 30 days from now,
:: and set TAB character

set day=0
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "today=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

echo today is "%today%"

set day=-8
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "day8=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

echo 8 days ago was "%day8%"

set day=+30
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "day30=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

echo 30 days from now is "%day30%"

pause

:: Robocopy - lists modified file dates in folder tree using UTC and sorted by date

:: this is the holding folder for the backups

    set "target=%userprofile%\desktop\Marked-for-deletion"
    #### 1. moved line "md "%target%" 2>nul" further down to prevent %target% from being created when there are no files to process ####

:: this folder is where the files are searched, excluding the target folder and *.lnk

    pushd "%userprofile%\desktop" &&
   (
   for /f "tokens=1-3,4,* delims=/-%tab% " %%a in
      (
        'robocopy "%cd%" "%cd%" "*" /xf "*.lnk" /s /is /nocopy /nc /ns /ts /fp /np /ndl /njh /njs /xjd /r:0 /w:0 /l ^| sort '
      ) #### 2. removed parameter "/xd "%target%" " to allow processing of files in %target%\ ####
      do (
            if %%a%%b%%c LSS %day8%
         (
         if filename=="*-del-on-*" #### 4. I'm embarrassed that I don't know how to write this. also prefix is being changed to suffix "*-del-on-yyyy-mm-dd.*" further down ####
            ( 
            md "%target%" 2>nul #### 3. moved line here to prevent %target% from being created when there are no files to process ####
            move "%%e" "%target%\ #### 5. Move files with correct suffix, but in \desktop rather than %target%, to %target%\ ####
            goto :skip #### 6. skip remaining lines in loop ####
            )
         md "%target%" 2>nul #### 7. moved line here to prevent %target% from being created when there are no files to process ####
         echo date="%%a%%b%%c" File="%%e"
         move "%%e" "%target%\%%~nxe-del-on-%day30%" #### 8. trying to change the prefix to a suffix but this results in "filename.extention-yyyy-mm-dd" e.g. file.jpg-2014-12-16 ####
         :skip #### 9. skip files with correct suffix ####
         )
            if %%a%%b%%c GEQ %day8% echo skipping the newer files & goto :skip
           )   
    )
    :skip
) #### 10. 30 day deletion code removed for now ####
pause

jbseventeen
Posts: 11
Joined: 13 Nov 2014 18:47

Re: Batch to intelligently delete files and folders

#15 Post by jbseventeen » 16 Nov 2014 17:00

I wonder if you might direct me to some good tutorials on this kind of batch scripting instead.

This is not the simple 10 line batch i had first imagined and I'd rather not ask you to "do this for me" for free...

Post Reply