3

I am fairly new to R programming and I apologize if this question has been answered already; I did search for an answer, but perhaps my wording is off.

I have imported a TXT file, performed my analysis and transformation of the data and now wish to write a CSV file for export. However, since this script is meant to run multiple files, I would like to use the file name from the input TXT file as the output CSV file.

>read.csv("C:\\Users\\Desktop\\filename.txt", header=FALSE)
>...
>...
>write.csv(Newfile, "filename.csv")

As an example, I want to be able to take the 'filename' portion of the pathway and (I would assume) create a string variable to pull into the name of the CSV file I want to write.

I know this is beginner level stuff, but any help would be appreciated. Thanks!

lmo
  • 37,904
  • 9
  • 56
  • 69
EScientist_WH
  • 31
  • 1
  • 2
  • 1
    Take a look at `?basename`. It should return what you need if you pass it the original filename (`basename("C:\Users\Desktop\filename.txt")`) – talat Oct 21 '16 at 12:38
  • How do you get all the file names? Everything in a folder? Or something like `filename1.txt, ...`? In general you can just split your `path2file <- paste0("C:\\Users\\Desktop\\", filename", ".txt")` and reuse `filename`. – Christoph Oct 21 '16 at 16:10
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Oct 28 '16 at 17:18

1 Answers1

1

We can keep the filename and path in a variable then manipulate to make output filename:

myInputFile <- "C:\\Users\\Desktop\\filename.txt"
myOutFile <- paste0(tools::file_path_sans_ext(myInputFile),".csv")

# test
myInputFile
# [1] "C:\\Users\\Desktop\\filename.txt"
myOutFile
# [1] "C:\\Users\\Desktop\\filename.csv"

Or more general approach, I use below to keep track of my ins and outs:

# define folders
folderWD <- "/users/myName/myXproject/"
folderInput <- paste0(folderWD, "data/")
folderOutput <- paste0(folderWD, "output/")

# input output files
fileInput <- paste0(folderInput, "filename.txt")
fileOutput <- paste0(folderOutput, tools::file_path_sans_ext(basename(fileInput)), ".csv")

# test
fileInput
# [1] "/users/myName/myXproject/data/filename.txt"
fileOutput
# [1] "/users/myName/myXproject/output/filename.csv"

#then codez
myInputData <- read.csv(fileInput, header = FALSE)
...
Newfile <- # do some stuff with myInputData
...
write.csv(Newfile, fileOutput)
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • What if I wanted to take just a section of my basename and include it in the output file name when writing the output file? Let's say I have this: "C:\\Users\\file_2016_10.txt" How can I pull out just "2016_10" without the extension and append that to my file name when writing my output file? – EScientist_WH Oct 28 '16 at 17:16
  • @EScientist_WH for example? input and expected output, please. – zx8754 Oct 28 '16 at 17:17
  • So I want to read in the above mentioned file and write a CSV with the "2016_10" string attached to the file name. For example: >read.csv("C:\\Users...\\file_2016_10.txt") >... >Output_file #after analysis and transformation > >write.csv(Output_file, "Output_file 2016_10") – EScientist_WH Oct 28 '16 at 17:24
  • @EScientist_WH This should work: `paste0(dirname(myInputFile), "/Output_", tools::file_path_sans_ext(basename(myInputFile)), ".csv")`, if not then you will need to experiment with mentioned functions to get desired output. – zx8754 Oct 28 '16 at 18:03