You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R调用PowerShell命令报意外标记错误,如何仅获取返回的文件名向量

问题描述

我尝试运行如下可在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 

请问是否有办法仅获取包含文件名的向量?


解决方案

两种方案都可以拿到纯文件名向量:

  1. 直接修改PowerShell命令,只输出文件名
    在原PowerShell命令末尾追加| Select-Object -ExpandProperty Name,让PowerShell直接返回纯文件名,不需要后续额外处理文本:
# intern=TRUE表示将命令输出作为字符向量返回给R变量
res <- system(paste('powershell -command ', shQuote('Get-ChildItem -Filter "*.html" | Where-Object { $_.LastWriteTime -ge "11/30/2021 09:10:00" } | Select-Object -ExpandProperty Name')), intern = TRUE)
# 过滤掉空行
file_vec <- res[res != ""]
  1. 对现有返回结果做文本提取
    如果不想调整PowerShell命令,也可以直接对已得到的字符向量做筛选提取:
# 定位到所有以-a----开头的有效文件行
valid_rows <- grep("^-a----", res)
# 按空白字符拆分每行,取最后一段就是文件名
file_vec <- sapply(strsplit(res[valid_rows], "\\s+"), function(x) tail(x, 1))

内容的提问来源于stack exchange,提问作者Brani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 00:24:03