我嘗試了以下命令(在 powershell 視窗中有效)
system('powershell -command \"Get-ChildItem -Filter "*.html" | Where-Object { $_.LastWriteTime -ge "11/12/2021 09:10:00" }\"')
但是,從 R 控制臺我得到錯誤:
At line:1 char:79
... er *.html | Where-Object { $_.LastWriteTime -ge 11/12/2021 09:10:00 }
~~~~~~~~
Unexpected token '09:10:00' in expression or statement.
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : UnexpectedToken
[1] 1
編輯
使用 Mikael Jagan 的想法,我設法得到了一個結果:
system(paste('powershell -command ',shQuote('Get-ChildItem -Filter "*.html" | Where-Object { $_.LastWriteTime -ge "11/30/2021 09:10:00" }')))
但是,如下所示,結果是字符向量而不是資料幀。有沒有辦法只獲取帶有檔案名的向量?
[1] ""
[2] ""
[3] " Directory: C:\\Users\\user\\Documents\\R_Data\\texk"
[4] ""
[5] ""
[6] "Mode LastWriteTime Length Name "
[7] "---- ------------- ------ ---- "
[8] "-a---- 30/11/2021 10:31 386751 auth_cash_flow.html "
[9] "-a---- 30/11/2021 10:31 189370 auth_cash_flow_total.html "
[10] "-a---- 30/11/2021 10:31 552947 auth_symv_gantt.html "
[11] "-a---- 30/11/2021 10:31 93238 auth_tender_schedule.html "
[12] "-a---- 30/11/2021 10:30 683088 dev_constr_pivot.html "
[13] "-a---- 30/11/2021 10:30 70224 form_org_chart.html "
[14] "-a---- 30/11/2021 10:31 199907 form_org_chart2.html "
[15] "-a---- 30/11/2021 10:30 618821 form_workload.html "
[16] "-a---- 30/11/2021 10:30 109127 index.html "
[17] ""
[18] ""
這是我的會話資訊:
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=Greek_Greece.1253 LC_CTYPE=Greek_Greece.1253 LC_MONETARY=Greek_Greece.1253
[4] LC_NUMERIC=C LC_TIME=Greek_Greece.1253
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.5 tools_4.0.5
uj5u.com熱心網友回復:
x <- system(paste('powershell -command ',shQuote('Get-ChildItem -Filter "*.html" | Where-Object { $_.LastWriteTime -ge "11/30/2021 09:10:00" } | Select -exp Name')), intern = TRUE)
uj5u.com熱心網友回復:
或者,這可以使用R的專用函式file.info() 完成,它比GeorgeDontas提供的 powershell 答案快約 6 倍。
基準測驗是在一個包含 1228 個檔案的檔案夾上完成的,其中 553 個匹配"*.txt",然后按日期過濾,給出 5 個檔案:
microbenchmark::microbenchmark(
R = {
files <- file.info(list.files(pattern = "*.txt"))
rownames(files[ files$mtime > as.POSIXct("01/01/2021 09:10:00", format = "%m/%d/%Y %H:%M:%S"), ])
},
PS = {
system(paste('powershell -command ',
shQuote('Get-ChildItem -Filter "*.txt" | Where-Object { $_.LastWriteTime -ge "01/01/2021 09:10:00" } | Select -exp Name')),
intern = TRUE)
})
# Unit: milliseconds
# expr min lq mean median uq max neval
# R 147.7614 161.6208 173.5587 168.8470 181.1235 261.7563 100
# PS 801.9531 959.1979 997.8903 999.4841 1024.4515 1259.1713 100
# ~6x
# 997/168 = 5.934524
注意:我知道 OP 可能有使用powershell方法的正當理由,將此答案作為基準發布。
uj5u.com熱心網友回復:
這個怎么樣?我從您的示例輸出中假設 (a) 檔案名僅出現在 shell 輸出行的末尾,后跟零個或多個空格,以及 (b) 檔案名僅包含.html后綴之前的字母數字字符和下劃線。如果不是這樣,那么第二行和第三行中的正則運算式需要稍微泛化。
## Get shell output as character vector
x <- system(paste('powershell -command', shQuote('Get-ChildItem -Filter "*.html" | Where-Object { $_.LastWriteTime -ge "11/30/2021 09:10:00" } | Select -exp Name')), intern = TRUE)
## Find lines ending in ".html" followed by zero or more spaces
i <- grep("\\.html\\s*$", x)
## Extract file names from those lines
fn <- sub("^.* (\\w*\\.html)\\s*$", "\\1", x[i])
這是對 shell 輸出中的行進行的測驗:
x <- c(
"",
"",
" Directory: C:\\Users\\user\\Documents\\R_Data\\texk",
"",
"",
"Mode LastWriteTime Length Name ",
"---- ------------- ------ ---- ",
"-a---- 30/11/2021 10:31 386751 auth_cash_flow.html ",
"-a---- 30/11/2021 10:31 189370 auth_cash_flow_total.html ",
"-a---- 30/11/2021 10:31 552947 auth_symv_gantt.html ",
"-a---- 30/11/2021 10:31 93238 auth_tender_schedule.html ",
"-a---- 30/11/2021 10:30 683088 dev_constr_pivot.html ",
"-a---- 30/11/2021 10:30 70224 form_org_chart.html ",
"-a---- 30/11/2021 10:31 199907 form_org_chart2.html ",
"-a---- 30/11/2021 10:30 618821 form_workload.html ",
"-a---- 30/11/2021 10:30 109127 index.html ",
"",
""
)
i <- grep("\\.html\\s*$", x)
i
# [1] 8 9 10 11 12 13 14 15 16
fn <- sub("^.* (\\w*\\.html)\\s*$", "\\1", x[i])
fn
# [1] "auth_cash_flow.html" "auth_cash_flow_total.html"
# [3] "auth_symv_gantt.html" "auth_tender_schedule.html"
# [5] "dev_constr_pivot.html" "form_org_chart.html"
# [7] "form_org_chart2.html" "form_workload.html"
# [9] "index.html"
如果您最終確實想要一個資料框:
## Read lines of shell output containing file names into table
tc <- textConnection(x[i])
dd <- read.table(tc)
names(dd) <- c("mode", "date", "time", "length", "name")
close(tc)
## Coerce character date and time to useful date-time class
dd$time <- as.POSIXct(paste(dd$date, dd$time), format = "%d/%m/%Y %H:%M")
dd$date <- NULL
dd
# mode time length name
# 1 -a---- 2021-11-30 10:31:00 386751 auth_cash_flow.html
# 2 -a---- 2021-11-30 10:31:00 189370 auth_cash_flow_total.html
# 3 -a---- 2021-11-30 10:31:00 552947 auth_symv_gantt.html
# 4 -a---- 2021-11-30 10:31:00 93238 auth_tender_schedule.html
# 5 -a---- 2021-11-30 10:30:00 683088 dev_constr_pivot.html
# 6 -a---- 2021-11-30 10:30:00 70224 form_org_chart.html
# 7 -a---- 2021-11-30 10:31:00 199907 form_org_chart2.html
# 8 -a---- 2021-11-30 10:30:00 618821 form_workload.html
# 9 -a---- 2021-11-30 10:30:00 109127 index.html
我的觀點是,這只是為了使用file.info專門為這項任務設計的,需要做很多作業。可能值得做一個基準測驗以確定該file.info方法是否真的像您想象的一樣慢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370383.html
