我读了https://stackoverflow.com/questions/ask/advice? 如果文件存在,Powershell 移动项目重命名 http://social.technet.microsoft.com/Forums/scriptcenter/en-US/c347ce7a-3e23-4476-90c2-37b79785ac2f/moving-files-based-on-modified-date Powershell :递归移动文件 http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/22/hey-scripting-guy-september-22-2009.aspx和大约两打文章。
我很抱歉,虽然我喜欢学习它以及它提供的可能性,但我对脚本知之甚少。除了使用 Windows CMD 的批处理文件之外,我以前的职位不需要我编写脚本。我最近接受了一个网络管理员职位,我的主管要求我编写一个脚本,该脚本将根据几个标准移动文件。我正在使用 Powershell 来完成此任务。
- 超过 1 天的文件从 $path 移动到 $destination
- 排除适用于 callpilot.dev 必须保持不变
- 必须移动位于源文件夹中的文件以及位于所有子文件夹中的文件
- 源文件夹结构必须保持不变
- 目标顶级文件夹必须包含实际日期减去 1 天
- 所有子文件夹结构必须使用与源结构相同的名称创建
- 应删除剩余的源文件(callpilot.dev 除外)
这是我的测试文件夹结构:
c:\temp\others\callpilot
c:\temp\others\ftps
c:\temp\others\mysql
c:\temp\others\rss-enterprise
c:\temp\others\rss-sql2k8
c:\temp\others\tree.txt
c:\temp\others\callpilot\example2.gz
c:\temp\others\callpilot\backup.gz
c:\temp\others\callpilot\barnak.bak
c:\temp\others\callpilot\callpilot.dev
c:\temp\others\callpilot\IPESystemBackup 131022 2000.bkp
c:\temp\others\ftps\www.123.com
c:\temp\others\ftps\www.123.com\backup-10.22.2013_00-00-08_123.tar.gz
c:\temp\others\ftps\www.123.com\backup-10.23.2013_00-00-08_123.tar.gz
c:\temp\others\ftps\www.123.com\backup-10.4.2013_00-00-07_123.tar.gz
c:\temp\others\mysql\sql.txt
c:\temp\others\rss-enterprise\enterprise.txt
c:\temp\others\rss-sql2k8\data
c:\temp\others\rss-sql2k8\data\New Bitmap Image.bmp
c:\temp\others\rss-sql2k8\data\New Journal Document.jnt
c:\temp\others\rss-sql2k8\data\New Microsoft PowerPoint Presentation.pptx
c:\temp\others\rss-sql2k8\data\New Microsoft Visio Drawing.vsd
这是我的脚本:
$date = get-date
$path = "C:\temp\others\*.*"
$destination = new-item "c:\temp\others\$($date.toshortdatestring().replace("/","-"))" -type directory
Foreach($file in (Get-ChildItem $path -file -recurse -exclude "callpilot.dev"))
{
If($file.LastWriteTime -gt (Get-Date).adddays(-1).date)
{
Move-Item -Path $file.fullname -Destination $destination
}
}
我错过了#6 和#7。你能帮我找到它们吗?
我收到了你的回复,谢谢。有些东西仍然无法正常工作。
这是我的脚本:
$date = get-date
$path = "C:\temp\others\*.*"
$destination = new-item "c:\temp\others\$($date.toshortdatestring().replace("/","-"))" -type directory
Foreach($file in (Get-ChildItem $path -file -recurse -exclude "callpilot.dev"))
{
If($file.LastWriteTime -gt (Get-Date).adddays(-2).date)
{
$destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root.ToString())
New-Item $destination -Type Directory -ea SilentlyContinue
Move-Item $file $destination
} else {
Remove-Item $file
}
}
文件已移动,但未创建文件夹。错误是:
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Move-Item : Cannot create a file when that file already exists.
At C:\temp\organize_final222.ps1:11 char:9
+ Move-Item $file $destination
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\temp\others\...sio Drawing.vsd:FileInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
At C:\temp\organize_final222.ps1:9 char:9
+ $destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
我是否在正确的位置插入了您的台词?