来自多个文件夹的照片归档

发布于 2025-02-06 13:40:54 字数 2481 浏览 1 评论 0原文

我正在制作一个归档脚本,我可以使用一些帮助,谢谢。

文件夹结构布局:

C:\Script_Testing\Photos_Folder\101\Accepted
C:\Script_Testing\Photos_Folder\101\Rejected
C:\Script_Testing\Photos_Folder\102\Accepted
C:\Script_Testing\Photos_Folder\102\Rejected

etc等。

所有接受和拒绝的文件夹都有PNG文件。这些文件来自多个摄像机,这就是为什么有多个编号文件夹的原因。我有组织照片的脚本(下图)。如果我要在单个文件夹中运行脚本(我希望这是有道理的),则该脚本有效。

我要弄清楚的是:

我只需要存档被接受的文件夹中的图片即可。 脚本以某种方式递归地执行目前正在做的事情,但仅用于文件夹

C:\Script_Testing\Photos_Folder\101\Accepted 
C:\Script_Testing\Photos_Folder\102\Accepted
C:\Script_Testing\Photos_Folder\103\Accepted

我需要 当它当前存档时,它会创建一组文件夹(假设照片来自今天):

C:\Script_Testing\Archived_Photos\2022\6\10\6102022.zip

上面的格式工作,但是我还需要保留101、102、103等,以便我知道照片来自哪些相机。

我希望它像这样存档:

C:\Script_Testing\Archived_Photos\101\2022\6\10\6102022.zip
C:\Script_Testing\Archived_Photos\102\2022\6\10\6102022.zip

等等

吗?

谢谢大家提供的任何帮助。

# Grab photos to be archived
$files = Get-ChildItem 'C:\Script_Testing\Photos_Folder' -Recurse | where { !$_.PsIsContainer }
    
# List of photos to be moved
$files
        
# Target where photos should be moved to. The script will automatically create a folder for the year, month, and date
$targetPath = 'C:\Script_Testing\Archived_Photos'
        
foreach ($file in $files) {
    # Get year and Month of the file
    $year = $file.LastWriteTime.Year.ToString()
    $month = $file.LastWriteTime.Month.ToString()
    $day = $file.LastWriteTime.Day.ToString()
        
    # Out FileName, year and month
    $file.Name
    $year
    $month
    $day
        
    # Set directory path structure
    $Directory = $targetPath + "\" + $year + "\" + $month + "\" + $day + "\"
    # Create directory if it doesn't exist
    if (!(Test-Path $Directory)) {
        New-Item $directory -type directory
    }
        
    # Move files to new location
    $file | Move-Item -Destination $Directory
        
    # Compress files by day then remove original PNG files.
    Compress-Archive -Path (Join-Path $directory *.png) -Update -DestinationPath ($Directory + (Get-Date -Format $day$month$year) + '.zip')
    Get-Item (Join-Path $directory *) -exclude *.zip | Remove-Item -WhatIf
}

I am working on an archiving script and I could use some help, please, and thank you.

Folder Structure Layout:

C:\Script_Testing\Photos_Folder\101\Accepted
C:\Script_Testing\Photos_Folder\101\Rejected
C:\Script_Testing\Photos_Folder\102\Accepted
C:\Script_Testing\Photos_Folder\102\Rejected

etc, etc.

All of the folders both Accepted and Rejected have PNG files. The files are coming in from multiple cameras which is why there are multiple numbered folders. I have the script (below) for organizing the photos. The script works if I were going to just run the script for a single folder (I hope that makes sense).

What I am trying to figure out is the following:

I only need to archive the pictures from the Accepted folders. I need the script to somehow recursively do what it is currently doing but only for folders:

C:\Script_Testing\Photos_Folder\101\Accepted 
C:\Script_Testing\Photos_Folder\102\Accepted
C:\Script_Testing\Photos_Folder\103\Accepted

etc.

It needs to keep that same format during the archive process. When it currently archives it creates a set of folders (assuming the photos are from today):

C:\Script_Testing\Archived_Photos\2022\6\10\6102022.zip

The format above works, but I need to also keep the 101, 102, 103, etc as well so I know what camera the photos came from.

I would like it to be archived like this:

C:\Script_Testing\Archived_Photos\101\2022\6\10\6102022.zip
C:\Script_Testing\Archived_Photos\102\2022\6\10\6102022.zip

etc.

Is this possible to do?

Thank you guys for any help you can provide.

# Grab photos to be archived
$files = Get-ChildItem 'C:\Script_Testing\Photos_Folder' -Recurse | where { !$_.PsIsContainer }
    
# List of photos to be moved
$files
        
# Target where photos should be moved to. The script will automatically create a folder for the year, month, and date
$targetPath = 'C:\Script_Testing\Archived_Photos'
        
foreach ($file in $files) {
    # Get year and Month of the file
    $year = $file.LastWriteTime.Year.ToString()
    $month = $file.LastWriteTime.Month.ToString()
    $day = $file.LastWriteTime.Day.ToString()
        
    # Out FileName, year and month
    $file.Name
    $year
    $month
    $day
        
    # Set directory path structure
    $Directory = $targetPath + "\" + $year + "\" + $month + "\" + $day + "\"
    # Create directory if it doesn't exist
    if (!(Test-Path $Directory)) {
        New-Item $directory -type directory
    }
        
    # Move files to new location
    $file | Move-Item -Destination $Directory
        
    # Compress files by day then remove original PNG files.
    Compress-Archive -Path (Join-Path $directory *.png) -Update -DestinationPath ($Directory + (Get-Date -Format $day$month$year) + '.zip')
    Get-Item (Join-Path $directory *) -exclude *.zip | Remove-Item -WhatIf
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-02-13 13:40:54

除了插入101102等。路径中的文件夹外,您的代码可能是更多的流线,我猜我首先要进行循环来创建子文件夹并移动文件,并移动文件,并且然后,在第二个循环中进行压缩/删除,而不是在每个文件中进行压缩。

这样的事情:

# Target where photos should be moved to. The script will automatically create a folder for the year, month, and date
$targetPath = 'C:\Script_Testing\Archived_Photos'
# Grab photos to be archived
$files = Get-ChildItem 'C:\Script_Testing\Photos_Folder\*\Accepted\*' -Filter '*.png' -File
  
$dirsToCompress = foreach ($file in $files) {
    $number = $file.DirectoryName.Split("\")[-2]  # e.g. 101
    # Set directory path structure
    $Directory = Join-Path -Path $targetPath -ChildPath ('{0}\{1:yyyy\\MM\\dd}' -f $number, $file.LastWriteTime)
    # Create directory if it doesn't exist
    $null = New-Item $directory -Type Directory -Force
    # move the file
    # for safety while testing, change this to `Copy-Item`
    $file | Move-Item -Destination $Directory
    # output the path to collect in $dirsToCompress
    $Directory
}

# now compress all files in the directories we have collected in $dirsToCompress
foreach ($folder in ($dirsToCompress | Select-Object -Unique)) {
    $zipTarget = Join-Path -Path $folder -ChildPath ('{0:ddMMyyyy}.zip' -f (Get-Date))
    $pngFiles  = Join-Path -Path $folder -ChildPath '*.png'
    Compress-Archive -Path $pngFiles -DestinationPath $zipTarget -Update
    Remove-Item -Path $pngFiles -Recurse -Confirm:$false
}

Besides inserting the 101, 102 etc. folder in the path, your code could be more streamlines I guess by first do a loop to create the subfolders and move the files, and then in a second loop do the compression/deleting instead of compressing each file as it comes along.

Something like this:

# Target where photos should be moved to. The script will automatically create a folder for the year, month, and date
$targetPath = 'C:\Script_Testing\Archived_Photos'
# Grab photos to be archived
$files = Get-ChildItem 'C:\Script_Testing\Photos_Folder\*\Accepted\*' -Filter '*.png' -File
  
$dirsToCompress = foreach ($file in $files) {
    $number = $file.DirectoryName.Split("\")[-2]  # e.g. 101
    # Set directory path structure
    $Directory = Join-Path -Path $targetPath -ChildPath ('{0}\{1:yyyy\\MM\\dd}' -f $number, $file.LastWriteTime)
    # Create directory if it doesn't exist
    $null = New-Item $directory -Type Directory -Force
    # move the file
    # for safety while testing, change this to `Copy-Item`
    $file | Move-Item -Destination $Directory
    # output the path to collect in $dirsToCompress
    $Directory
}

# now compress all files in the directories we have collected in $dirsToCompress
foreach ($folder in ($dirsToCompress | Select-Object -Unique)) {
    $zipTarget = Join-Path -Path $folder -ChildPath ('{0:ddMMyyyy}.zip' -f (Get-Date))
    $pngFiles  = Join-Path -Path $folder -ChildPath '*.png'
    Compress-Archive -Path $pngFiles -DestinationPath $zipTarget -Update
    Remove-Item -Path $pngFiles -Recurse -Confirm:$false
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文