Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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