Downloading content in a loop with Powershell
This is some handy powershell that I used when to downloading the images that i wanted to use in the pokeguess.trickey.solutions game.
The images are originally sourced from the public asset.pokemon.com cdn. Rather than hot link assets to thier CDN, I wanted to make a copy of the assets I needed. The CDN has the images keyed on thier Pokeex ID , so i needed a simple script that i could run to incremement the ID in the correct format! make a call to get the file contents and output a local copy.
The script makes use of Do {} until ()
looping to build the file name (and format it correctly)
We use the Invoke-WebRequest
command to download the content, and the -OutFile
switch to get our local copy
$a = 1 #starting point
$baseurl = "https://assets.pokemon.com/assets/cms2/img/pokedex/full/"
$outfolder = "C:\src\public-asset-vault\public-asset-vault\wwwroute\pokeassets\"
do {
#format file name
$filenumber = $a
if($a -lt 10) {$filenumber = "00" + $a}
if($a -lt 100 -and $a -ge 10) {$filenumber = "0" + $a}
$file = "$($filenumber).png"
#download content
Invoke-WebRequest $($baseurl + $file) -OutFile $($outfolder + $file)
$a++
} until ($a -ge 1011) #do until we have got an image for every pokemen
in this case my out folder is actually the simple web assets folder I created in a previous post so now I just need to create a pull request on that repo, hit update and all these images that we have just downloaded are now going to be hosted on our production site e.g.
- Previous: Resurecting pokeguess.trickey.solutions