Choose Language Hide Translation Bar
Highlighted
AY
AY
Occasional Contributor

How to delete previous folder and substitute with new folder?

I want to extract all data from my .csv file and put it inside a folder. Problem is everytime i run my code, it will generate different folder everytime because I use this line of code: 

 

 

newDir = filepath||Substitute(MDYHMS(Today()), "/", "-", ":", ".");

 

Question is, how can I generate new folder everytime but at the same time delete the previous created folder by using JMP script? I dont want my directory full with folder each time i run my script as I only need the latest folder. Below is part of my code. Thank you so much!

 

 

dt.file = Pick File();
//Get csv file directory path
prefilepath=substr(dt.file,1,length(dt.file)-length(word(-1,dt.file,"\/"))-1);
If( Host is( Mac ),
filepath="/"||prefilepath,
filepath = Convert File Path( prefilepath, Windows )
);
Show(filepath);
//Create new directory to save all the extracted .csv files
newDir = filepath||Substitute(MDYHMS(Today()), "/", "-", ":", ".");
CreateDirectory(newDir);
Show(newDir);

 

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Craige_Hales
Staff (Retired)

Re: How to delete previous folder and substitute with new folder?

Be careful, I've managed to delete the wrong directory at least once when writing code like this.

Use filesInDirectory(...) with the option to return directory names. Make sure you use a format for dates that sorts well (YYYYMMDD, for example, but not DDMMYYYY). Sort the list from FilesInDirectory, pick the smallest that matches your pattern. I recommend adding some extra text to the file name that you can check for, something like

files/DeleteTomorrow-2019-04-19.CSV

that will help prevent a mistake a year later when someone else is maintaining the code.

There are lots of variations possible for keeping several versions, prompting when something seems odd, etc.

Craige

View solution in original post

1 REPLY 1
Highlighted
Craige_Hales
Staff (Retired)

Re: How to delete previous folder and substitute with new folder?

Be careful, I've managed to delete the wrong directory at least once when writing code like this.

Use filesInDirectory(...) with the option to return directory names. Make sure you use a format for dates that sorts well (YYYYMMDD, for example, but not DDMMYYYY). Sort the list from FilesInDirectory, pick the smallest that matches your pattern. I recommend adding some extra text to the file name that you can check for, something like

files/DeleteTomorrow-2019-04-19.CSV

that will help prevent a mistake a year later when someone else is maintaining the code.

There are lots of variations possible for keeping several versions, prompting when something seems odd, etc.

Craige

View solution in original post