2014-07-21 115 views
0

嗨不能弄清楚什麼錯了...我需要這個腳本來創建一個robocopy日誌文件夾,然後創建robocopy txt文件。工作正常,但它不會創建\ log文件夾...請幫助嗎?powershell文件夾創建和創建robocopy文件

$sourceFolder = "C:\x\y\z" 
$destFolder = "C:\x\y\zz" 
{ 
{ 
New-Item -ItemType Directory -Force -Path "$DestFolder\Log" 
} 
foreach ($folder in (Get-ChildItem -Directory $sourceFolder)) { 
    "robocopy `"$($sourceFolder)\$($folder)\zArchive\Data files to Nov 13`" `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`"" | Out-File zArchiveMove.txt -Append 
    "robocopy `"$($sourceFolder)\$($folder)\Data files to Nov 13`"  `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`""  | Out-File zArchiveMove2.txt -Append 
} 
} 

回答

0

它爲什麼用大括號寫出它的方式?我不能說我對Powershell有很多瞭解,但是隻是在測試窗口中重複您的代碼就會讓我聯想到它。所以如果我寫:

{{new-item -itemtype Directory -Force -Path "$DestFolder\Log"}} 

它會回聲。由於意志:

{新項目-itemtype目錄-Force -Path 「$ DestFolder \登錄」}

如果我溝大括號,它的工作原理。 所以我會建議你擺脫大括號,除非他們需要的東西。如果它們在與條件語句相關的代碼塊內,請在此複製該代碼塊。

所以:

$sourceFolder = "C:\x\y\z" 
$destFolder = "C:\x\y\zz" 


New-Item -ItemType Directory -Force -Path "$DestFolder\Log" 

foreach ($folder in (Get-ChildItem -Directory $sourceFolder)) { 
"robocopy `"$($sourceFolder)\$($folder)\zArchive\Data files to Nov 13`" `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`"" | Out-File zArchiveMove.txt -Append 
"robocopy `"$($sourceFolder)\$($folder)\Data files to Nov 13`"  `"$($destFolder)\$($folder)\folder`" /E /MOVE /DCOPY:T /log+:`"$DestFolder\Log\log.olog`""  | Out-File zArchiveMove2.txt -Append 
} 

應該工作。請測試並回報。也請考慮縮進你的初始代碼。我知道Powershell用戶不會經常這樣做,但是當您嵌套代碼塊時,它更容易閱讀。

相關問題