r system doesn't work when trying 7zip

2019-08-10 19:38发布

问题:

Hi I'm trying to use R to control unzipping a file.

I've added 7z into PATH, and did

7z e hat.2015-09-26T01-10-02.gz

and it worked.

Now in R, i'm in the same directory, and i tried

> command1 = paste0('7z e ', drop.file)
> command1
[1] "7z e hat.2015-09-26T01-10-02.gz"
> system(command1, intern=T)
Error in system(command1, intern = T) : '7z' not found
> system2(command1)
Warning message:
running command '"7z e hat.2015-09-26T01-10-02.gz"' had status 127 

> shell(command1)
'7z' is not recognized as an internal or external command,
operable program or batch file.
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c 7z e hat.2015-09-26T01-10-02.gz' had status 1 
2: In shell(command1) :
  '7z e hat.2015-09-26T01-10-02.gz' execution failed with error code 1

回答1:

It's little bit raw, but try this out (:

Only Windows. It's uses CMD

ZiparEm7zip = function(sQualPasta)
{
  sWDTava = getwd()
  setwd(dirname(sQualPasta))
  sQuem = gsub("[/]", "\\\\", sQualPasta)

  dirname(sQuem)
  NomeArquivo = paste0("eufaco7zip",".bat")
  sNomePasta7zip = basename(sQuem)
  sArquivoSaida = basename(sQuem)
  sQualPasta7zip = sQuem

  if(dir.exists("C:/Program Files/7-Zip/"))
  {
    sTexto = "set PATH=%PATH%;C:\\Program Files\\7-Zip\\"
  }else if(dir.exists("C:/Program Files (x86)/7-Zip/"))
  {
    sTexto = "set PATH=%PATH%;C:\\Program Files (x86)\\7-Zip\\"
  }else
  {
    stop("Error, o 7zip not installed?")
  }


  sTexto2 = paste0("\npushd ",sQualPasta7zip)
  sTexto3 = paste0("\n7z a -r ../",sArquivoSaida," *")
  sTexto4 = '\n( del /q /f "%~f0" >nul 2>&1 & exit /b 0  )'

  cat(sTexto,file=NomeArquivo,append = TRUE)
  cat(sTexto2,file=NomeArquivo,append = TRUE)
  cat(sTexto3,file=NomeArquivo,append = TRUE)
  cat(sTexto4,file=NomeArquivo,append = TRUE)

  shell.exec(NomeArquivo)

  while(file.exists(NomeArquivo))
  {
    Sys.sleep(10)
  }      

  setwd(sWDTava)
  return(TRUE)
}

Try like:

ZiparEm7zip("F:/MYFOLDER")


标签: r shell system