Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Last active May 22, 2021 09:06
Show Gist options
  • Save DonaldKellett/d33171c969f801fa33d7a8ae16343dc4 to your computer and use it in GitHub Desktop.
Save DonaldKellett/d33171c969f801fa33d7a8ae16343dc4 to your computer and use it in GitHub Desktop.
Simple Windows PowerShell script I made for processing and zipping submission material for COMP 4521 at HKUST
Param ($PrefixFile, $ProjectFolder)
# Credits: https://devblogs.microsoft.com/powershell-community/borrowing-a-built-in-powershell-command-to-create-a-temporary-folder/
Function New-TemporaryFolder {
# Make a new folder based upon a TempFileName
$T="$($Env:temp)\tmp$([convert]::tostring((get-random 65535),16).padleft(4,'0')).tmp"
return (New-Item -ItemType Directory -Path $T)
}
if (-not (Test-Path $PrefixFile -PathType Leaf)) {
Write-Error "-PrefixFile parameter must be a file!"
exit 1
} elseif (-not (Test-Path $ProjectFolder -PathType Container)) {
Write-Error "-ProjectFolder parameter must be a folder!"
exit 1
} else {
$PrefixFile = Get-Item $PrefixFile
$ProjectFolder = Get-Item $ProjectFolder
$tmpdir = New-TemporaryFolder
Copy-Item "$($ProjectFolder.FullName)\*" -Destination $tmpdir.FullName -Recurse
Get-ChildItem $tmpdir.FullName -File -Recurse -Include "*.js" | ForEach-Object {
$tmpfile = New-TemporaryFile
Get-Content @($PrefixFile.FullName, $PSItem.FullName) | Out-File $tmpfile.FullName
Move-Item $tmpfile.FullName -Destination $PSItem.FullName -Force
}
Compress-Archive "$($tmpdir.FullName)\*" -Destination "$($ProjectFolder).zip"
Remove-Item $tmpdir.FullName -Recurse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment