1

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
YJZ
  • 3,934
  • 11
  • 43
  • 67
  • 1
    Did you restart R after updating the PATH? – wush978 Oct 14 '15 at 03:19
  • Hi thanks @wush978 i did `echo %PATH%` and saw the PATH was there, and it worked in command line. Oh would it be possible that R hasn't get the updated PATH? – YJZ Oct 14 '15 at 03:25
  • 1
    You can check the PATH inside R via `Sys.getenv("PATH")`. If the PATH is the old one, restarting R should solve your issue. The reason is that modification of the PATH will not take effect in the existed processes. You need to restart those processes to get the new PATH. – wush978 Oct 14 '15 at 03:38
  • Ahh i see. Thanks a lot I'll try this @wush978 – YJZ Oct 14 '15 at 04:14
  • hi @wush978 yes it worked. Thanks a lot! – YJZ Oct 14 '15 at 12:21

1 Answers1

0

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")