2017-04-21 45 views
0

ディレクトリ内のフォルダの一覧を取得して最新の.bakファイルを圧縮するpowershellスクリプト別のディレクトリにコピーします。Powershell - ディレクトリ内のすべてのフォルダを一覧表示し、各フォルダ内の最新の.bakファイルを取り出し、圧縮してディレクトリにコピーします。

.bakファイルを検索したくない2つのフォルダがあります。これらのフォルダはどのように除外しますか?私は複数の方法を試しています - 除外ステートメントと私は運がなかった。私は無視したい

フォルダは、「新しいフォルダ」と「新folder1の」

$source = "C:\DigiHDBlah" 
$filetype = "bak" 

$list=Get-ChildItem -Path $source -ErrorAction SilentlyContinue 
foreach ($element in $list) { 
$fn = Get-ChildItem "$source\$element\*" -Include "*.$filetype" | sort LastWriteTime | select -last 1 
$bn=(Get-Item $fn).Basename 
$CompressedFile=$bn + ".zip" 
$fn| Compress-Archive -DestinationPath "$source\$element\$bn.zip" 
Copy-Item -path "$source\$element\$CompressedFile" -Destination "C:\DigiHDBlah2" 
} 

はありがとうございます!

答えて

1

あなたが見つけたファイルのDirectoryプロパティと-NotLike演算子を使用して、不要なフォルダと簡単にマッチさせます。あなただけのあなただけの最後のフォルダを取得するにはdirectoryNameでプロパティに少し文字列操作を行うことができ除外するフォルダの一覧を提供したい場合は、

$Dest = "C:\DigiHDBlah2" 
$files = Get-ChildItem "$source\*\*.$filetype" | Where{$_.Directory -NotLike '*\New Folder' -and $_.Directory -NotLike '*\New Folder1'} | Sort LastWriteTime | Group Directory | ForEach{$_.Group[0]} 
ForEach($file in $Files){ 
    $CompressedFilePath = $File.FullName + ".zip" 
    $file | Compress-Archive -DestinationPath $CompressedFilePath 
    Copy-Item $CompressedFilePath -Dest $Dest 
} 

または:私はまた、ワイルドカードを使用して検索を単純化します次のような除外リストに含まれているかどうかを確認してください:

$Excludes = @('New Folder','New Folder1') 
$Dest = "C:\DigiHDBlah2" 
$files = Get-ChildItem "$source\*\*.$filetype" | Where{$_.DirectoryName.Split('\')[-1] -NotIn $Excludes} | Sort LastWriteTime | Group Directory | ForEach{$_.Group[0]} 
ForEach($file in $Files){ 
    $CompressedFilePath = $File.FullName + ".zip" 
    $file | Compress-Archive -DestinationPath $CompressedFilePath 
    Copy-Item $CompressedFilePath -Dest $Dest 
} 
関連する問題