0

I have this Powershell script that I need to run in R. So, in Rstudio's Terminal I paste this escaped Powershell script:

system('powershell -command "
$folder = \'C:\\Users\\User\\Documents\\Folder Name\\\'
$userAgent = \'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\'
$web = New-Object System.Net.WebClient
$web.Headers.Add(\'user-agent\', $userAgent)
$input = \'C:\\Users\\Administrator\\User\\Another Folder Name\\data.csv\'
$delimit = \' \'

Get-Content $input |
    Foreach-Object { 
        $line = $_.split($delimit);
        $destination_file = ([io.path]::getfilename($line[1]))
        \'[\' + $destination_file  + \']\' + \'Downloading \' + $line[0]
        try {
            $target = join-path $folder $destination_file
            $web.DownloadFile($line[0], $target)
        } catch {
            $_.Exception.Message + \': \' + $destination_file
            add-content (join-path $folder (\'downloader\' + \'.log\')) (\'[\' + $destination_file + \'] \' + $_.Exception.Message)
            add-content (join-path $folder (\'downloader\' + \'.retry\')) ($line -join \' \')
        }
    }
"')

And it all works as expected.

Now, I am trying to define the $folder and $Input from the Powershell script in R, like so:

Rpath <- "C:/Users/User/Documents/Folder Name/"
var <- system('powershell -command "
$folder = \'Rpath\'
$userAgent = \'Mozilla/5.0 ...

I need help figuring out how to pass this Rpath from R into the escaped Powershell script.

Any help would be greatly appreciated

Thanks @I_O for your help I followed your advise and entered in Rstudio:

folder = "'C:\Users\User\Documents\Folder Name\'"
input = "'C:\Users\Administrator\User\Another Folder Name\data.csv'"
text = "
$folder = %s
$userAgent = \'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\'
$web = New-Object System.Net.WebClient
$web.Headers.Add(\'user-agent\', $userAgent)
$input = %s
$delimit = \' \'

Get-Content $input |
    Foreach-Object { 
        $line = $_.split($delimit);
        $destination_file = ([io.path]::getfilename($line[1]))
        \'[\' + $destination_file  + \']\' + \'Downloading \' + $line[0]
        try {
            $target = join-path $folder $destination_file
            $web.DownloadFile($line[0], $target)
        } catch {
            $_.Exception.Message + \': \' + $destination_file
            add-content (join-path $folder (\'downloader\' + \'.log\')) (\'[\' + $destination_file + \'] \' + $_.Exception.Message)
            add-content (join-path $folder (\'downloader\' + \'.retry\')) ($line -join \' \')
        }
    }
"
sprintf(text, folder, input)

which returned:

[1] "\n$folder = 'C:\\Users\\Administrator\\Documents\\Trading\\ETF Arbitrage\\Market Data\\Borrow Fees\\ChartExchange\\PowerShell\\'\n$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1'\n$web = New-Object System.Net.WebClient\n$web.Headers.Add('user-agent', $userAgent)\n$input = 'C:\\Users\\Administrator\\Documents\\Trading\\ETF Arbitrage\\Market Data\\Borrow Fees\\ChartExchange\\data.csv'\n$delimit = ' '\n\nGet-Content $input |\n    Foreach-Object { \n        $line = $_.split($delimit);\n        $destination_file = ([io.path]::getfilename($line[1]))\n        '[' + $destination_file  + ']' + 'Downloading ' + $line[0]\n        try {\n            $target = join-path $folder $destination_file\n            $web.DownloadFile($line[0], $target)\n        } catch {\n            $_.Exception.Message + ': ' + $destination_file\n            add-content (join-path $folder ('downloader' + '.log')) ('[' + $destination_file + '] ' + $_.Exception.Message)\n            add-content (join-path $folder ('downloader' + '.retry')) ($line -join ' ')\n        }\n    }\n"

Sorry I am new at this. I have several follow up questions:

  1. folder and input look the same in this result, as in the intended string, with 2 \\ as separators. However it gave an Error: '\U' used without hex digits in character string starting ""'C:\U"
  2. All the \' turned into just '. If I change them to \\', they will return as \\' in the returned string, not the expected \'
  3. There are all these \n now added
  4. Finally, I tried system('powershell -command sprintf(text, folder, input)') to execute the Powershell script in R, but did not work.

About the first 3 questions, I don't know which ones do matter and which ones don't, if any. The 4th, not sure if that is the way.

Any further help would be most appreciated

fionpo
  • 141
  • 1
  • 10
  • Does this answer your question? [Is there a string formatting operator in R similar to Python's %?](https://stackoverflow.com/questions/46085274/is-there-a-string-formatting-operator-in-r-similar-to-pythons) – JosefZ May 08 '23 at 20:43

1 Answers1

0

string substitution with sprintf might be handy:

folder = "\\path"
file = "\\door"
text = "the long and winding %s that leads to your %s"
sprintf(text, folder, file)
## [1] "the long and winding \\path that leads to your \\door"

Note that the s in %s is not a placeholder name but indicates that a string will be supplied. Accordingly, the arguments to sprintf must be arranged in the order of their intended appearance.

I_O
  • 4,983
  • 2
  • 2
  • 15