0

Every morning I have to copy yesterday's file, paste it as today's file, refresh a PowerQuery via a macro and save the file with today's date.

I'm beginning to learn Python and I have found a way to copy and paste the file with today's date. Because I want it to run with no actual file name constraints, I want the code to be able to select the most recently modified file, which would be always be yesterday's file, copy it with today's date in the file name, refresh the PowerQuery and run the macro.

My code so far is a simple copy paste of the folders but I'd like to insert functions to have:

import shutil

original = r"C:\Users\name\Desktop\9.13.2021 - Daily Item Record.xlsm"
target = r"C:\Users\name\Desktop\9.14.2021 - Daily Item Record.xlsm"

shutil.copyfile(original, target)

The above involves changing the dates every day. Instead, I'd like to have the original file path be the most recent file in the directory and the target file be the copy with today's date.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
datoro
  • 59
  • 10
  • You can use `os.path.getmtime()` to get the file's modification time. Could you please provide some code so that we can better help you? Here is a similar question: [How to get file creation & modification date/times in Python?](https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python?rq=1) – Sylvester Kruin Sep 13 '21 at 20:27
  • Hey @SamMatzko, just put my very basic code. Hope it helps! Sorry... First time here, trying to understand how the site works in the meantime. – datoro Sep 13 '21 at 20:33
  • Welcome to Stack Overflow! I reccommend reading the [Help Center](https://stackoverflow.com/help), so that you can understand a little better how things run here. – Sylvester Kruin Sep 13 '21 at 20:35
  • It would also help me help you if you specified the exact problem. Are you having trouble getting a file's creation date? Or are you unsure about how to get the current date? Or something else? – Sylvester Kruin Sep 13 '21 at 20:38
  • @SamMatzko What I am having trouble with is instead of specifying the original and target file paths manually, I want the original to always be the most recent and the target to always contain today's date. That way when I run the code, the most recent file (yesterday's file) would be copied and renamed with today's date. – datoro Sep 13 '21 at 20:40
  • So do you want to create seperate file for each day, or to just change the one file every day? – Sylvester Kruin Sep 13 '21 at 20:52
  • @SamMatzko I want to create a copy of yesterday's file every morning with today's date. – datoro Sep 13 '21 at 20:54

1 Answers1

0

This should work:

import os
import shutil
import time

my_dir = "C:\\Users\\name\Desktop\\"

# A dictionary for storing the files
files_dict = {}

# Put the creation time and the path for each file in the directory into files_dict
for f in os.listdir(my_dir):
    file = my_dir + f
    file_createdtime = os.path.getctime(file)
    files_dict[file_createdtime] = file

# Get the most recently created file
most_recent_file = files_dict[max(files_dict)]

# Copy and rename the file
shutil.copy(file, os.path.dirname(file) + time.strftime("\\%m.%d.%Y - Daily Item Record.xlsm"))

In the for loop, I create a dictionary of each file's creation time and path. The max() function returns the highest value in an iterable sequence (in this case, the dictionary). In the code below, it returns the creation date for the most recently created file, which I can use to get the file path from the dictionary and put it in most_recent_file.

In the last line, I copy the file with a new name in the format 09.13.2021 - Daily Item Record.xlsm using the time.strftime() function. See the python documentation for more information on time.strftime. Basically what it does is replace certain patterns in the given string with certain units of time. Here, it replaces "%m" with the current month number, "%d" with the current day of the month, and "%Y" with the current year.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
  • I really appreciate you taking the time to go over this. I have modified the `my_dir` to fit my files and I'm trying to test it with some mock data but it is not working. The program runs and gives no errors though. I'm trying to debug on my end in case there is something I am missing. – datoro Sep 13 '21 at 21:47
  • In the debugging, I am seeing `(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape` as a SyntaxError. Not sure if this is useful to you. – datoro Sep 13 '21 at 21:49
  • Got it to work! Just needed ` \\ ` on the end of `my_dir`. This is awesome man. Thanks again! – datoro Sep 13 '21 at 21:56
  • @datoro: Yes, I got the same error for the same reason when working on the answer :-). – Sylvester Kruin Sep 13 '21 at 22:12