Skip to content

Instantly share code, notes, and snippets.

@r-plus
Created August 3, 2014 03:49
Show Gist options
  • Save r-plus/49d595a22398a063da54 to your computer and use it in GitHub Desktop.
Save r-plus/49d595a22398a063da54 to your computer and use it in GitHub Desktop.
Output file size and max length of filepath per division to csv from robocopy log.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Usage:
# script.py <robocopy.log> [robocopy.log]...
import re
from sys import argv
# key = division name
# value = list of total file size and max file path length
info_per_division = {}
for file in argv:
if file == argv[0]:
continue
f = open(file)
for line in f:
# get file size, path and path length.
l = line.strip().split()
size = long(l[0])
file_path = ''
for path in l[1:]:
file_path += ' ' + path
file_path = file_path.lstrip()
uni_str = file_path.decode('cp932')
file_path_length = len(uni_str)
# get division.
division = ''
r = re.compile(r"\\(A[0-9]{1,2}_.*?)\\")
result = r.search(file_path)
if result is not None:
division = result.group(1)
# update information.
if division not in info_per_division:
info_per_division[division] = [size, file_path_length]
else:
total_size = info_per_division[division][0]
max_filepath_length = info_per_division[division][1]
info_per_division[division] = [
total_size + size,
max_filepath_length if max_filepath_length > file_path_length else file_path_length]
f.close()
print '部署名, total_size, max_filepath_length'
for division in info_per_division:
print division + ', ' + str(info_per_division[division][0]) + ', ' + str(info_per_division[division][1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment