获取目录中的文件名列表

问题描述 投票:1回答:1

如果我有一个完整的目录文件和子的,我怎么就能够用R获取文件名,并将其输出为CSV的所有列表?

例如,在C:\Files,我有test1test2test3的子文件夹,并有pic1.jpg为一个文件。在test1子文件夹中,我还具有其他文件和子文件夹。

目前,我的做法是设置每端子文件夹的工作目录,然后列出了所有的文件名,将它们转换成一个数据帧之前。最后,我所有的行合并为一个数据集。

除了副本的替代方法和手动粘贴的所有文件到一个单一的文件夹,然后列出文件名,会有越来越目录名称列表的更有效的方法?

r
1个回答
0
投票

下面的代码将有助于

# To list all files of a folder in a list variable including files 
# from sub-folders. The code below gets the full path of files not just names.
list = list.files(path = full_path_to_directory ,full.names=TRUE,recursive=TRUE)
# To get names of all files from their corresponding paths in all_names variable.
all_names = basename(list)
# To write all_names variable to a CSV file.
write.csv(all_names, "test.csv")
© www.soinside.com 2019 - 2024. All rights reserved.