列出 R 目录中嵌套的 Excel 个文件
List nested Excel files in R directories
我在 Windows 10.
上使用的目录结构如下
C:
├── my-R-working-directory/
├── excel01.xlsx
├── excel02.xlsx
├── nested-folder-A
| ├── excel03.xlsx
└── nested-folder-B
└── excel04.xlsx
我可以使用以下命令直接在我的工作目录中列出所有 Excel 文件:
getwd()
#> "C:/my-R-working-directory"
list.files("path/of/folder", pattern = "\.xlsx$", full.names = TRUE)
#> [1] "C:/my-R-working-directory/excel01.xlsx
#> [2] "C:/my-R-working-directory/excel02.xlsx
我可以为 pattern
参数使用什么正则表达式来列出所有 Excel 嵌套一层的文件?换句话说,我只想在 nested-folder-A
和 nested-folder-B
中看到 Excel 个文件,它们是 excel03.xlsx
和 excel04.xlsx
(以及任何其他 Excel文件夹中的文件恰好是一层深)。
我的想法是使用您在下面看到的内容,但它 returns character(0)
,我认为这意味着我的正则表达式是错误的:
list.files("path/of/folder", pattern = "\\.xlsx$", full.names = TRUE)
#> character(0)
我使用在工作目录中查找的 pattern = "\.xlsx$"
并尝试向上一级,即 pattern = "\\.xlsx$"
。尝试失败。正确的做法是什么?
如果您查看 list.files()
中的 recursive
参数,它可以让您看到较低级别目录中的文件。结合 pattern = "\.xlsx$"
将匹配所有递归目录中的 Excel 文件。
list.files("path/of/folder", pattern = "\.xlsx$", full.names = TRUE, recursive = TRUE)
如果您想执行 level-specific 递归列表文件,这个 可能会有所帮助。
我在 Windows 10.
上使用的目录结构如下C:
├── my-R-working-directory/
├── excel01.xlsx
├── excel02.xlsx
├── nested-folder-A
| ├── excel03.xlsx
└── nested-folder-B
└── excel04.xlsx
我可以使用以下命令直接在我的工作目录中列出所有 Excel 文件:
getwd()
#> "C:/my-R-working-directory"
list.files("path/of/folder", pattern = "\.xlsx$", full.names = TRUE)
#> [1] "C:/my-R-working-directory/excel01.xlsx
#> [2] "C:/my-R-working-directory/excel02.xlsx
我可以为 pattern
参数使用什么正则表达式来列出所有 Excel 嵌套一层的文件?换句话说,我只想在 nested-folder-A
和 nested-folder-B
中看到 Excel 个文件,它们是 excel03.xlsx
和 excel04.xlsx
(以及任何其他 Excel文件夹中的文件恰好是一层深)。
我的想法是使用您在下面看到的内容,但它 returns character(0)
,我认为这意味着我的正则表达式是错误的:
list.files("path/of/folder", pattern = "\\.xlsx$", full.names = TRUE)
#> character(0)
我使用在工作目录中查找的 pattern = "\.xlsx$"
并尝试向上一级,即 pattern = "\\.xlsx$"
。尝试失败。正确的做法是什么?
如果您查看 list.files()
中的 recursive
参数,它可以让您看到较低级别目录中的文件。结合 pattern = "\.xlsx$"
将匹配所有递归目录中的 Excel 文件。
list.files("path/of/folder", pattern = "\.xlsx$", full.names = TRUE, recursive = TRUE)
如果您想执行 level-specific 递归列表文件,这个