Hi Everyone, I am trying to loop through the folders in the major working directory. Read the dbf file into the data frame then save the data frame as CSV file in another folder. For this, I have written this code, But not able to figure out where it is going wrong. Any ideas will be of great support. setwd(choose.dir()) csvpath= "C:/plan/Learning/dummydata/csv/" a<-list.dirs() inpath<-"C:/workplan/Q2/Project1" for (folder in list.dirs()[-1]) { path<-setwd(paste0("inpath",folder)) dbf<-list.files(path, pattern = "*ward.dbf") df <- read.dbf(dbf) dbfname<-basename(dbf) name<-file_path_sans_ext(dbfname) # get the name of the file like agra_ward write.csv( df, file = paste0("csvpath",name,"csv")) print(path) } -- Thanks & Regards, Shubhasmita Sahani [[alternative HTML version deleted]]
What package is "read.dbf" from? What error message/behavior did you see? Should it be: path<-setwd(paste0("inpath/",folder)) ## did you forget the "/" ? Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Apr 24, 2020 at 7:08 AM Shubhasmita Sahani <shubhasmita.sahani at gmail.com> wrote:> > Hi Everyone, > I am trying to loop through the folders in the major working directory. > Read the dbf file into the data frame then save the data frame as CSV file > in another folder. > For this, I have written this code, But not able to figure out where it is > going wrong. Any ideas will be of great support. > > > setwd(choose.dir()) > csvpath= "C:/plan/Learning/dummydata/csv/" > a<-list.dirs() > inpath<-"C:/workplan/Q2/Project1" > > for (folder in list.dirs()[-1]) { > > path<-setwd(paste0("inpath",folder)) > dbf<-list.files(path, pattern = "*ward.dbf") > df <- read.dbf(dbf) > dbfname<-basename(dbf) > name<-file_path_sans_ext(dbfname) # get the name of the file like > agra_ward > write.csv( df, file = paste0("csvpath",name,"csv")) > print(path) > > } > > > > > > -- > Thanks & Regards, > Shubhasmita Sahani > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
I suspect much if not all of your trouble would be eliminated by using file.path() instead of paste0(). https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/file.path (Also check your file name - you probably want a . between name and csv, so using paste(name, "csv", sep = ".") would create a more usual file name. It's always a good idea to work thru your loop by hand once, and look at all the intermediate steps. Often that quickly shows you where you went wrong. Sarah On Fri, Apr 24, 2020 at 10:08 AM Shubhasmita Sahani <shubhasmita.sahani at gmail.com> wrote:> > Hi Everyone, > I am trying to loop through the folders in the major working directory. > Read the dbf file into the data frame then save the data frame as CSV file > in another folder. > For this, I have written this code, But not able to figure out where it is > going wrong. Any ideas will be of great support. > > > setwd(choose.dir()) > csvpath= "C:/plan/Learning/dummydata/csv/" > a<-list.dirs() > inpath<-"C:/workplan/Q2/Project1" > > for (folder in list.dirs()[-1]) { > > path<-setwd(paste0("inpath",folder)) > dbf<-list.files(path, pattern = "*ward.dbf") > df <- read.dbf(dbf) > dbfname<-basename(dbf) > name<-file_path_sans_ext(dbfname) # get the name of the file like > agra_ward > write.csv( df, file = paste0("csvpath",name,"csv")) > print(path) > > } > > > > > > -- > Thanks & Regards, > Shubhasmita Sahani >-- Sarah Goslee (she/her) http://www.numberwright.com
Hi, I am sorry if I am misunderstanding what you are trying to do here, but can you simplify it this way? (unfortualtely, this is untested since I dont have a suitable set of files and a directory structure to test against) dbifiles <- list.files(pattern="*.dbi",recursive=TRUE) csvfiles <- gsub("dbi$","csv",dbifiles) for(i in seq_along(csvfiles)){ df <- read.dbf(dbfiles[i]) write.csv( df, file =csvfiles[i]) } or something along these lines? Fredrik On Fri, Apr 24, 2020 at 4:08 PM Shubhasmita Sahani < shubhasmita.sahani at gmail.com> wrote:> Hi Everyone, > I am trying to loop through the folders in the major working directory. > Read the dbf file into the data frame then save the data frame as CSV file > in another folder. > For this, I have written this code, But not able to figure out where it is > going wrong. Any ideas will be of great support. > > > setwd(choose.dir()) > csvpath= "C:/plan/Learning/dummydata/csv/" > a<-list.dirs() > inpath<-"C:/workplan/Q2/Project1" > > for (folder in list.dirs()[-1]) { > > path<-setwd(paste0("inpath",folder)) > dbf<-list.files(path, pattern = "*ward.dbf") > df <- read.dbf(dbf) > dbfname<-basename(dbf) > name<-file_path_sans_ext(dbfname) # get the name of the file like > agra_ward > write.csv( df, file = paste0("csvpath",name,"csv")) > print(path) > > } > > > > > > -- > Thanks & Regards, > Shubhasmita Sahani > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- "Life is like a trumpet - if you don't put anything into it, you don't get anything out of it." [[alternative HTML version deleted]]