• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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

hi

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

Rob
0
robjeeves
Asked:
robjeeves
  • 3
  • 2
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I believe this will do what you seek:

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

-------8<----------------
@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
)
)
-------8<------------------

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
0
 
robjeevesAuthor Commented:
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

Rob
0
 
demingj1Commented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
Lee W, MVPTechnology and Business Process AdvisorCommented:
I MADE AN ERROR IN THE SCRIPT!!!

the line that reads
   zip %2 %%a

SHOULD read
   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.
0
 
robjeevesAuthor Commented:
Thanks for such a detailed explaination.  I will give this a try this week.

Rob
0
 
robjeevesAuthor Commented:
THanks very much for you input on the Question.  We actually don't require solution anymore but thanks again for advice.

Rob
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now