简体   繁体   English

R:获取目录中所有文件的短文件名(文件名)列表

[英]R: get a list of short file names (filenames) for all files in a directory

Problem 问题

list.files(Path) returns a vector of names of files in the directory Path . list.files(Path)返回目录Path的文件名称向量。 This is great, but I need a vector of short file names (SFN). 这很好,但我需要一个短文件名(SFN)的向量。 For instance, the SFN for WageDataFile.csv is WAGEDA~1.csv (if there is no other file in the directory with the stem "WageDa"). 例如,WageDataFile.csv的SFN是WAGEDA~1.csv(如果目录中没有其他文件,其中包含词干“WageDa”)。 The SFN is also called the 8.3 filename. SFN也称为8.3文件名。

Desired solution 期望的解决方案

Specifically, I am hoping for a function that will pull the SFN from the OS rather than reconstruct it from the output of list.files() (but methods to reconstruct the SFN from the output of list.files() are welcome, too). 具体来说,我希望有一个函数可以从操作系统中提取SFN而不是从list.files()的输出重构它(但是也欢迎从list.files()的输出重构SFN的方法) 。

Code for reproducible case 可重复案件的代码

This will create a set of files at "E:/FileNameTest" with variable name lengths: 这将在“E:/ FileNameTest”创建一组具有可变名称长度的文件:

setwd("E:/FileNameTest")
library(stringi)
nFiles = 12
minNameLength = 2
maxNameLength = 12
set.seed(1)
FileNames = 
    stri_rand_strings(nFiles, 
                length=sample(minNameLength:maxNameLength,nFiles,replace=T), 
                '[A-Za-z0-9]')
file.create(FileNames)

Here is the content of FileNames : 这是FileNames的内容:

 [1] "lUizNmvDe7"   "GN0Nr"        "LTbUBpfn"     "6i"           "Poe"          "mYWm1Tjg"    
 [7] "TrRF46JWfPuI" "SKe"          "FTl5sLqLKTtr" "OmxQ"         "iO"           "KkCi7F" 

Here is the list of SFN that I need from those file names (edit: the names that were shortened should be in all caps): 以下是我需要从这些文件名中获取的SFN列表(编辑:缩短的名称应该全部大写):

[1] "6i"       "FTL5SL~1" "GN0Nr"    "iO"       "KkCi7F"   "LTbUBpfn" "LUIZNM~1" "mYWm1Tjg" "OmxQ"    
[10] "Poe"      "SKe"      "TRRF46~1"

Here is a solution (based on a comment from MrFlick) that may rely on invalid assumptions about the output of dir . 这是一个解决方案(基于MrFlick的评论),可能依赖于关于dir输出的无效假设。 Specifically, I assume that the header and footer of the output always take up 5 and 2 lines, respectively, and that the width of fields in the dir /x/a:-d output is always constant. 具体来说,我假设输出的页眉和页脚总是分别占用5行和2行,并且dir /x/a:-d输出中的字段宽度始终是常量。

library(stringr)
FileList = shell("dir /x/a:-d", intern=T)
FileList = FileList[6:(length(FileList)-2)]
SFN = str_replace(str_sub(FileList, 39, 46), "        ", "")
Name = str_sub(FileList, 52, -1)
NameList = ifelse(str_length(SFN)>0, SFN, Name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM