来自多个文件夹的照片归档
我正在制作一个归档脚本,我可以使用一些帮助,谢谢。
文件夹结构布局:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了插入
101
,102
等。路径中的文件夹外,您的代码可能是更多的流线,我猜我首先要进行循环来创建子文件夹并移动文件,并移动文件,并且然后,在第二个循环中进行压缩/删除,而不是在每个文件中进行压缩。这样的事情:
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: