Skip to content

Instantly share code, notes, and snippets.

@AlexCallejas
Created February 27, 2018 04:27
Show Gist options
  • Save AlexCallejas/18006491df9408295031ed134dd9c343 to your computer and use it in GitHub Desktop.
Save AlexCallejas/18006491df9408295031ed134dd9c343 to your computer and use it in GitHub Desktop.
from os import path
from os import listdir
import re
import shutil
import datetime
source_dir = 'workdir'
dest_dir = 'newfiles'
def moveFile(filename, new_filename):
source = path.abspath('./' + source_dir) + '/' + filename
destination = path.abspath('./' + dest_dir) + '/' + new_filename
print(source, destination)
shutil.copy(source, destination)
def getFiles():
p = path.abspath('./' + source_dir)
return listdir(p)
def getDateSubstring(file_name):
pattern = r'(?<=\d{4}).{14}(?=\D)'
matches = re.finditer(pattern, file_name)
for matchNum, match in enumerate(matches):
hour = match.group()
break
return hour
def addSecond(date):
initial_date = datetime.datetime.strptime(date, "%d%m%Y%H%M%S")
result_date = initial_date + datetime.timedelta(seconds=1)
return result_date.strftime("%d%m%Y%H%M%S")
def getNewFilename(file_name):
date = getDateSubstring(file_name)
new_date = addSecond(date)
return file_name.replace(date, new_date, 1)
def main():
for filename in getFiles():
new_filename = getNewFilename(filename)
moveFile(filename, new_filename)
main()
@AlexCallejas
Copy link
Author

Script en python, ganador del #SysArmyMx Challenge:
http://www.rootzilopochtli.com/2018/02/sysarmymx-challenge/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment