@(echo '> NULecho off)setlocal enableextensionsset "THIS_PATH=%~f0"set "PARAM_1=%~1"PowerShell.exe -Command "iex -Command ((gc "%THIS_PATH:`=``%") -join "`n")"exit /b %errorlevel%-- この1つ上の行までバッチファイル') | sv -Name TempVar# ここからPowerShellスクリプトecho "This batch file is executed as PowerShell script (file = $env:THIS_PATH, param = $env:PARAM_1)"#日付取得関数(
http://api.fixer.io/向けURL)function dateurl($dayDiff){ $pre ='
http://api.fixer.io/'; $suf ='?base=USD';# $time = Get-Date -Format 'yyyy-MM-dd'; $date = [DateTime]::Today.AddDays($dayDiff).ToString('yyyy-MM-dd'); $url = [String]::Join('',@($pre,$date,$suf)); return $url;}#保存パス生成function datepath($dayDiff){ $pre =''; $suf ='_usd.json'; $date = [DateTime]::Today.AddDays($dayDiff).ToString('yyyy-MM-dd'); $path = [String]::Join('',@($pre,$date,$suf)); return $path;}#過去1ヶ月分回すfor ($i=0; $i -lt 31; $i++){ $inv = $i*(-1); $path = datepath($inv); echo $path; $array = Get-Content $path -Encoding UTF8;$json=@"{'items': [$array ]}"@echo $json;Add-Type -AssemblyName System.Web.Extensions$serializer=new-object System.Web.Script.Serialization.JavaScriptSerializer$obj=$serializer.DeserializeObject($json) #$obj["items"][0]["date"] #「メモリ8GB」と表示される#$obj.items[0].rates # 上と同じ#$obj["items"]|%{$_["rates"]} # 名前が列挙される$json2=@"{'items': [$obj.items[0].rates ]}"@;#echo $json2;Add-Type -AssemblyName System.Web.Extensions$serializer2=new-object System.Web.Script.Serialization.JavaScriptSerializer$obj=$serializer2.DeserializeObject($json2)$obj.items[0].rates.GetEnumerator() | Select @{N="Country"; E={$_.Key}}, @{N=; E={$_.Value}} | Export-Csv .currencytest.csv -Encoding Default -NoTypeInformation}Read-Host "続けるにはENTERキーを押して下さい"
#echo $url;#$d=new-object System.Net.WebClient;$d.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;$d.DownloadFile($url,$save);#ワンライナーバッチ版(参考)#@powershell -NoProfile -ExecutionPolicy Bypass -Command "function time(){$time = Get-Date -Format 'yyyy-MM-dd';return $time};$date=time(0);$urlarray=@('
http://api.fixer.io/',$date,'?base=USD');$url=[String]::Join('',$urlarray);$savearray=@($date,'_usd.json');$save=[String]::Join('',$savearray);echo $url;$d=new-object System.Net.WebClient;$d.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;$d.DownloadFile($url,$save)"