【问题标题】:Moving jpg's using powershell script with variable directories使用带有可变目录的powershell脚本移动jpg
【发布时间】:2015-09-24 01:23:05
【问题描述】:

我正在编写一个预定脚本,它将图像(jpg 的)从一个位置移动到另一个位置。 问题是父目录的名称是可变的,而最终目录是固定的。

如果 robocopy 会这样做,我会很高兴:robocopy C:\temp\pcbmodel**\defect c:\test**\defect*。但它没有

例如,这几乎可以工作:

foreach ($i in Get-ChildItem C:\temp\pcbmodel\*\*\defect -recurse)
{
if ($i.CreationTime -lt ($(Get-Date).AddMonths(0)))
{
    write $i.FullName
    Copy-Item $i.FullName C:\Test
}
}

问题是文件被复制到 c:\test 但 ** 路径不是。还有我需要的 * 路径,因为它会因每个客户而改变。

一些建议会很好, 伯特

【问题讨论】:

  • 究竟是什么阻止了你做robocopy C:\temp\pcbmodel C:\test *.jpg /s

标签: powershell robocopy


【解决方案1】:

这应该会让您走上获得有效解决方案的正确道路。重要的部分是 Resolve-Path cmdlet,它采用 -Relative 参数:http://technet.microsoft.com/en-us/library/hh849858.aspx。 new-Item -Force 只是告诉它在需要时创建一个文件夹结构。

# $OldRoot = 'Top-level of old files'
# $DestRoot = 'Top-level of destination'
# Go to old root so relative paths are correct
Set-Location $OldRoot
# Get all the images, then for each one...
Get-ChildItem -Recurse -Include "*.jpeg", "*.jpg" | 
ForEach-Object { 
        # Save full name to avoid issues later
        $Source = $_.FullName

        # Construct destination filename using relative path and destination root
        $Destination = '{0}\{1}' -f $DestRoot, (Resolve-Path -Relative -Path:$Source).TrimStart('.\')

        # If new destination doesn't exist, create it
        If(-Not (Test-Path ($DestDir = Split-Path -Parent -Path:$Destination))) { 
            New-Item -Type:Directory -Path:$DestDir -Force -Verbose 
        }

        # Copy old item to new destination
        Copy-Item -Path:$Source -Destination:$Destination -Verbose
}

【讨论】:

  • 谢谢,但我无法正常工作。一些额外的帮助。我不知道如何将根与原始来源分开。请解释 '{0}\{1}' -f $DestinationRoot, (Resolve-Path -Relative -Path $_)。谢谢
  • 这是powershell字符串格式化程序; devcentral.f5.com/blogs/us/… 有很好的解释。
  • 谢谢。现在我只需要删除 . ?任何想法? PS C:\temp> C:\Temp\test3.ps1 c:\test\.\pcbmodel\help1\help2\defect c:\test\.\pcbmodel\help1\help2\defect\t.jpg
  • 嗨,我现在收到此错误:Resolve-Path : Cannot find path 'C:\test\pcbmodel\help1\help2\defect\t.jpg' 因为它不存在。 + CategoryInfo : ObjectNotFound: (C:\test\pcbmode...p2\defect\t.jpg:String) [Resolve-Path], At C:\Temp\test5.ps1:14 char:61 + If(-Not (Test-Path ($DestDir = Split-Path -Parent -Path:$DestItem))) { + ~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Split-Path], .. 它是使路径正确,但在目录不存在时不创建目录。如果你可以看看,请!
  • Resolve-Path 似乎只适用于已经存在的项目。总之,修好了。
【解决方案2】:

这样的事情怎么样:

$step1 = get-childitem  C:\temp\pcbmodel\
$Else = #FILETYPE YOU DO NOT WANT
$step1 | foreach {get-childitem -recurse -exclude "Directories", $ELSE | where {$_.Creationtime -lt ($(get-date).Addmonths(0))} 
                 }

希望我做对了,这会有所帮助:)

编辑:使用 -Exclude-Include 参数会很有帮助:)

EDIT2:Ofc,我读多了一些东西,很抱歉进行了许多编辑 - 你只是在寻找 jpg 文件。

$step1 | foreach {get-childitem -recurse -include *.jpg, *.jpeg | where {$_.Creationtime -lt ($(get-date).Addmonths(0))} 

【讨论】:

  • 您好,谢谢,但问题不是获取文件,而是将它们放在相应的目录中。所以原始地图可以是:c:\temp\pcbmodel\customerA\Files\Defect\images.jpg。它需要移动到 d:\archive\pcbmodel\customerA\Files\Defect\images.jpg。我无法将文件放在维护原始路径的不同目录中......让我知道它是否清晰(或不清晰)。谢谢!
  • 添加了一个新的答案,因为它与这个完全不同。希望这对现在有帮助:)
【解决方案3】:

另一个想法: 不要在 C: 磁盘 上尝试。它将永远需要。但是,如果您对某个路径中的每个目录和子目录都具有权限,并且子目录的数量不是很大,那么它应该可以工作。

我在 C: 上创建了一个目录“新目录”,并在该目录中创建了另一个“新目录”。和ofc,目录中的另一个“新目录”。最后我创建了一个名为“superduper.txt”的文件-> C:\New Directory\New Directory\New Directory\superduper.txt

(get-childitem C:\).FullName | where {(Get-ChildItem $_).FullName | where {(get-childitem $_ -Recurse).FullName -match "superduper"}}

结果:

C:\New Directory

我认为它不会很快,你需要其中 2 个来实现你的目标,我想它应该可以工作。

【讨论】:

    【解决方案4】:

    我正在尝试将所有子文件夹及其文件从一个文件夹复制到另一个文件夹,而不复制其父文件夹。

    例如,在带有一些包含文件的子文件夹的C:\test 中,复制到D:\test 而不是D:\test\test\subfolders

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      相关资源
      最近更新 更多