当前位置:网站首页>list. files: List the Files in a Directory/Folder

list. files: List the Files in a Directory/Folder

2022-07-05 07:14:00 RS&Hydrology

Reprint :https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/list.files

1.list.files Function function
Save the file name in the folder to the list , It is often used to import files in batches . That is, this function can generate the character vector of the specified directory file or directory name .

2.list.files Usage method

list.files(path = ".", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
      

Parameters :

  • path: Enter the file path ;
  • pattern: An optional regular expression . Only file names that match regular expressions will be returned ( That is, match the file name , Select the desired file , Default is all file names , For example, you want to select all formats as ‘.txt’ The file of ,pattern="*.txt$"), Indicates that the output is in .txt Final document , Be sure to add $ Symbol , Otherwise, only the file name will be selected txt The file of ).
  • all.files: Determine whether to return all files , The default value is no ;
  • full.names: Determine the return path + file name , The default value is no ( That is, only the file name is returned );
  • recursive: Whether to list the files in subfolders , The default value is no ;
  • ignore.case: Whether matching file names ignore case , Default no , That is, case is not ignored .

It can also be used list.dirs Get directory list :

dir(path = ".", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

list.dirs(path = ".", full.names = TRUE, recursive = TRUE)

3. Example

#1. Get the working directory:
getwd()

#2. Setup the working directory:
setwd("C:/Documents and Settings/Folder name")

#3.list files in a specific folder
list.files (path = "C:/Folder/Subfolder1/Subfolder2")

#4.  Extract all files in the format ‘.txt’ file 
list.files(path = "C:/Folder/",pattern = ".txt$")
原网站

版权声明
本文为[RS&Hydrology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140559171662.html