Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
R: Creating Sequential Filenames or Folders: AA, AA_01, AA_02, …

R: Creating Sequential Filenames or Folders: AA, AA_01, AA_02, …

Posted May 8, 2024 at 12:30 pm
Sang-Heon Lee
SHLee AI Financial Model

This post demonstrates how to generate sequential filenames or folders, like ‘AA’, ‘AA_01’, ‘AA_02’, and beyond.

Sequential Filenames or Folders

After saving estimation results in an ‘AA’ folder, subsequent results with different configurations are stored in sequentially numbered folders such as ‘AA_01’, ‘AA_02’, and so on. Of course, this version history management also applies to files.

This method is useful to save forecasting results with varying specifications, such as different models or sample periods, for ongoing comparative analysis.

Sequential Folders

The following R code is used for creating sequential folders.

setwd('your working directory')
 
# creates sequentially numbered folders 
create_numbered_folder <- function(str_nm, digits=2) {
    
    # Create the folder only if it doesn't exist
    if (!file.exists(str_nm)) { 
        folder_name <- str_nm
    } else {
        kk <- 1
        folder_name <- sprintf("%s_%0*d", str_nm, digits, kk)
        while (file.exists(folder_name)) {
            kk <- kk + 1
            folder_name <- sprintf("%s_%0*d", str_nm, digits, kk)
        }
    }
    dir.create(folder_name)
    return(folder_name)
}
 
# Example usage:
str_nm <- "AA"
new_folder <- create_numbered_folder(str_nm)
print(paste("Created folder:", new_folder))
> 
[1] "Created folder: AA"
> 
[1] "Created folder: AA_01"
> 
[1] "Created folder: AA_02"
>

Sequential Filenames

The following R code is used for creating sequential filenames.

# creates sequentially numbered file names 
create_numbered_file_name <- function(base_name, digits = 2, 
                                      extension = ".csv") {
    # Create the folder only if it doesn't exist
    base_name_we <- sprintf("%s%s", base_name, extension)
    if (!file.exists(base_name_we)) {
        file_name <- base_name_we
    } else {
        kk <- 1
        file_name <- sprintf("%s_%0*d%s", base_name, 
                             digits, kk, extension)
        while (file.exists(file_name)) {
            kk <- kk + 1
            file_name <- sprintf("%s_%0*d%s", base_name, 
                                 digits, kk, extension)
        }
        return(file_name)
    }
}
 
# Example usage:
base_file_name <- "FF"
new_file_name  <- create_numbered_file_name(base_file_name)
print(paste("file name:", new_file_name))
 
# writing row in the csv file 
write.table(NA, file = new_file_name, sep = ",", 
            append = TRUE, quote = FALSE, 
            col.names = FALSE, row.names = FALSE) 
> 
[1] "file name: FF.csv"
> 
[1] "file name: FF_01.csv"
> 
[1] "file name: FF_02.csv"
>

Originally posted on the SHLee AI Financial Model blog.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.