Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Last active March 27, 2024 14:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidruhmann/4bb3cab53c51022f74c8 to your computer and use it in GitHub Desktop.
Save davidruhmann/4bb3cab53c51022f74c8 to your computer and use it in GitHub Desktop.
[Batch] [PowerShell] Download Gists
@echo off
:Main <Username> [Destination=CD]
call :DownloadGists %1 %2 "%CD%"
exit /b
:: TODO Support duplicate file names.
:: Current pagination limit set to 1000
:DownloadGists <Username> <Destination> ~UI/IO
PowerShell -NoProfile -ExecutionPolicy RemoteSigned -Command "$c = New-Object System.Net.WebClient; $c.Headers.Add('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)'); $c.DownloadString('https://api.github.com/users/%~1/gists?per_page=1000').Split(',') | ForEach { $_.Split('{') | ForEach { $_.Split('}') | ForEach { If ($_.Contains('filename')) { Write-Host ($f = $_.Split([char]0x0022)[3].Replace(([char]0x0022).ToString(), '')) } ElseIf ($_.Contains('raw_url')) { $c.DownloadFile($_.Split([char]0x0022)[3].Replace(([char]0x0022).ToString(), ''), '%~2\' + $f) } } } };"
exit /b
@davidruhmann
Copy link
Author

The destination directory must exist else the script will throw an exception. If no destination directory is specified, the current working directory is used. The current limit is hard coded to 1000. Update the limit if needed. If there are any duplicate file names, the older file will overwrite the newer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment