Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
移动jpg';s使用带变量目录的powershell脚本_Powershell_Robocopy - Fatal编程技术网

移动jpg';s使用带变量目录的powershell脚本

移动jpg';s使用带变量目录的powershell脚本,powershell,robocopy,Powershell,Robocopy,我正在编写一个预定脚本,将图像(jpg)从一个位置移动到另一个位置。 问题是父目录的名称是可变的,而结束目录是固定的 如果robocopy能够做到这一点,我会很高兴:robocopy C:\temp\pcbmodel**\defect C:\test**\defect*。但事实并非如此 例如,这几乎可以实现: foreach ($i in Get-ChildItem C:\temp\pcbmodel\*\*\defect -recurse) { if ($i.CreationTime -lt (

我正在编写一个预定脚本,将图像(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中复制的,但**路径不是。以及我需要的*路径,因为它将为每个客户更改

一些建议会很好,
伯特

像这样的怎么样:

$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))} 
                 }
希望我做对了,这有帮助:)

编辑:使用-排除-包括参数,它们非常有用:)

EDIT2:Ofc,我读过头了,很抱歉这么多的编辑-你只是在找jpg文件

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

这将使您走上获得有效解决方案的正确道路。重要的部分是Resolve-Path cmdlet,它接受一个-Relative参数:。新建项-强制仅告诉它在需要时创建文件夹结构

# $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
}
另一个想法是: 不要在你的C:disk上尝试它。它将需要永远。但是,如果您对某个路径中的每个目录和子目录都有权限,并且子目录的数量不是很大,那么它应该可以工作

我在C:上创建了一个目录“newdirectory”,并在这个目录中创建了另一个“newdirectory”。和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

我认为这不会很快,你需要其中的两个来实现你的目标,我想它应该会起作用。

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


例如,在
C:\test
中,如果某些子文件夹包含文件,请复制到
D:\test
,而不是
D:\test\test\subfolders

是什么阻止您执行
机器人副本C:\temp\pcbmodel C:\test*.jpg/s
?谢谢,但问题不是获取文件,而是把它们放在相应的目录中。因此,原始映射可以是:c:\temp\pcbmodel\customerA\Files\Defect\images.jpg。需要将其移动到d:\archive\pcbmodel\customerA\Files\Defect\images.jpg。我无法在保持原始路径的其他目录中获取文件。。。让我知道它是否清晰。谢谢添加了一个新的Answare,因为它与此完全不同。希望这有帮助:)谢谢,但我不能让它工作。一些额外的帮助。我不知道如何将根与原始源分开。请解释“{0}\{1}”-f$DestinationRoot,(Resolve Path-Relative-Path$\”。谢谢,这是powershell字符串格式化程序;有一个很好的解释。谢谢。现在,我只需要删除的?有什么想法吗?PS C:\temp>C:\temp\test3.ps1 C:\test\。\pcbmodel\help1\help2\defect C:\test\。\pcbmodel\help1\help2\defect\t.jpgHi,我现在遇到了这个错误:解析路径:找不到路径“C:\test\pcbmodel\help1\help2\defect\t.jpg”,因为它不存在CategoryInfo:ObjectNotFound:(C:\test\pcbmode…p2\defect\t.jpg:String)[解析路径],位于C:\Temp\test5.ps1:14 char:61+If(-Not(测试路径($DestDir=Split Path-Parent-Path:$destem)){+~~~~~~~+CategoryInfo:InvalidData:(:)[Split Path],…它正在使路径正确,但不创建不存在的目录。如果您可以查看,请!解析路径似乎仅适用于已存在的项目。无论如何,已修复它。