Moving jpg's using powershell script with variable directories

两盒软妹~` 提交于 2019-12-24 10:55:47

问题


I am writing a scheduled script that will move images (jpg's) from one location to another. The problem is that a parent directory is variable in name while the end directory is fixed.

If robocopy would do this I would be happy: robocopy C:\temp\pcbmodel**\defect c:\test**\defect*. But it doesn't

For example this will almost work:

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
}
}

The problem is that the files are copied in c:\test but the ** path isn't. And the * path I need as it will change for each customer.

Some suggestions would be nice, Bert


回答1:


This should put you on the right path to getting a working solution. The important part is the Resolve-Path cmdlet which takes a -Relative parameter: http://technet.microsoft.com/en-us/library/hh849858.aspx. new-Item -Force merely tell it to create a folder structure if required.

# $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
}



回答2:


How about something like that:

$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))} 
                 }

Hope I got it right and this helps :)

EDIT: Work with -Exclude and -Include parameters they help a lot :)

EDIT2: Ofc, I overread something, sorry for the many edits - you are just looking for jpg files.

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



回答3:


Another idea: Don't try it on your C: disk. It will need forever. But if u have permissions for every directory and subdirectory in a certain path and the subdirectory amount is not enormous it should work.

I created a directory "New Directory" on C: and another "New Directory" in this directory. And ofc, another "New Directory" in the directory. and lastly I created a file with the name "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"}}

Result:

C:\New Directory

I think it won't be fast and you would need 2 of these to achieve your goal, I guess it should work.




回答4:


I'm trying to copy all subfolders and their files from one folder into another, without copying their parent folder as well.

For example, in C:\test with some subfolders containing files, copy to D:\test without it being D:\test\test\subfolders.



来源:https://stackoverflow.com/questions/17788208/moving-jpgs-using-powershell-script-with-variable-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!