从 R : Unexpected token in expression or statement 运行 powershell 命令

标签 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] ""

这是我的 session 信息:

> 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 

最佳答案

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)

关于从 R : Unexpected token in expression or statement 运行 powershell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70165832/

相关文章:

r - 为 10 支球队和 6 轮创建循环赛时间表

python - 为虚拟变量创建模型

r - 如何在 R 中使用 ggplot2 在投影 map 上绘制插值数据

java - 如何在 PowerShell 中获取 Java 版本

powershell - 相当于 PowerShell 中的 (dir/b > files.txt)

r - 在 Leaflet 中使用带有 SpatialPolygons 的 markerClusterOptions

html - 如何在 Powershell Core 中解析 html

powershell - 如何在Powershell中使用字符串变量?

azure - 无法使用 mcr.microsoft.com/windows/servercore :ltsc when building from azure cloudshell (powershell)

r - 合并多个 hclust 对象(或树状图)