Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System
- import System.IO
- import System.Diagnostics
- def NumberFromFileName(fileName as string):
- match = /(?<name>.*\.old)(?<number>\d{2})/.Match(fileName)
- if match==null or not match.Success:
- raise InvalidOperationException(fileName)
- return match.Groups['name'].ToString(),int.Parse(match.Groups['number'].ToString())
- def ZipTo(inputDirectory as string,outputFileName as string,backupDirectoryName as string):
- backupFile=Path.Combine(inputDirectory,Path.Combine(backupDirectoryName,outputFileName))
- startInfo=ProcessStartInfo()
- startInfo.UseShellExecute=true
- startInfo.WorkingDirectory=inputDirectory
- startInfo.WindowStyle=ProcessWindowStyle.Hidden
- startInfo.FileName="7za.exe"
- startInfo.Arguments="""a -r -tzip -x!"${backupDirectoryName}" "${backupFile}" ."""
- process as Process=null
- try:
- process=Process.Start(startInfo)
- process.WaitForExit()
- except e as Exception:
- print "exception occured zipping data, '${e.GetType().Name}, ${e.Message}'"
- exitCode=process.ExitCode
- if exitCode!=0:
- message as string=null
- if exitCode==1:
- message="""Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed."""
- elif exitCode==2:
- message="""Fatal error"""
- elif exitCode==7:
- message="""Command line error"""
- elif exitCode==8:
- message="""Not enough memory for operation"""
- elif exitCode==255:
- message="""User stopped the process"""
- # raise Exception(message)
- print message
- Console.ReadLine()
- def Main(args as (string)):
- if not args or args.Length<1:
- print("backup <directory>")
- return
- argumentDirectory=args[0]
- backupSubDir='_backup'
- directory=argumentDirectory
- backupName="{0} {1:ddMMyyyyy}.zip" % (Path.GetFileName(directory),DateTime.Now)
- print "backupSubDir='${backupSubDir}'"
- print "backupName ='${backupName}'"
- print "directory ='${directory}'"
- backupDirectory=Path.Combine(directory,backupSubDir)
- if not Directory.Exists(backupDirectory):
- print 'created backup directory'
- Directory.CreateDirectory(backupDirectory)
- backupFile=Path.Combine(directory,Path.Combine(backupSubDir,backupName))
- if File.Exists(backupFile):
- print 'moving existing files'
- files = [file for file in Directory.GetFiles(Path.Combine(directory,backupSubDir)) if file=~/old\d{2}/]
- files.Sort()
- for file in reversed(files):
- name,number as int = NumberFromFileName(file)
- File.Move(file,"${name}${(number+1).ToString('D2')}")
- if File.Exists(backupFile):
- File.Move(backupFile,"${backupFile}.old01")
- ZipTo(directory,backupName,backupSubDir);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement