file backup in windows

K

k.i.n.g.

Hi ALL,

I am a newbee programmer and started of with python recently. I am
trying write a script which backups outlook (.pst ) files everytime I
shutdown my system. I have writtern following code after some findings
on net. My .pst file path is as follows

" c:\documents and settings\060577\Local Settings\Application
Data\Microsoft\Outlook "
where 060577 represents username. I want my script to identigy the user
logged in and go to the resp outlook folder or should be able to read
outlook store directory path from registry and the copy the files to
the desired path.

---------------------------------------------------
how can i make the following code work, I have probelm with filepath
declaration.
---------------------------------------------------
import os, shutil
filepath = ' C:\\Documents and Settings\\060577\\Local
Settings\\Application Data\\Microsoft\\Outlook\\* '
backup = ' D:\\temp\\outlook '
os.system ("xcopy /s %s %s" % (filepath, backup))
 
J

John Machin

k.i.n.g. said:
Hi ALL,

I am a newbee programmer and started of with python recently. I am
trying write a script which backups outlook (.pst ) files everytime I
shutdown my system. I have writtern following code after some findings
on net. My .pst file path is as follows

" c:\documents and settings\060577\Local Settings\Application
Data\Microsoft\Outlook "
where 060577 represents username. I want my script to identigy the user
logged in and go to the resp outlook folder or should be able to read
outlook store directory path from registry and the copy the files to
the desired path.

---------------------------------------------------
how can i make the following code work, I have probelm with filepath
declaration.
---------------------------------------------------
import os, shutil
filepath = ' C:\\Documents and Settings\\060577\\Local
Settings\\Application Data\\Microsoft\\Outlook\\* '
backup = ' D:\\temp\\outlook '

Aside: having a space at the beginning and/or end of the filename has
no good effect and may cause problems, so don't do it.
os.system ("xcopy /s %s %s" % (filepath, backup))
-----------------------------------------

It's always a good idea *before* you write an os.system call on *any*
operating system to try a few sample commands at the command line. You
would find in this case that the problem exists there too -- it has
nothing to do with Python. The problem is that the first argument
*contains* spaces, but the Windows command processor splits the command
line on spaces, so it thinks the first argument is 'C:\\Documents'. On
both the command line and in your script, you will need to wrap quotes
around each argument that does/could contain spaces.

[untested]
os.system ('xcopy /s "%s" "%s"' % (filepath, backup))

Hint: you should find it easier using raw strings for Windows
filenames:
backup = r'D:\temp\outlook'

HTH,
John
 
K

k.i.n.g.

Thank You all for reply's so far
<code>
import os, sys
from win32com.shell import shell, shellcon

local_app_data = shell.SHGetSpecialFolderPath (0,
shellcon.CSIDL_LOCAL_APPDATA)
outlook_path = os.path.join (local_app_data, "Microsoft", "Outlook")

print outlook_path

</code)

The above code was fine while printing, when I am trying to use this
(outlook_path) to use as source path it is giving file permission error

can you please clarify this
 
M

Mikael Olofsson

k.i.n.g. said:
[snip code]
The above code was fine while printing, when I am trying to use this
(outlook_path) to use as source path it is giving file permission error
can you please clarify this

Did you follow the link that Fredrik Lundh gave you?

/MiO
 
K

k.i.n.g.

Hi ,

I am sorry I am providing the code i used as it is. Being newbee to
programming I have tinkerd with various options i found on the net.
--------------------------------------------------------------------------------
start of the code
------------------------------------------------------------------------------------
import os, sys ,shutil, win32file
import time
from win32com.shell import shell, shellcon

local_app_data = shell.SHGetSpecialFolderPath
(0,shellcon.CSIDL_LOCAL_APPDATA)
outlook_path = os.path.join (local_app_data, "Microsoft", "Outlook")

# print outlook_path
#c:\documents and settings\060577\Local Settings\Application
Data\Microsoft\Outlook

source = outlook_path
#source = outlook_path +'\\*'
print source

backup = 'D:\MailBackup'
backup1=r'D:\temp\outlook1'
backup2 = 'D:\MailBackup'
today = backup1 + '_' + time.strftime ( '%Y-%m-%d')
now = time.strftime('%H.%M.%S')
target = today +'_'+ now
if not os.path.exists(target):
os.mkdir(target) # make directory
print 'Successfully created directory', target

#shutil.copy(source, backup)
#os.system( "copy "(source,backup))
#os.system ("xcopy %s %s" % (source, backup1))
#os.system ("xcopy /s %s %s" % (outlook_path, backup1))
#os.system ("xcopy /s %s %s" % (backup2, backup1))
#os.system( 'xcopy /i D:\\MailBackup\\* d:\\temp\\outlook')
#win32file.CopyFile (outlook_path, backup, 0)
#os.system ("xcopy /s %s %s" % (backup,target))

os.system ("xcopy /s %s %s" % (source,target)) # this doesnt give me
any errors but the #work is not done

win32file.CopyFile (source, target, 1)
-----------------------------------------------------------------------
end
------------------------------------------------------------------


---------
output
----------
C:\Documents and Settings\060577\Local Settings\Application
Data\Microsoft\Outlook\*
Successfully created directory D:\temp\outlook1_2006-11-22_17.41.54

Traceback (most recent call last):
File "C:\Documents and
Settings\060577\kk\source_code\py\Mypy\pywin32test.py", line 34, in
<module>
win32file.CopyFile (source, target, 1)
error: (123, 'CopyFile', 'The filename, directory name, or volume label
syntax is incorrect.')
 
M

MC

Hi!

" are your friend.

See, also:
filepath = '"%HOMEPATH%\\LocalSettings\\Application
Data\\Microsoft\\Outlook\\*"'

and %USERPROFILE% %APPDATA% etc.
 
D

Dennis Lee Bieber

os.system ("xcopy /s %s %s" % (source,target)) # this doesnt give me
any errors but the #work is not done
That one is using a command line interpreter -- open a command shell
and type in exactly what you think the command line looks like, and see
what the command line says... Hint: you have spaces in the paths
win32file.CopyFile (source, target, 1)

That is NOT from the code you posted, since you have commented out
the only line I saw with the *

Note that there is NO ".PST" file specified! That is just a path
with a wildcard. Are you planning to copy ALL files that are found?

Compare:
-=-=-=-=-=-=-
import os
import win32file
import time
from win32com.shell import shell, shellcon

local_app_data = shell.SHGetSpecialFolderPath(0,
shellcon.CSIDL_LOCAL_APPDATA)
outlook_path = os.path.join(local_app_data, "Microsoft", "Outlook")

source = outlook_path

backup1 = r'e:\temp\outlook1'
today = backup1 + '_' + time.strftime ( '%Y-%m-%d')
now = time.strftime('%H.%M.%S')
target = today +'_'+ now
if not os.path.exists(target):
os.mkdir(target) # make directory
print 'Successfully created directory', target

cmline = 'xcopy /s "%s" "%s"' % (source, target) #QUOTES
print "'%s'" % cmline
os.system (cmline)

print "'%s'\n'%s'" % (source, target)
win32file.CopyFile (os.path.join(source, "Outlook.pst"), #FILES
os.path.join(target, "Outlook_x.pst"), 1)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
K

k.i.n.g.

Hi,

The following code has worked for me, I will continue from here to make
this further userfriendly.
More I would like to know how can i distribute my python code as self
installer package.
In the process of learning programming I would like take
OutlookBackup.py as my first project and learn accordingly. Please
guide me in this.

Once again thank you all of you for your valuable suggestions

Regards,
Kk
---------------------------------------------------------------------------------------------------
import os, sys ,shutil, win32file
import time
from win32com.shell import shell, shellcon

local_app_data = shell.SHGetSpecialFolderPath
(0,shellcon.CSIDL_LOCAL_APPDATA)
outlook_path = os.path.join (local_app_data, "Microsoft", "Outlook" )

# print outlook_path
#c:\documents and settings\060577\Local Settings\Application
Data\Microsoft\Outlook

source = outlook_path
#source = outlook_path +'\\*'
#print source

backup = 'D:\MailBackup'
backup1=r'D:\temp\outlook1'
backup2 = 'D:\MailBackup'
today = backup1 + '_' + time.strftime ( '%Y-%m-%d')
now = time.strftime('%H.%M.%S')
target = today +'_'+ now
shutil.copytree(source, target) # copy directory tree

-------------------------------------------------------------------------------------------------------
 
D

Dennis Lee Bieber

In the process of learning programming I would like take
OutlookBackup.py as my first project and learn accordingly. Please
guide me in this.

Once again thank you all of you for your valuable suggestions
First suggestion... Forget about just doing a simple /directory/
copy to a new destination -- consider using the zipfile module to create
a compressed ZIP format backup /file/ (no need for creating new
directories each time), and common tools available to open the one
desired.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top