Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iqbmo04/260e4597841048a28e7ad337aa3b0888 to your computer and use it in GitHub Desktop.
Save iqbmo04/260e4597841048a28e7ad337aa3b0888 to your computer and use it in GitHub Desktop.
$TotalNumberVersionsToKeep=100
$BackupFolder = "\\osgdesign\Studio\Windows\Users\v-senefe\Backup" # make sure there is no ending \
$ProjectFolder = "C:\DesignDepot.Git" # make sure there is no ending \
$ProjectFolderName = [System.IO.Path]::GetFileName($ProjectFolder)
Push-Location -Path $ProjectFolder
$changes = CMD.EXE /C git ls-files --modified --others --exclude-standard | Out-String
if ($changes.Length -eq 0){
Exit
}
Pop-Location
Push-Location -Path $BackupFolder
$dirs = Get-ChildItem | Where-Object {$_.Name -match '^.*\.[0-9]{4}$'} | Sort-Object -Property Name -Descending
foreach($d in $dirs){
$ext = [int32]::Parse([System.IO.Path]::GetExtension($d.Name).Substring(1))
# Write-Host $ext
if ($ext -ge $TotalNumberVersionsToKeep){
Remove-Item -Recurse -Force $d
}
$newName = $d.Name.Substring(0, $d.Name.Length-4) + "{0:D4}" -f ($ext+1)
Rename-Item $d -NewName $newName
}
foreach($file in ($changes -split '[\r\n]') |? {$_} | ForEach {$($_ -replace "/", "\")}){
$from = $ProjectFolder + "\" + $file
$to = $BackupFolder + "\$ProjectFolderName.0000\" + $file
$toDir = [System.IO.Path]::GetDirectoryName($to)
# Write-Host $from, $to, $toDir
if (!(Test-Path -path $toDir)) { New-Item $toDir -Type Directory }
Copy-Item $from -Destination $to
}
Pop-Location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment