Hi,
I am trying to delete files from a folder in a loop using locals. The files have a number as prefix (1-2300ish) and the date when they were saved as suffix. I will get the following message: (note: file 26_2022-04-17.dta not found). However, when I manually enter the file name it works as expected, the file is deleted.
foreach ID in `IDlist' {
* A lot of data cleansing etc..
* Even more data cleansing etc..
* Now I want to delete the previous file, save a new with the prefix and todays date
* first delete the previous file (created in another date)
local s =insID
cd "/Users/jesper.eriksson/.../somefolder"
local file : dir . files "`s'_*"
local file2 : subinstr local file " " "", all
capture noisily erase `file2'
* After the previous line is where my problem is, where the error message is showing up
* Finally save the new file (with todays date)
local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
di "`todate'"
* Remove irritating space in local
local todate : subinstr local todate " " "", all
* Save
save "`s'_`todate'.dta", replace
}
As I said, when I use capture noisily erase 26_2022-04-17.dta the file is deleted as expected, however I canĀ“t get it to work with locals. I know you can not have wildcards in filenames.
Any tips?
I am trying to delete files from a folder in a loop using locals. The files have a number as prefix (1-2300ish) and the date when they were saved as suffix. I will get the following message: (note: file 26_2022-04-17.dta not found). However, when I manually enter the file name it works as expected, the file is deleted.
foreach ID in `IDlist' {
* A lot of data cleansing etc..
* Even more data cleansing etc..
* Now I want to delete the previous file, save a new with the prefix and todays date
* first delete the previous file (created in another date)
local s =insID
cd "/Users/jesper.eriksson/.../somefolder"
local file : dir . files "`s'_*"
local file2 : subinstr local file " " "", all
capture noisily erase `file2'
* After the previous line is where my problem is, where the error message is showing up
* Finally save the new file (with todays date)
local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
di "`todate'"
* Remove irritating space in local
local todate : subinstr local todate " " "", all
* Save
save "`s'_`todate'.dta", replace
}
As I said, when I use capture noisily erase 26_2022-04-17.dta the file is deleted as expected, however I canĀ“t get it to work with locals. I know you can not have wildcards in filenames.
Any tips?
Comment