-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_trim.ps1
64 lines (56 loc) · 2.31 KB
/
run_trim.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function ProcessFiles {
param(
[string]$folderPath
)
$skipPatterns = @('paper*.pdf', 'sub*.pdf')
# Process the files in the current folder
$files = Get-ChildItem -Path $folderPath -File
foreach ($file in $files) {
if ($file.Extension -match '\.(pdf|jpg|png)$') {
$srcFile = $file
# Skip files that match patterns in $skipPatterns
$skipThisFile = $false
foreach ($pattern in $skipPatterns) {
if ($srcFile.Name -like $pattern) {
$skipThisFile = $true
break
}
}
if ($skipThisFile) {
Write-Host ("Skipping file: " + $srcFile.FullName + " (matches skip pattern)")
continue
}
$cropFile = if ($srcFile.BaseName -like "*-crop") {
Join-Path $srcFile.DirectoryName ($srcFile.BaseName + $srcFile.Extension)
} else {
Join-Path $srcFile.DirectoryName ($srcFile.BaseName + "-crop" + $srcFile.Extension)
}
if ((-not (Test-Path $cropFile)) -or ((Test-Path $cropFile) -and ($srcFile.LastWriteTime -gt (Get-Item $cropFile).LastWriteTime))) {
Write-Host ("Processing file: " + $srcFile.FullName)
switch ($srcFile.Extension) {
".pdf" {
# your pdfcrop command here
pdfcrop $srcFile.FullName $cropFile
}
".jpg" {
# your imagemagick command here
magick $srcFile.FullName -trim $cropFile
}
".png" {
# your imagemagick command here
magick $srcFile.FullName -trim $cropFile
}
}
} else {
# Omitted the Write-Host statement that indicates the crop file is up-to-date
}
}
}
# Recursively go through each subfolder
$subfolders = Get-ChildItem -Path $folderPath -Directory
foreach ($subfolder in $subfolders) {
ProcessFiles -folderPath $subfolder.FullName
}
}
# Start the processing from the current directory
ProcessFiles -folderPath "."