Skip to content

Instantly share code, notes, and snippets.

@t2psyto
Last active January 22, 2025 09:00
Show Gist options
  • Save t2psyto/316b367fc97dd66c63bd72d169c50d77 to your computer and use it in GitHub Desktop.
Save t2psyto/316b367fc97dd66c63bd72d169c50d77 to your computer and use it in GitHub Desktop.
更新されていたらダウンロードする & レスポンスコードを環境変数 http_code に格納する
rem 更新されていたらダウンロードする & レスポンスコードを環境変数 http_code に格納する
rem curl.exe -z と同様の処理を powershell で実装
rem ※ Windows 10 LTSB Version 1607 には curl.exe が標準コマンドに含まれていない
rem ※ powershell v5.1 の Invoke-webrequest は 200 以外の時に例外を吐く
set fetch_remote=https://example.org/sample.txt
set fetch_local=d:\sample.txt
set http_code=
if exist "c:\windows\system32\curl.exe" (
for /f "delims=" %%i in ('curl.exe -k -s -z %fetch_local% -R -o %fetch_local% -w "%%{http_code}" %fetch_remote%') do set http_code=%%i
) else (
for /f "delims=" %%i in ('powershell.exe -NoProfile -Command "$url = $env:fetch_remote; $outfile = $env:fetch_local; $headers = @{}; if (Test-Path $outfile) { $lastModified = (Get-Item $outfile).LastWriteTimeUtc.ToString('R'); $headers = @{ 'If-Modified-Since' = $lastModified }}; $ProgressPreference = 'SilentlyContinue'; try { $response = wget -Uri $url -Headers $headers -OutFile $outfile -UseBasicParsing; $response_StatusCode = $response.StatusCode; if ($response.StatusCode -eq 200) {} catch {$response_StatusCode = $_.Exception.Response.StatusCode.Value__; }; $ProgressPreference = 'Continue'; echo $response_StatusCode;"') do set http_code=%%i
)
echo %http_code%
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment