Powershell,robocopy 数组:无效参数和时间戳保留
Powershell, robocopy array: invalid parameter & timestamp preserving
我有这个 Powershell 代码:
Function CheckFileList()
{
$limit = (Get-Date).AddDays(-270)
$input_path = gci '//network/sourceDir' | sort -property LastWriteTime
$output_file = 'c:\PowershellScripts\prune_results.txt'
#Clear-Content $output_file
$countf = 0
$outputstr = ""
$outputstr = $(Get-Date -format 'F') + " - Folders to be purged:`r`n"
$input_path | Foreach-Object{
if ( (Get-Item $_.FullName) -is [System.IO.DirectoryInfo] ) {
if ( $_.LastWriteTime -le $limit ) {
$source='//network/sourceDir' + $_.Name
$dest="\computer\c$\targetDir" + $_.Name
$what=@("/MOVE")
$options=@("/COPY:DAT /DCOPY:T")
$cmdArgs = @("$source","$dest",$what,$options)
#"robocopy " + $cmdArgs >> $output_file
robocopy @cmdArgs
$outputstr = $outputstr + " (" + $_.LastWriteTime + ") `t" + $_.Name + "`r`n"
$countf++
$outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
$outputstr >> $output_file
Exit
}
}
}
$outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
$outputstr >> $output_file
}
CheckFilelist
这是为了在保留文件夹时间戳的同时移动多个文件夹(以及其中的文件)。
当我 运行 它时,我得到这个错误:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Mon Apr 27 13:20:35 2015
Source - \network\sourceDir\someFolder12345\
Dest - \computer\c$\someFolder12345\
Files :
Options : /COPY:DAT /MOVE /R:1000000 /W:30
------------------------------------------------------------------------------
ERROR : Invalid Parameter #4 : "/COPY:DAT /DCOPY:T"
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \server\share\path).
destination :: Destination Dir (drive:\path or \server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !
我的 what/options 数组有问题吗?这些参数对我来说有效。
[编辑] 我还发现此脚本未保留文件夹时间戳。 someFolder12345
以 "now" 的 date/time 结束在 targetDir 上。文件夹中的文件保留了时间戳,但文件夹没有?
您的字符串 "/COPY:DAT /DCOPY:T"
似乎作为一个参数传递给 robocopy,而不是作为 2 个单独的参数。如果您检查 $options
变量,它在数组中只有一个项目。尝试将该行更改为 $options=@("/COPY:DAT","/DCOPY:T")
,以便单独传入每个参数。
我有这个 Powershell 代码:
Function CheckFileList()
{
$limit = (Get-Date).AddDays(-270)
$input_path = gci '//network/sourceDir' | sort -property LastWriteTime
$output_file = 'c:\PowershellScripts\prune_results.txt'
#Clear-Content $output_file
$countf = 0
$outputstr = ""
$outputstr = $(Get-Date -format 'F') + " - Folders to be purged:`r`n"
$input_path | Foreach-Object{
if ( (Get-Item $_.FullName) -is [System.IO.DirectoryInfo] ) {
if ( $_.LastWriteTime -le $limit ) {
$source='//network/sourceDir' + $_.Name
$dest="\computer\c$\targetDir" + $_.Name
$what=@("/MOVE")
$options=@("/COPY:DAT /DCOPY:T")
$cmdArgs = @("$source","$dest",$what,$options)
#"robocopy " + $cmdArgs >> $output_file
robocopy @cmdArgs
$outputstr = $outputstr + " (" + $_.LastWriteTime + ") `t" + $_.Name + "`r`n"
$countf++
$outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
$outputstr >> $output_file
Exit
}
}
}
$outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
$outputstr >> $output_file
}
CheckFilelist
这是为了在保留文件夹时间戳的同时移动多个文件夹(以及其中的文件)。
当我 运行 它时,我得到这个错误:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Mon Apr 27 13:20:35 2015
Source - \network\sourceDir\someFolder12345\
Dest - \computer\c$\someFolder12345\
Files :
Options : /COPY:DAT /MOVE /R:1000000 /W:30
------------------------------------------------------------------------------
ERROR : Invalid Parameter #4 : "/COPY:DAT /DCOPY:T"
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \server\share\path).
destination :: Destination Dir (drive:\path or \server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !
我的 what/options 数组有问题吗?这些参数对我来说有效。
[编辑] 我还发现此脚本未保留文件夹时间戳。 someFolder12345
以 "now" 的 date/time 结束在 targetDir 上。文件夹中的文件保留了时间戳,但文件夹没有?
您的字符串 "/COPY:DAT /DCOPY:T"
似乎作为一个参数传递给 robocopy,而不是作为 2 个单独的参数。如果您检查 $options
变量,它在数组中只有一个项目。尝试将该行更改为 $options=@("/COPY:DAT","/DCOPY:T")
,以便单独传入每个参数。