R - 使用系统命令对文件进行参数化解压缩

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

使用以下方法对R中解压缩大文件的速度很慢感到失望:

unzip("C:/My File.zip", exdir="C:/")

所以我写了一个系统命令:

system('powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"')

这个命令工作得很漂亮,但现在我想参数化输入文件,我在构造命令时遇到了反斜杠转义字符的问题:

in_file <- "C:/My File.zip"
command <- paste0("\'powershell -command \"Expand-Archive -Path \"", in_file, "\" -DestinationPath \"C:/\" -Force\"'")

虽然cat(command)看起来像我想要的字符串:

cat(command)
> 'powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"'

当我运行system(command)时,我收到一个错误:

Warning message:
running command ''powershell -command "Expand-Archive -Path "C:/My File.zip" -DestinationPath "C:/" -Force"'' had status 127 

我把它放到命令实际上没有像我想要的那样运行,因为转义字符仍然存在:

print(command)
> "'powershell -command \"Expand-Archive -Path \"C:/My File.zip\" -DestinationPath \"C:/\" -Force\"'"

我做了各种尝试去除反斜杠,例如gsub("\","",str, fixed=TRUE)但我无法管理它。

如何让我的解压缩命令工作?

r system backslash
1个回答
0
投票

mt1022让我走上正轨。为完成这篇文章,答案是使用:

paste0('powershell -command "Expand-Archive -Path """', in_file, '""" -DestinationPath """', out_file, '""" -Force"')
© www.soinside.com 2019 - 2024. All rights reserved.