2015-09-20 40 views
-1

我想壓縮一個飛蛾的文件夾到一個文件。例如201501適用於所有jan文件夾,我需要壓縮到一個名爲somename-2015-jan.zip的文件。所有其他的邏輯工作只zipplig部分顯示上述錯誤。我有PowerShell v3和,網絡4.54,當我可以調用System.IO.Compression.ZipFile] :: CreateFromDirectory來壓縮文件時出錯。帶有「4」參數的「CreateFromDirectory」:「路徑不是合法形式。」

    $year = 2015 
       $base = "D:\Archive\" 


       $folder = Get-Content "D:\Archive\MasterFolder.txt" 
          #write-host $folder.count total lines read from file 
          foreach ($Fname in $folder) 
          { 

        $yearmonth = Get-Content "D:\Archive\duration.txt" 
             write-host $yearmonth.count total lines read from file 
             foreach ($date in $yearmonth) 
        { 


      $source = "$base$Fname\$date*" 


      #[string]$text = "$date"  

      $string = $date 
      #201501 
      $mmd = $string.Substring(4,2) 
       Write-host "fffcmd $mmd" 


      if ($mmd -gt 12) { 
       "$mmd is bigger than 12 Which is not valid,Please Correct The file name durations.txt and run the command again" 
       break 
         } 

Else { 

write-host " This is Source Folder Name !!!!!!!!!!!!!!!!!!!!$source" 
switch($mmd) 
    { 
    "01" {$mm = "jan" 
     write-host "New mm is $mm" 
} 

    "02" {$mm = "Feb" 
     write-host "New mm is $mm" 
} 

    "03" {$mm = "Mar" 
     write-host "New mm is $mm" 
} 
    "04" {$mm = "Apr" 
     write-host "New mm is $mm" 
} 
    "05" {$mm = "May" 
     write-host "New mm is $mm" 
} 
    "06" {$mm = "Jun" 
     write-host "New mm is $mm" 
} 
    "07" {$mm = "Jul" 
     write-host "New mm is $mm" 
} 
    "08" {$mm = "Aug" 
     write-host "New mm is $mm" 
} 
    "09" {$mm = "Sep" 
     write-host "New mm is $mm" 
} 
    "10" {$mm = "Oct" 
     write-host "New mm is $mm" 
} 
    "11" {$mm = "Now" 
     write-host "New mm is $mm" 
} 
    "12" {$mm = "Dec" 
     write-host "New mm is $mm" 
} 
    default 
    { 
     Write-host "The Month Duration $mmd is not valid";continue 
    } 
} 
      $destination = "$base$Fname\$Fname-$year-$mm.zip" 
      write-host " Destination $destination" 
      Add-Type -assembly "system.io.compression.filesystem" 


function ZipFiles($zipfilename, $sourcedir) 
{ 
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") 
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal 
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, 
    $zipfilename, $compressionLevel, $false) 
} 
ZipFiles "$destination", "$source" 

        } 

      start-sleep -s 1 
          } 

} 

看起來像提供參數的方式不正確或放置在源中的通配符不正確。

回答

0

CreateFromDirectory不支持通配符。改爲使用Get-ChildItem

$source = "$base$Fname\$date*" 

匹配的文件傳遞給foreach循環後:

$files = (Get-ChildItem "$base$Fname\$date*") 

foreach ($file in $files) { 
    # ... 
    # Continue with the rest of your code, starting at the line "$string = $date" 
    # ... 
    ZipFiles $destination, $file.FullName 
} 

其他一些建議:

  1. 移動ZipFiles函數定義foreach loops-外它不需要重新定義循環運行。
  2. foreach之外移動加載/導入System.IO.Compression.FileSystem也是如此。
  3. 刪除整個switch聲明以及與此替換:$mm = [CultureInfo]::CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName($mmd)
相關問題