Skip to content

Instantly share code, notes, and snippets.

@saurabhwahile
Created June 16, 2014 19:08
Show Gist options
  • Save saurabhwahile/50f1091fb29c2bb327b7 to your computer and use it in GitHub Desktop.
Save saurabhwahile/50f1091fb29c2bb327b7 to your computer and use it in GitHub Desktop.
Creating RAR Archives From Windows Powershell
#Set-ExecutionPolicy Unrestricted
$fromFolderPath = $args[0][0]
$toFolderPath = $args[0][1]
$toFilename = ""
if ($fromFolderPath.Split("\")[-1] -eq "")
{
$toFilename = $fromFolderPath.Split("\")[-2]
$fromFolderPath = $fromFolderPath.SubString(0, ($fromFolderPath.length-1))
}
else
{
$toFilename = $fromFolderPath.Split("\")[-1]
}
$toFilePath = $($toFolderPath+"\"+$toFilename+".rar")
#Keep backup if archiving fails
try
{
move $toFilePath $($toFilePath+".old" )
}
catch
{
echo "No Backup Found"
}
#Generate RAR archive
$argList = @("a", ('"'+$toFilePath+'"'), ('"'+$fromFolderPath+'"'))
Start-Process -FilePath "C:\Program Files\Winrar\winrar.exe" -ArgumentList $argList -NoNewWindow -Wait
#Remove Backup
$toFilePathOld = ($toFilePath)+".old"
del $toFilePathOld
$fromFolder = $currentPath.Substring(0, ($currentPath.length-1))
$toFolder = ($env:USERPROFILE+"\Desktop\Backup")
$argList = @($PWD.Path, $toFolder)
& ($env:PS_SCRIPTS+"\backup-winrar.ps1") $argList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment