首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell 3.0中基于不同标准的文件移动

Powershell 3.0中基于不同标准的文件移动
EN

Stack Overflow用户
提问于 2013-10-25 15:09:04
回答 1查看 3.3K关注 0票数 1

我读过“https://stackoverflow.com/questions/ask/advicePowershell Move-Item Rename If File Exists http://social.technet.microsoft.com/Forums/scriptcenter/en-US/c347ce7a-3e23-4476-90c2-37b79785ac2f/moving-files-based-on-modified-date Powershell: Move Files recursively http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/22/hey-scripting-guy-september-22-2009.aspx”和大约20多篇文章。

我很抱歉,虽然我喜欢学习脚本以及它提供的可能性,但是我对脚本一无所知。除了使用Windows的批处理文件之外,我以前的位置不需要我编写脚本。我最近接受了一个网络管理职位,我的主管让我编写一个脚本,根据几个标准来移动文件。我正在使用Powershell来完成这个任务。

  1. 更新超过一天的文件被移动-not复制-从$path到$destination
  2. 排除适用于必须保持不变的callpilot.dev
  3. 必须移动位于源文件夹中的文件以及位于所有子文件夹中的文件。
  4. 源文件夹结构必须保持原状。
  5. 目标顶部文件夹必须包含实际日期减去1天。
  6. 必须使用与源结构相同的名称创建所有子文件夹结构。
  7. 剩余的源文件应被删除(callpilot.dev除外)

下面是我的测试文件夹结构:

代码语言:javascript
复制
    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

这是我的剧本:

代码语言:javascript
复制
$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号,你能帮我找到吗?

我收到你的回复了,谢谢。还是有些事情不正常。

这是我的剧本:

代码语言:javascript
复制
$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
}
}

文件被移动,但没有创建文件夹。错误是:

代码语言:javascript
复制
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

我把你的台词插入到正确的地方了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-25 19:58:53

您可以使用$file.DirectoryName.TrimStart($file.Directory.Root.ToString())获得没有驱动器名的源folderpath,因此这将处理#6:

代码语言:javascript
复制
$destination += '\' + $file.DirectoryName.TrimStart($file.Directory.Root.ToString())
New-Item $destination -Type Directory -ea SilentlyContinue
Move-Item $file $destination

几个注意事项:

  • 第一行将原始路径(不包括驱动器号)附加到$destination
  • 如果目标目录不存在,则第二行创建目标目录。如果目标目录已经存在,-ea SilentlyContinue将阻止它发出抱怨。你可以肛交做if (! (Test-Path $destination)) {New-Item ...,但我认为那是浪费时间。
  • 您可以省略参数名称、-Path、和,因为它们是由位置所暗示的(谷歌"powershell位置参数“以获得更多信息)。
  • 您不需要读取FullName属性。如果向字符串参数提供非字符串参数,PowerShell将自动对参数调用ToString()方法。因此,Move-Item $file $destMove-Item $file.ToString() $dest相同,调用FileInfo对象上的ToString()方法会给出FullName属性(这是由对象类定义指定的)。这似乎是个小问题,但一旦你习惯了,你会发现自己做的不必要的打字要少得多。一般规则是,如果从直觉上看它似乎应该工作,那么它很可能会工作(正如在直觉中一样,您应该能够告诉它移动一个文件对象,而不需要做任何花哨的事情,比如指定对象的属性--所以您可以这样做)。

对于#7,只需添加一个else子句来删除与条件不匹配的源文件:

代码语言:javascript
复制
if ($file.LastWriteTime -gt (Get-Date).AddDays(-1).Date) {
  Move-Item $file "$destination\$($file.DirectoryName.TrimStart($file.Directory.Root.ToString()))"
} else {
  Remove-Item $file
}

为了安全起见,我建议使用Remove-Item -WhatIf $file进行测试,以确保您得到了所需的结果。更好的方法是在脚本开始时声明[CmdletBinding(SupportsShouldProcess=$true)],并使用-WhatIf运行整个脚本,但这可能比您现在想要的更高级。重要的是,在让脚本进行大量不可逆转的更改之前,始终要进行一次无损测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19593464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档