2013-03-27 49 views
1

我正在嘗試搜索多個目錄並替換newfile.swf的所有實例。替換/更新文件共享上的特定文件

$Logfile = "C:\FOLDER\filecopy.log" 
$ServerDir = "\\SERVER\TESTING\DIRECTORY" 
$SourceFile = "C:\FOLDER\newfile.swf" 
$OldFiles = Get-ChildItem -path $ServerDir -Recurse -Include newfile.swf | 
      Sort-Object Name | Format-Table Fullname -Auto 

foreach ($O in $OldFiles) { 
    if (Test-Path $O) { 
     Copy-Item $SourceFile $O -force 
    } else { 
     Add-Content $logfile "File Copy Failed" 
     Add-Content $logfile $error[$error.count-1] 
     Add-Content $logfile "===================" 
    } 
} 

錯誤在下面列出。

File Copy Failed System.Management.Automation.ParseException: 
Unexpected token 'C:\FOLDER\newfile.swf' in expression or statement. 
at System.Management.Automation.Parser.PipelineRule() 
at System.Management.Automation.Parser.StatementRule() 
at System.Management.Automation.Parser.StatementListRule(Token start) 
at System.Management.Automation.Parser.ScriptBlockRule(String name, Boolean requireBrace, Boolean isFilter, ParameterDeclarationNode parameterDeclaration, List`1 functionComments, List`1 parameterComments) 
at System.Management.Automation.Parser.ParseScriptBlock(String input, Boolean interactiveInput) 
at System.Management.Automation.AutomationEngine.ParseScriptBlock(String script, Boolean interactiveCommand) 
at System.Management.Automation.ScriptCommandProcessor..ctor(String script, ExecutionContext context, Boolean isFilter, Boolean useLocalScope, Boolean interactiveCommand, CommandOrigin origin) 
at System.Management.Automation.Runspaces.Command.CreateCommandProcessor(ExecutionContext executionContext, CommandFactory commandFactory, Boolean addToHistory) 
at System.Management.Automation.Runspaces.LocalPipeline.CreatePipelineProcessor() 
at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper() 
at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc() 
=================== 

回答

3

Format-Table不會產生文件對象的列表,這是什麼Copy-Item預計作爲其輸入。試試這個:

Get-ChildItem -Path $ServerDir -Recurse -Include newfile.swf | sort Name | % { 
    if (Test-Path -LiteralPath $_.FullName) { 
    Copy-Item $SourceFile $_.FullName -Force 
    } else { 
    Add-Content $logfile "File Copy Failed" 
    Add-Content $logfile $error[$error.count-1] 
    Add-Content $logfile "===================" 
    } 
} 
+0

這很好用!謝謝您的幫助 – user2217597 2013-03-28 14:27:06