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:
folder
andinput
look the same in this result, as in the intended string, with 2\\
as separators. However it gave anError: '\U' used without hex digits in character string starting ""'C:\U"
- All the
\'
turned into just'
. If I change them to\\'
, they will return as\\'
in the returned string, not the expected\'
- There are all these
\n
now added - 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