Google Answers Logo
View Question
 
Q: Automatic folder archive... ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Automatic folder archive...
Category: Computers > Programming
Asked by: keenr1214-ga
List Price: $20.00
Posted: 09 Sep 2004 19:29 PDT
Expires: 09 Oct 2004 19:29 PDT
Question ID: 399166
I am looking for some sort of program that can be scheduled to run
automatically by windows.

I need it to essentially take the entire contents of a specified
folder, copy it, and move it one folder up and into a folder and then
into another new folder with today's date. (ie... select * of this
folder, copy it, move ../Archive/09-09-04/ paste it all here)

I would appreciate something that I could easily manage through
Windows Scheduled Tasks (Win XP Pro SP2).

I am looking for someone to write something that is guaranteed to work
on my machine or to point me to something cheap that will do EXACTLY
what I need.

Request for Question Clarification by leapinglizard-ga on 09 Sep 2004 19:38 PDT
I can write a program that does exactly what you require, but it is
guaranteed to run on your machine only if you download and install a
recent version of the Python interpreter.

Download Standard Python Software [see Python 2.3.4 Windows installer]
http://python.org/download/

Do we have a deal?

leapinglizard

Clarification of Question by keenr1214-ga on 09 Sep 2004 19:46 PDT
Do you mean will work through Scheduled tasks too?  Will I be able to
schedule multiple times a week?

If both are a yes, then there is a deal.

Thank you.

Clarification of Question by keenr1214-ga on 09 Sep 2004 19:48 PDT
I just want to make sure that I was clear in that it needs to take the
entire contents of the folder, including subfolders, and documents,
and preserve directory structure...

Request for Question Clarification by leapinglizard-ga on 09 Sep 2004 20:02 PDT
I understand the necessity to preserve directory structure, and I can
implement that functionality for you.

The scheduling itself would be up to you. What I would provide is a
custom-made script, which is a text file containing Python code. If
the script were named, say, archiver.py, you would execute it by
running

  python.exe archiver.py

from the DOS prompt or some other command line. What's happening there
is that you execute the Python interpreter, which reads your script
and performs the necessary actions. I confess that I have never used
Windows Scheduled Tasks, but any scheduling software worth its salt
should be able to run the above line several times a week or whenever
necessary.

If you like, I'll just write the script and post it here as a Request
for Question Clarification. You try it out, and if it doesn't work,
the deal is off. Otherwise, I'll post it as an answer and I get paid.
What do you say?

leapinglizard

Clarification of Question by keenr1214-ga on 09 Sep 2004 20:15 PDT
Sounds good.  I am sure it will work with scheduler.  I understand
what I need to do to get it to execute

python.exe archiver.py

.

Thanks.

Request for Question Clarification by leapinglizard-ga on 09 Sep 2004 21:26 PDT
Here's the script. Copy the whole shebang into a text file called
archiver.py without altering the spacing one bit (Notepad works well
for this). Modify the constants at the beginning as necessary. There
are some comments beside them in the code, and most are
self-explanatory. If you run the script several times in one day and
OVERWRITE_EXISTING_DIRECTORY is true, the same archive folder gets
used each time. Otherwise, a new archive folder is made with a number
appended. In the American date format, the month and day come first,
but in the international format, the year comes first. The log file is
a text file where the script records its actions. You might like to
take a look at it from time to time and delete it if it gets too big.
Tell me what you think!

leapinglizard



# beginning of archiver.py

# *** please adjust the below constants as necessary ***

READ_FROM_DIRECTORY = 'test'        # back up this directory

BASE_DIRECTORY = '../Archive'       # make archive folders here

OVERWRITE_EXISTING_DIRECTORY = 1    # 0 means no, 1 means yes

AMERICAN_DATE_FORMAT = 0      	    # ditto

LOG_FILE = 'archiver_log.txt'       # set to '' if no log is desired
				    # the log can get very big over time!

# *** end of constants ***


import sys, os, string, re
import time

source_dir = READ_FROM_DIRECTORY
target_base = BASE_DIRECTORY
overwrite = OVERWRITE_EXISTING_DIRECTORY 
for dir_name in [source_dir, target_base]:
    if not os.path.isdir(dir_name):
	sys.exit('error: "%s" is not a valid directory name' % dir_name)

def msg(s):
    if LOG_FILE != '':
	open(LOG_FILE, 'a').write(s+'\n')
    print s

msg('[%s] starting archiver.py' % time.asctime())

[year, month, day] = time.localtime()[:3]
target_dir = '%s/%02d-%02d-%s' % (target_base, month, day, str(year)[:-2])
if not AMERICAN_DATE_FORMAT:
    target_dir = '%s/%d.%02d.%02d' % (target_base, year, month, day)

if not os.path.isdir(target_dir):
    if os.path.exists(target_dir):
        os.remove(target_dir)
    msg('making new archive folder "%s"' % target_dir)
    os.makedirs(target_dir)
else:
    if overwrite:
        msg('using existing archive folder "%s"' % target_dir)
    if not overwrite:
        i = 1
        while 1:
            new_target_dir = '%s.%05d' % (target_dir, i)
            if not os.path.isdir(new_target_dir):
                msg('making new archive folder "%s"' % new_target_dir)
                if os.path.exists(new_target_dir):
                    os.remove(new_target_dir)
                os.makedirs(new_target_dir)
                break
            i += 1
            if i >= 10**5:
                sys.exit('error: too many versions of "%s"')

def copy(source_name, target_name):
    inf = open(source_name, 'rb', 8192)
    outf = open(target_name, 'wb', 8192)
    outf.write(inf.read())
    outf.close()
    inf.close()

def rec_copy(_, dir_name, names):
    msg('copying from directory "%s"' % dir_name)
    target_subdir = '%s/%s' % (target_dir, dir_name)
    if not os.path.isdir(target_subdir):
	if os.path.exists(target_subdir):
	    os.remove(target_subdir)
	os.makedirs(target_subdir)
    for name in names:
	source_name = '%s/%s' % (dir_name, name)
	if os.path.isdir(source_name):
	    continue
	msg('    copying file "%s"' % name)
	copy(source_name, '%s/%s' % (target_subdir, name))

os.path.walk(source_dir, rec_copy, None)
msg('[%s] ending archiver.py\n' % time.asctime())

# end of archiver.py

Request for Question Clarification by leapinglizard-ga on 09 Sep 2004 21:46 PDT
Oops! I've found a bug that makes the script fail under some
circumstances. Please ignore the above code and use the version below.
Again, spacing is crucial, so don't meddle with it. And remember to
adjust the constants as necessary.

leapinglizard



# beginning of archiver.py

# *** please adjust the below constants as necessary ***

READ_FROM_DIRECTORY = 'test'        # back up this directory

BASE_DIRECTORY = '../Archive'       # make archive folders here

OVERWRITE_EXISTING_DIRECTORY = 1    # 0 means no, 1 means yes

AMERICAN_DATE_FORMAT = 0      	    # ditto

LOG_FILE = 'archiver_log.txt'       # set to '' if no log is desired
				    # the log can get very big over time!

# *** end of constants ***


import sys, os, string, re
import time

source_dir = READ_FROM_DIRECTORY
target_base = BASE_DIRECTORY
overwrite = OVERWRITE_EXISTING_DIRECTORY 
for dir_name in [source_dir, target_base]:
    if not os.path.isdir(dir_name):
	sys.exit('error: "%s" is not a valid directory name' % dir_name)

def msg(s):
    if LOG_FILE != '':
	open(LOG_FILE, 'a').write(s+'\n')
    print s

msg('[%s] starting archiver.py' % time.asctime())

[year, month, day] = time.localtime()[:3]
target_dir = '%s/%02d-%02d-%s' % (target_base, month, day, str(year)[:-2])
if not AMERICAN_DATE_FORMAT:
    target_dir = '%s/%d.%02d.%02d' % (target_base, year, month, day)

if not os.path.isdir(target_dir):
    if os.path.exists(target_dir):
        os.remove(target_dir)
    msg('making new archive folder "%s"' % target_dir)
    os.makedirs(target_dir)
else:
    if overwrite:
        msg('using existing archive folder "%s"' % target_dir)
    if not overwrite:
        i = 1
        while 1:
            new_target_dir = '%s.%05d' % (target_dir, i)
            if not os.path.isdir(new_target_dir):
                msg('making new archive folder "%s"' % new_target_dir)
                if os.path.exists(new_target_dir):
                    os.remove(new_target_dir)
                os.makedirs(new_target_dir)
                break
            i += 1
            if i >= 10**5:
                sys.exit('error: too many versions of "%s"')

def copy(source_name, target_name):
    inf = open(source_name, 'rb', 8192)
    outf = open(target_name, 'wb', 8192)
    outf.write(inf.read())
    outf.close()
    inf.close()

def rec_copy(_, dir_name, names):
    if dir_name == source_dir:
	target_subdir = target_dir
    else:
	subdir_name = '/'+re.sub('^%s/'%source_dir, '', dir_name)
	msg('copying from directory "%s"' % dir_name)
	target_subdir = '%s%s' % (target_dir, subdir_name)
    if not os.path.isdir(target_subdir):
	if os.path.exists(target_subdir):
	    os.remove(target_subdir)
	os.makedirs(target_subdir)
    for name in names:
	source_name = '%s/%s' % (dir_name, name)
	if os.path.isdir(source_name):
	    continue
	msg('    copying file "%s"' % name)
	copy(source_name, '%s/%s' % (target_subdir, name))

os.path.walk(source_dir, rec_copy, None)
msg('[%s] ending archiver.py\n' % time.asctime())

# end of archiver.py

Request for Question Clarification by leapinglizard-ga on 09 Sep 2004 22:05 PDT
Arrrgghh! I made another mistake! I fixed it, though. So please ignore
the above version as well. The one below works on my system. If it
doesn't work on yours, there might be a difference in the way we
construct pathnames, in which case there's another fix I can apply.




# beginning of archiver.py

# *** adjust the below constants as necessary ***

READ_FROM_DIRECTORY = 'test'        # back up this directory

BASE_DIRECTORY = '../Archive'       # make archive folders here

OVERWRITE_EXISTING_DIRECTORY = 1    # 0 means no, 1 means yes

AMERICAN_DATE_FORMAT = 0            # ditto

LOG_FILE = 'archiver_log.txt'       # set to '' if no log is desired
                                    # the log can get very big over time!

# *** end of constants ***


import sys, os, string, re
import time

source_dir = READ_FROM_DIRECTORY
target_base = BASE_DIRECTORY
overwrite = OVERWRITE_EXISTING_DIRECTORY
for dir_name in [source_dir, target_base]:
    if not os.path.isdir(dir_name):
        sys.exit('error: "%s" is not a valid directory name' % dir_name)

def msg(s):
    if LOG_FILE != '':
        open(LOG_FILE, 'a').write(s+'\n')
    print s

msg('[%s] starting archiver.py' % time.asctime())

[year, month, day] = time.localtime()[:3]
target_dir = '%s/%02d-%02d-%s' % (target_base, month, day, str(year)[:-2])
if not AMERICAN_DATE_FORMAT:
    target_dir = '%s/%d.%02d.%02d' % (target_base, year, month, day)

if not os.path.isdir(target_dir):
    if os.path.exists(target_dir):
        os.remove(target_dir)
    msg('making new archive folder "%s"' % target_dir)
    os.makedirs(target_dir)
else:
    if overwrite:
        msg('using existing archive folder "%s"' % target_dir)
    if not overwrite:
        i = 1
        while 1:
            new_target_dir = '%s.%05d' % (target_dir, i)
            if not os.path.isdir(new_target_dir):
                msg('making new archive folder "%s"' % new_target_dir)
                if os.path.exists(new_target_dir):
                    os.remove(new_target_dir)
                os.makedirs(new_target_dir)
                break
            i += 1
            if i >= 10**5:
                sys.exit('error: too many versions of "%s"')

def copy(source_name, target_name):
    inf = open(source_name, 'rb', 8192)
    outf = open(target_name, 'wb', 8192)
    outf.write(inf.read())
    outf.close()
    inf.close()

def rec_copy(_, dir_name, names):
    if dir_name == source_dir:
        target_subdir = target_dir
    else:
        msg('copying from directory "%s"' % dir_name)
        subdir_name = re.sub('^%s'%source_dir, '', dir_name)
        target_subdir = '%s/%s' % (target_dir, subdir_name)
    if not os.path.isdir(target_subdir):
        if os.path.exists(target_subdir):
            os.remove(target_subdir)
        os.makedirs(target_subdir)
    for name in names:
        source_name = '%s/%s' % (dir_name, name)
        if os.path.isdir(source_name):
            continue
        msg('    copying file "%s"' % name)
        copy(source_name, '%s/%s' % (target_subdir, name))

os.path.walk(source_dir, rec_copy, None)
msg('[%s] ending archiver.py\n' % time.asctime())

# end of archiver.py

Clarification of Question by keenr1214-ga on 10 Sep 2004 08:11 PDT
What are the links (../Archive, test, archiver_log.txt) relative to? 
Where do I have to put the archiver.py file in order for this to work?
 Right above test, right next to python.exe?  Can I use fully
qualified paths?

Request for Question Clarification by leapinglizard-ga on 10 Sep 2004 09:31 PDT
You can put the script anywhere, and then you can use paths relative
to its location, or you can use full paths. It's up to you.

If archiver.py is in the same directory as python.exe, you can just
pass the name of the script. Otherwise, you'll have to pass python.exe
a longer path name, whether relative or absolute, so that it can find
archiver.py.

leapinglizard

Clarification of Question by keenr1214-ga on 10 Sep 2004 20:08 PDT
Excellent, I was able to get it to work!

One small thing that I noticed, I am using the American date and the
folder date for today reads 09-10-20 .  Could that be fixed to 04 or
2004 ??

Thank you, thank you.
Answer  
Subject: Re: Automatic folder archive...
Answered By: leapinglizard-ga on 10 Sep 2004 20:31 PDT
Rated:4 out of 5 stars
 
Dear keenr1214,

I'm glad the script works for you. Thanks for pointing out the error
in the American date format, which I hadn't noticed in my late-night
testing. I'll explain what the problem is, and then I'll post the
whole script again below.

In the line that reads

target_dir = '%s/%02d-%02d-%s' % (target_base, month, day, str(year)[:-2])

the last item inside the parentheses, "str(year)[:-2]", which was
intended to capture the last two digits of the year, should actually
be "str(year)[-2:]". Alternatively, it could be "str(year)" or even
just "year" to use all four digits. In the source code below, I've
made it use the last two digits, as you originally specified. You can
recopy the whole script, or if you're feeling confident, adjust the
relevant line yourself.

Regards,

leapinglizard




# beginning of archiver.py

# *** adjust the below constants as necessary ***

READ_FROM_DIRECTORY = 'test'        # back up this directory

BASE_DIRECTORY = '../Archive'       # make archive folders here

OVERWRITE_EXISTING_DIRECTORY = 1    # 0 means no, 1 means yes

AMERICAN_DATE_FORMAT = 0            # ditto

LOG_FILE = 'archiver_log.txt'       # set to '' if no log is desired
                                    # the log can get very big over time!

# *** end of constants ***


import sys, os, string, re
import time

source_dir = READ_FROM_DIRECTORY
target_base = BASE_DIRECTORY
overwrite = OVERWRITE_EXISTING_DIRECTORY
for dir_name in [source_dir, target_base]:
    if not os.path.isdir(dir_name):
        sys.exit('error: "%s" is not a valid directory name' % dir_name)

def msg(s):
    if LOG_FILE != '':
        open(LOG_FILE, 'a').write(s+'\n')
    print s

msg('[%s] starting archiver.py' % time.asctime())

[year, month, day] = time.localtime()[:3]
target_dir = '%s/%02d-%02d-%s' % (target_base, month, day, str(year)[-2:])
if not AMERICAN_DATE_FORMAT:
    target_dir = '%s/%d.%02d.%02d' % (target_base, year, month, day)

if not os.path.isdir(target_dir):
    if os.path.exists(target_dir):
        os.remove(target_dir)
    msg('making new archive folder "%s"' % target_dir)
    os.makedirs(target_dir)
else:
    if overwrite:
        msg('using existing archive folder "%s"' % target_dir)
    if not overwrite:
        i = 1
        while 1:
            new_target_dir = '%s.%05d' % (target_dir, i)
            if not os.path.isdir(new_target_dir):
                msg('making new archive folder "%s"' % new_target_dir)
                if os.path.exists(new_target_dir):
                    os.remove(new_target_dir)
                os.makedirs(new_target_dir)
                break
            i += 1
            if i >= 10**5:
                sys.exit('error: too many versions of "%s"')

def copy(source_name, target_name):
    inf = open(source_name, 'rb', 8192)
    outf = open(target_name, 'wb', 8192)
    outf.write(inf.read())
    outf.close()
    inf.close()

def rec_copy(_, dir_name, names):
    if dir_name == source_dir:
        target_subdir = target_dir
    else:
        msg('copying from directory "%s"' % dir_name)
        subdir_name = re.sub('^%s'%source_dir, '', dir_name)
        target_subdir = '%s/%s' % (target_dir, subdir_name)
    if not os.path.isdir(target_subdir):
        if os.path.exists(target_subdir):
            os.remove(target_subdir)
        os.makedirs(target_subdir)
    for name in names:
        source_name = '%s/%s' % (dir_name, name)
        if os.path.isdir(source_name):
            continue
        msg('    copying file "%s"' % name)
        copy(source_name, '%s/%s' % (target_subdir, name))

os.path.walk(source_dir, rec_copy, None)
msg('[%s] ending archiver.py\n' % time.asctime())

# end of archiver.py

Request for Answer Clarification by keenr1214-ga on 10 Sep 2004 20:40 PDT
Excellent, in a moment I will post another google answers question
seeking for a python program very similar to the one you just wrote,
that will simplay take a given directory which contains folders with
dates as their name in the format of "mm-dd-yy". and erase folders,
their contents, and subfolders too, if their date is older than a
specified period of time (I am seeking 1 month).  I hope that you are
interested.  Thank you.

Clarification of Answer by leapinglizard-ga on 10 Sep 2004 21:15 PDT
Sure, I'll take a look at your next question, though I might not have
time to answer it.

leapinglizard
keenr1214-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy