I would like to edit some files names to move the date from the end of the file name to the beginning. I have both .xlsx files and .csv files. All of the dates at the end of the file names are formatted as DD-Mon-YY, and the length of the file names are variable.
I have attempted to use a local for file date, then subinstr to remove the file date, then save as intended with the date then the base file name. When I run this code I get files saved with the file date, but no base file name. With this method, I would need to repeat with edits to have the same process for .xlsx files due to the position of the date within the file name.
Please let me know if there are suggestions on how to fix and/or improve this method. Appreciate any suggestions.
Code:
*** 0.A Initializing local macros for filepaths and files ******************************************** local fldr_archive "I:/Archive" *************************************************************************************************** *** PART 1: Changing working directory and Load file(s) **************************************************************************************************** ***1A. Change working directory cd "`fldr_archive'" ***1B. Load files dir *.csv local filelist: dir "." files "*.csv" di `"`filelist'"' foreach file of local filelist { local file_date = substr("`file'",-13,9) local wanted = subinstr("`file'","`file_date'","",1) export delimited using "`fldr_archive'/`file_date'-`wanted'.csv", replace clear }
Please let me know if there are suggestions on how to fix and/or improve this method. Appreciate any suggestions.
Comment