使用powershell将文件夹添加到winrar

时间:2016-11-14 13:14:28

标签: powershell powershell-v2.0

我使用下面的脚本将一些文件夹从源复制到目标。

https://techblog.dorogin.com/powershell-how-to-recursively-copy-a-folder-structure-excluding-some-child-folders-and-files-a1de7e70f1b#.uw7nnhmd2

$from = '\\DB01\Test'
$to = New-Item -Path '\\DB01\SQLBackups\DBTest\' -ItemType Directory -Name ("Backup_$(Get-Date -f yyyy_MM_dd)")

$exclude = @("*.log", "*.csv")
$excludeMatch = @("logs")

Get-ChildItem -Path $from -Recurse -Exclude $exclude | 
          where { $excludeMatch -eq $null -or $_.FullName.Replace($from, "") -notmatch $excludeMatch } |
          Copy-Item -Destination {
            if ($_.PSIsContainer) {
              Join-Path $to $_.Parent.FullName.Substring($from.length)
            } else {
              Join-Path $to $_.FullName.Substring($from.length)
            }
           } -Force -Exclude $exclude  

现在,我想将$ to variable中创建的文件夹添加到同一位置的zip文件中,其中Backup _ $(Get-Date -f yyyy_MM_dd).zip作为文件名。如何使用PowerShell 2.0实现这一目标?

我在下面试过,但它没有用。

"%ProgamFiles%\WinRAR\Rar.exe" a -ep1 -r "Backup_$(Get-Date -f yyyy_MM_dd)" "$to" 

2 个答案:

答案 0 :(得分:0)

您需要指定正在创建的存档的完整路径,否则它将在运行powershell的路径中创建(可能 C:\ Users \ USERNAME )。

如果您从$to变量中拆分目标,则可以在以后的归档命令中使用该变量:

$from = '\\DB01\Test'
$backup_location = '\\DB01\SQLBackups\DBTest'
$backup_folder = "Backup_$(Get-Date -f yyyy_MM_dd)"
$to = New-Item "$backup_location\$backup_folder" -ItemType Directory

$exclude = @("*.log", "*.csv")
$excludeMatch = @("logs")

Get-ChildItem -Path $from -Recurse -Exclude $exclude | 
          where { $excludeMatch -eq $null -or $_.FullName.Replace($from, "") -notmatch $excludeMatch } |
          Copy-Item -Destination {
            if ($_.PSIsContainer) {
              Join-Path $to $_.Parent.FullName.Substring($from.length)
            } else {
              Join-Path $to $_.FullName.Substring($from.length)
            }
           } -Force -Exclude $exclude 


Start-Process -FilePath "$($env:ProgramFiles)\winrar\rar.exe" -ArgumentList "a -ep1 -r `"$backup_location\$backup_folder.rar`" `"$to`""

答案 1 :(得分:0)

尝试这样的东西(不要忘记点)

. "C:\Program Files (x86)\WinRAR\winrar.exe" a -ep "C:\temp\currentrar.rar" "C:\temp\filetoadd.txt"