从多个文件夹归档照片



我正在做一个存档脚本,我可以使用一些帮助,请,谢谢。

文件夹结构布局:

C:Script_TestingPhotos_Folder101Accepted
C:Script_TestingPhotos_Folder101Rejected
C:Script_TestingPhotos_Folder102Accepted
C:Script_TestingPhotos_Folder102Rejected

等等。

接受和拒绝的所有文件夹都有PNG文件。这些文件来自多个摄像头这就是为什么有多个编号的文件夹。我有组织照片的脚本(如下)。如果我只在一个文件夹中运行脚本,脚本就会工作(我希望这是有意义的)。

我想弄明白的是:

我只需要存档图片从接受文件夹。我需要脚本以某种方式递归地做它目前正在做的事情,但只针对文件夹:

C:Script_TestingPhotos_Folder101Accepted 
C:Script_TestingPhotos_Folder102Accepted
C:Script_TestingPhotos_Folder103Accepted

等。

在归档过程中需要保持相同的格式。当它当前存档时,它会创建一组文件夹(假设照片是今天的):

C:Script_TestingArchived_Photos20226106102022.zip

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

我希望这样归档:

C:Script_TestingArchived_Photos10120226106102022.zip
C:Script_TestingArchived_Photos10220226106102022.zip

等。

这是可能的吗?

感谢你们提供的任何帮助。

# Grab photos to be archived
$files = Get-ChildItem 'C:Script_TestingPhotos_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_TestingArchived_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
}

除了在路径中插入101,102等文件夹外,我猜你的代码可以更精简,首先做一个循环来创建子文件夹并移动文件,然后在第二个循环中做压缩/删除,而不是压缩每个文件。

像这样:

# Target where photos should be moved to. The script will automatically create a folder for the year, month, and date
$targetPath = 'C:Script_TestingArchived_Photos'
# Grab photos to be archived
$files = Get-ChildItem 'C:Script_TestingPhotos_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
}

最新更新