Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import System
  2. import System.IO
  3. import System.Diagnostics
  4.  
  5. def NumberFromFileName(fileName as string):
  6. match = /(?<name>.*\.old)(?<number>\d{2})/.Match(fileName)
  7. if match==null or not match.Success:
  8. raise InvalidOperationException(fileName)
  9. return match.Groups['name'].ToString(),int.Parse(match.Groups['number'].ToString())
  10.  
  11. def ZipTo(inputDirectory as string,outputFileName as string,backupDirectoryName as string):
  12. backupFile=Path.Combine(inputDirectory,Path.Combine(backupDirectoryName,outputFileName))
  13. startInfo=ProcessStartInfo()
  14. startInfo.UseShellExecute=true
  15. startInfo.WorkingDirectory=inputDirectory
  16. startInfo.WindowStyle=ProcessWindowStyle.Hidden
  17. startInfo.FileName="7za.exe"
  18. startInfo.Arguments="""a -r -tzip -x!"${backupDirectoryName}" "${backupFile}" ."""
  19.  
  20. process as Process=null
  21. try:
  22. process=Process.Start(startInfo)
  23. process.WaitForExit()
  24. except e as Exception:
  25. print "exception occured zipping data, '${e.GetType().Name}, ${e.Message}'"
  26. exitCode=process.ExitCode
  27. if exitCode!=0:
  28. message as string=null
  29. if exitCode==1:
  30. message="""Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed."""
  31. elif exitCode==2:
  32. message="""Fatal error"""
  33. elif exitCode==7:
  34. message="""Command line error"""
  35. elif exitCode==8:
  36. message="""Not enough memory for operation"""
  37. elif exitCode==255:
  38. message="""User stopped the process"""
  39. # raise Exception(message)
  40. print message
  41. Console.ReadLine()
  42.  
  43.  
  44. def Main(args as (string)):
  45. if not args or args.Length<1:
  46. print("backup <directory>")
  47. return
  48. argumentDirectory=args[0]
  49.  
  50. backupSubDir='_backup'
  51. directory=argumentDirectory
  52. backupName="{0} {1:ddMMyyyyy}.zip" % (Path.GetFileName(directory),DateTime.Now)
  53.  
  54. print "backupSubDir='${backupSubDir}'"
  55. print "backupName ='${backupName}'"
  56. print "directory ='${directory}'"
  57.  
  58. backupDirectory=Path.Combine(directory,backupSubDir)
  59. if not Directory.Exists(backupDirectory):
  60. print 'created backup directory'
  61. Directory.CreateDirectory(backupDirectory)
  62.  
  63. backupFile=Path.Combine(directory,Path.Combine(backupSubDir,backupName))
  64. if File.Exists(backupFile):
  65. print 'moving existing files'
  66. files = [file for file in Directory.GetFiles(Path.Combine(directory,backupSubDir)) if file=~/old\d{2}/]
  67. files.Sort()
  68.  
  69. for file in reversed(files):
  70. name,number as int = NumberFromFileName(file)
  71. File.Move(file,"${name}${(number+1).ToString('D2')}")
  72.  
  73. if File.Exists(backupFile):
  74. File.Move(backupFile,"${backupFile}.old01")
  75.  
  76. ZipTo(directory,backupName,backupSubDir);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement