Question

The function dir() (same as the list.files function) can be used to list files in a...

The function dir() (same as the list.files function) can be used to list files in a directory. Read the help file on the dir() function. Write code that will allow you to list the files in a directory and all subdirectories. Include the code in your answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us setup this test directory structure

C:UsersjhollaDesktop ry--> having

2 sub directories dir1 and dir2 and some files

further dir1 has a subdirectory dir11 and some fiiles

The following R code lists the files and the subdirectories

R code (all statements starting with # are comments)

#use path= to give the starting directory,
#   note the reverse "/" which separates the directory names
# use recursive=TRUE to list the subdirectories
dir(path='C:/Users/jholla/Desktop/try',recursive=TRUE)

#output is

> #use path= to give the starting directory, > #note the reve r 3 e / which 3eparates the directory name 3 > # use recursive-TRUE t list the subdirectories > dir (path-C:/Users/jholla/Desktop/try, recursive TRUE) [11 dirl/dir11/datbase11.accdb dirl/dir11/file11.txt [5] dir1/file1.txt 91 dir2/sheet2.xlsx dirl/sheet1.xlsx file.txt dirl/dir11/ppt11.pptx dir2/file2.txt rarfile.rar dir1/doc1.docx dir2/ppt2.pptx sheet.xlsx

the listing has the files in the directory C:UsersjhollaDesktop ry and the sub directories under it.

if we use the recursive=FALSE (this is the default and hence omitting this also has the same effect), we get

#use path= to give the starting directory,
#   note the reverse "/" which separates the directory names
# use recursive=TRUE to list the subdirectories
dir(path='C:/Users/jholla/Desktop/try')

#get the following

> #use path-to give the starting directory, > #note the reverse / which separates the directory names > # use recursive=TRUE to list the subdirectories > dir(path C:/Users/jholla/Desktop/try) [11 dir1 dir2 file.txtrarfile.rar sheet.xlsx

Suppose we set the directory first to C:UsersjhollaDesktop ry . a dot as the path, (path='.'), would list the current directory.

#change the directory to the desired path
setwd('C:/Users/jholla/Desktop/try')
#use path= to give the starting directory,
#   note the reverse "/" which separates the directory names
# use recursive=TRUE to list the subdirectories
dir(path='.',recursive=TRUE)

# get the output

> #change the directory to the desired path > setwd (C:/Users/jholla/Desktop/try) > #use path# to give the starting directory, > #note the reve r 3 e / which 3eparates the directory names > # use recursive=TRUE to list the subdirectories > dir (path=. , recursive=TRUE) I1] dir1/diri1/datbase11.accdb dirl/dirl1/file11.txt [5] dir1/filel.txt 91 dir2/sheet2.xlsx dir1/sheetl.xlsx file txt dir1/dir11/ppt11.pptx dir2/file2.txt rarfile.rar dirl/doc1.docx dir2/ppt2.pptx sheet.xlsx

Add a comment
Know the answer?
Add Answer to:
The function dir() (same as the list.files function) can be used to list files in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • What program do you use to list the files in a directory? Which option do you...

    What program do you use to list the files in a directory? Which option do you use to display: All files, including dotfiles (hidden files) Names in reverse alphabetical order Entire tree, including all subdirectories Flags after each name (/ = directory) Size of each file, in human readable units Long listings (permissions, owner, and so on) Information about a directory itself, not its contents ​When you look at a directory listing, what do the entries . and .. mean?

  • Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program...

    Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program should count the (regular) files in the specified directory as well as all subdirectories and output the total number on the console. Files and subdirectories whose names .start with should be ignored! To do this, define a function int countFilesRec(char* dirName)that dirName returns the number of (regular) files in the directory and all the subdirectories. Call the function recursively to count the...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • I need help with this. 22. Show a command that will create a file named "etc.tar"...

    I need help with this. 22. Show a command that will create a file named "etc.tar" in your home directory from recursively going through all of the files in the "/etc" directory. In other words, the "etc.tar" file will include the files in the "/etc" directory as well as all of its subdirectories. IMPORTANT: This command must work for any user as well as work from any directory. 23. In bash, multiple commands can appear on a single command line...

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • COSC 3411 /ITAP 3411 Homework (UNIX Shell Commands) How would you ensure that all ordinary files...

    COSC 3411 /ITAP 3411 Homework (UNIX Shell Commands) How would you ensure that all ordinary files created by you have rw-rw---- as default permissions? How would you sort in the background a file called "bad.txt", and place the results in a file called "sort.txt"? Archive the contents of your home directory (including any subdirectories) using tar. Compress the tar archive with gzip. Now extract their contents. Use the “find” command to locate in /docs and /usr/docs all files that Begin...

  • I'm stuck, if you can provide the code I can put it into the BASH environment...

    I'm stuck, if you can provide the code I can put it into the BASH environment Executing tasks: In this section of the project, you will demonstrate your ability to execute Linux commands to create files and create and organize the Linux directory structure. At the end of this section, you will create a log file that will include a list of all the commands you used to complete these steps. VII. In the workspace directory, create new directories titled...

  • Reading and Writing Complete Files in C: The first part of the lab is to write...

    Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...

  • 1.Write a find command that will display the inode number of all files in the working...

    1.Write a find command that will display the inode number of all files in the working directory (only) that contain the string "#include". 2.Write an ls command with a glob specification that will list the names of all files in the working directory that are exactly 10 characters long, begin with w, x, y, or z, and do not contain a "." character (i.e. no file extension). 3.Write an ls command with a glob specification that will list the names...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT