file backup in windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • k.i.n.g.

    file backup in windows

    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\Applic ation
    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\\06057 7\\Local
    Settings\\Appli cation Data\\Microsoft \\Outlook\\* '
    backup = ' D:\\temp\\outlo ok '
    os.system ("xcopy /s %s %s" % (filepath, backup))
    -----------------------------------------


    Thank you,
    Kk

  • Fredrik Lundh

    #2
    Re: file backup in windows

    k.i.n.g. wrote:
    how can i make the following code work, I have probelm with filepath
    declaration.


    </F>

    Comment

    • John Machin

      #3
      Re: file backup in windows

      k.i.n.g. wrote:
      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\Applic ation
      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\\06057 7\\Local
      Settings\\Appli cation Data\\Microsoft \\Outlook\\* '
      backup = ' D:\\temp\\outlo ok '
      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\outlo ok'

      HTH,
      John

      Comment

      • k.i.n.g.

        #4
        Re: file backup in windows

        Thank You all for reply's so far
        <code>
        import os, sys
        from win32com.shell import shell, shellcon
        >
        local_app_data = shell.SHGetSpec ialFolderPath (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

        Comment

        • Mikael Olofsson

          #5
          Re: file backup in windows

          k.i.n.g. wrote:
          [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

          Comment

          • k.i.n.g.

            #6
            Re: file backup in windows

            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.SHGetSpec ialFolderPath
            (0,shellcon.CSI DL_LOCAL_APPDAT A)
            outlook_path = os.path.join (local_app_data , "Microsoft" , "Outlook")

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

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

            backup = 'D:\MailBackup'
            backup1=r'D:\te mp\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(so urce, 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\\outlo ok')
            #win32file.Copy File (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.CopyF ile (source, target, 1)
            -----------------------------------------------------------------------
            end
            ------------------------------------------------------------------


            ---------
            output
            ----------
            C:\Documents and Settings\060577 \Local Settings\Applic ation
            Data\Microsoft\ Outlook\*
            Successfully created directory D:\temp\outlook 1_2006-11-22_17.41.54

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

            Comment

            • MC

              #7
              Re: file backup in windows

              Hi!

              " are your friend.

              See, also:
              filepath = '"%HOMEPATH%\\L ocalSettings\\A pplication
              Data\\Microsoft \\Outlook\\*"'

              and %USERPROFILE% %APPDATA% etc.

              --
              @-salutations

              Michel Claveau


              Comment

              • Fredrik Lundh

                #8
                Re: file backup in windows

                "MC" wrote:
                " are your friend.
                to be precise, list2cmdline is your friend. see discussion and examples here:



                </F>



                Comment

                • k.i.n.g.

                  #9
                  Re: file backup in windows

                  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.p y 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.SHGetSpec ialFolderPath
                  (0,shellcon.CSI DL_LOCAL_APPDAT A)
                  outlook_path = os.path.join (local_app_data , "Microsoft" , "Outlook" )

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

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

                  backup = 'D:\MailBackup'
                  backup1=r'D:\te mp\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

                  -------------------------------------------------------------------------------------------------------

                  Comment

                  Working...