How to automatically zip files not accessed for specified time & remove uncompressed file

Posted on 2004-09-30
Medium Priority
Last Modified: 2010-04-14

Is there some additional software / script that could be run that can check the 'Last Accessed' attribute of files and Zip old items and remove the uncompressed file?  The computer is a Windows 2000 Server.  

Thanks for any pointers

Question by:robjeeves
  • 3
  • 2
LVL 97

Expert Comment

by:Lee W, MVP
ID: 12197334
I believe this will do what you seek:

(It's late and I haven't tested it thoroughly, so be careful

@echo off
for /f "usebackq tokens=1,5" %%a in (`dir /a-d /ta | find /i "/"`) do (
if "%%a" GEQ "%1" (
      zip %2 %%a
      del %%a

Requires usage as such:

scriptname.cmd date file.zip

For example, all files accessed since 9/15/04 would result in running the script with the following parameters:

scriptname 09/15/2004 file.zip

Author Comment

ID: 12197516
That looks great.  Trouble is I have no idea how to use it :-)

I should of indicated my level of scripting knowledge (none) before asking for a script.  How do I use this script i.e Execute it


Expert Comment

ID: 12201122
1. create a new .txt file and rename it scriptname.bat

2. paste that code in a the called scriptname.bat

3. Start -> Run : "cmd.exe"

4. "cd" to directory w/ script in it

5. now:
prompt> scriptname.bat 9/15/04 file.zip
to run it

I can't read the script syntax well enough to tell which files will be .zipped from there, be careful, it may be just files in that directory and below or it may be all files on your machine.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 97

Accepted Solution

Lee W, MVP earned 200 total points
ID: 12201608

the line that reads
   zip %2 %%a

   zip %2 %%b

To explain in more detail, the for command cycles through each entry found when performing a DIR /A-D /TA (/a-d = all files except directories; /ta = time displayed is last accessed; do a dir /? from a command prompt for more info on the DIR command).  Further, the FIND /I "/" looks through the results of the DIR /A-D /TA command to find only lines with "/" in them - in this case, it will be anything with a date, since dates are formatted MM/DD/YYYY.

For each entry found, it then does a check - if "%%a" (the first token from the DIR command executed before, which happens to be the date) is GEQ (Greater then or EQual to) %1 which is the first parameter entered when executing the script and must be in the identical form of the dates listed when doing the dir (meaning you MUST enter the date as 09/15/2004 - entering them in 9/15/04 won't likely work, nor will 09-15-2004.  It MUST BE in the form of 09/15/2004.

So anyway, if "%%a" = "%1" Then it executes a ZIP program (NOT included with windows - there's a freeware ZIP utility called ZIP and ZIP16 that works much like PKZIP, but I'm moving and the computer with the bookmark is currently unavailable).  In any case, %2 is the file name - the SECOND parameter specified when entering the script.  So ZIP %2 %%b  should add the file name (%%b, the 5th token) to the zip file named %2.

tokens are numbered, 1-?, each one seperated by either tab(s) or spaces(s), by default - other tokens can be specified, but my sample aren't.
If your directory listing read ... then tokens would be:

10/01/2004  12:49 AM               125 la.cmd
09/12/2004  11:43 AM            11,808 gbmail.exe
09/12/2004  11:34 AM               286 getversion.cmd
09/12/2004  11:34 AM                30 Null
09/12/2004  11:45 AM             3,316 xcopy.help

If you execute the script as "scriptname 09/15/2004 file.zip", then the script would actually read:

for /f "usebackq tokens=1,5" %%a in (`dir /a-d /ta | find /i "/"`) do (
if "10/01/2004" GEQ "09/15/2004" (
     zip file.zip la.cmd
     del la.cmd

the for would actually run the if comparison once for each file listed.  The example above is only one comparison.

Author Comment

ID: 12222278
Thanks for such a detailed explaination.  I will give this a try this week.


Author Comment

ID: 12478991
THanks very much for you input on the Question.  We actually don't require solution anymore but thanks again for advice.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Are you in the migration process of your Exchange to Exchange Online? Be aware of customized solutions developed on the transport role on your old Exchange server. They might not be convertible to Exchange Online!
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question