cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add a current date as suffix while using copy?

databicky
Contributor II

how to add a current date after filename suffix while copy from the dbutils like report20221223.xlsx

dbutils.fs.cp('dbfs://temp/balancing/report.xlsx','abfss://con@adls/provsn/result/report.xlsx',True)

i need to add the current date in the file like report20220123.xlsx while copying from source to target location

how can we add a current date after the suffix in filename while using dbutils.​

1 ACCEPTED SOLUTION

Accepted Solutions

Chaitanya_Raju
Honored Contributor

@Mohammed sadamusean​ hope the below code might help you,

from datetime import datetime
date_value = datetime.now().strftime("%Y%m%d")
 
src = 'dbfs:/FileStore/Test/File.csv'
trgt = f'dbfs:/FileStore/Test/File_{date_value}.csv' 
 
dbutils.fs.cp(src,trgt)

Happy Learning!!

View solution in original post

5 REPLIES 5

Aviral-Bhardwaj
Esteemed Contributor III

This will give you a little bit more idea

import time
 
timestr = time.strftime("%Y%m%d-%H%M%S")
 
print(timestr)

Now you can use formator and implement this solution

Thanks

Aviral

Chaitanya_Raju
Honored Contributor

@Mohammed sadamusean​ hope the below code might help you,

from datetime import datetime
date_value = datetime.now().strftime("%Y%m%d")
 
src = 'dbfs:/FileStore/Test/File.csv'
trgt = f'dbfs:/FileStore/Test/File_{date_value}.csv' 
 
dbutils.fs.cp(src,trgt)

Happy Learning!!

how can we get the data without hyphen​

You can try like below

from datetime import datetime
datevalue = datetime.now().strftime("%Y%m%d")
 
src = 'dbfs:/FileStore/Test/File.csv'
trgt = f'dbfs:/FileStore/Test/File{datevalue}.csv' 
 
dbutils.fs.cp(src,trgt)
 
 

Happy Learning!!

Kaniz
Community Manager
Community Manager

Hi @Mohammed sadamusean​, We haven’t heard from you since the last response from @Aviral Bhardwaj​ and @Ratna Chaitanya Raju Bandaru​, and I was checking back to see if their suggestions helped you.

Or else, If you have any solution, please do share that with the community as it can be helpful to others.

Also, Please don't forget to click on the "Select As Best" button whenever the information provided helps resolve your question.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.