2017-01-01 14 views
0

devからルートフォルダにファイルをコピーした後、ファイルの拡張子などを変更するpowershellスクリプトがあります。私はすでに正常にファイルをコピーします。しかし、私は、すべてのサブフォルダに適用される-Recurse(devフォルダを除外したい)を使用するか、コードをeootフォルダにのみ適用することの間にこだわっています。Powershell -Recurseパラメータ。特定のサブディレクトリを除外する方法

CODE:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "$w = $args[0];foreach ($file in Get-ChildItem -Recurse -Path "$w" -Exclude *.bat, *.txt, *.png, *.jpg, *.jpeg, *.gif, *.mp3, *.mp4, dev\\*) {"$w+$file";(Get-Content $w$file).replace('dev/', '') | Set-Content "$w$file";(Get-Content $w$file).replace('.min.js', '.minjs') | Set-Content "$w$file";(Get-Content $w$file).replace('.json', '.xson') | Set-Content "$w$file";(Get-Content $w$file).replace('https://www.google-analytics.com/analytics.js', '/httpswgacanalyticsjs/') | Set-Content "$w$file";(Get-Content $w$file).replace('.js', '.min.js') | Set-Content "$w$file";(Get-Content $w$file).replace('.minjs', '.min.js') | Set-Content "$w$file";(Get-Content $w$file).replace('.xson', '.json') | Set-Content "$w$file";(Get-Content $w$file).replace('/httpswgacanalyticsjs/', 'https://www.google-analytics.com/analytics.js') | Set-Content "$w$file";(Get-Content $w$file).replace('.min.css', '.mincss') | Set-Content "$w$file";(Get-Content $w$file).replace('.css', '.min.css') | Set-Content "$w$file";(Get-Content $w$file).replace('.mincss', '.min.css') | Set-Content "$w$file";};Write-Host ".""" "%path%" 

はINPUT:

FOLDER 
    -> dev 
     ->files 
     ->folders 

OUTPUT:

FOLDER 
    -> dev  //exclude 
     ->files //exclude 
     ->folders //exclude 
    ->files //execute code on this 
     ->folders //execute code on this 

答えて

0

がどのようにでしょう。このような何か(使用-fileオプションのPowerShellのV5)

$w = "c:\temp\"; 
[email protected]("*.bat", "*.txt", "*.png", "*.jpg", "*.jpeg", "*.gif", "*.mp3", "*.mp4") 

$hashreplace = @{} 
$hashreplace.'dev/'= '' 
$hashreplace.'.min.js'= '.minjs' 
$hashreplace.'.json'= '.xson' 
$hashreplace.'https://www.google-analytics.com/analytics.js'= '/httpswgacanalyticsjs/' 
$hashreplace.'.js'= '.min.js' 
$hashreplace.'.minjs'= '.min.js' 
$hashreplace.'.xson'= '.json' 
$hashreplace.'/httpswgacanalyticsjs/'= 'https://www.google-analytics.com/analytics.js' 
$hashreplace.'.min.css'= '.mincss' 
$hashreplace.'.css'= '.min.css' 
$hashreplace.'.mincss'= '.min.css' 

foreach ($file in Get-ChildItem -Recurse -File -Path "$w" -Exclude $exclude | where {$_.fullname -notlike "*\dev\*"}) 
{ 
    $text=(Get-Content $file.FullName) 
    $hashreplace.Keys | %{ $text = $text.Replace($_, $hashreplace[$_])} 
    $text | set-content $file.FullName 
}; 
+0

を試してみてください私はこのPowershellファイルをバッチファイルから起動しますか?彼らは道の中の空間なので、私はそれが動作するように見ることができません。また、どのように引数を入力するのですか? –

+0

powershell -executionPolicy bypass -noexit -file "c:\ temp \ path with space \ test.ps1" "argument" – Esperento57

+0

多くの試行錯誤の後、whereステートメントにはアスタリスクが "* \ dev \ *"なくなっています。助けてくれてありがとう、今は機能している。 –

0

私はので、私はもっぱらしかし、次のことが役立つかもしれない理論オフつもりだこれをテストしていませんあなたがしようとしていることを理解しているからです。それはあなたが除外したいファイルやフォルダを含まないので、名前の変更を行うために、ファイルのリストについては、$ filesToRecurse変数を使用すると

$filesToRecurse = Get-ChildItem -Path {pathToFileToRecurse} -Exclude {pathToFolderToExclude} | where {! $_.PSIsContainer} 

のようなものに追加します。

注:あなたが複数の場所を除外したい場合は、また、配列変数を作成して使用することができ、など

関連する問題