-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps-bates-enumerator-v5.ps1
108 lines (74 loc) · 2.63 KB
/
ps-bates-enumerator-v5.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# By Andre Abadi
# starting bits & info
echo "`nRecursive Document Enumerator by Andre Abadi.`n"
echo "Recurses this and all subdirectories"
echo " and lists all files with Bates numbers."
echo " EG: ABC.0001.0001.0001.pdf`n"
echo "Puts them in a file called '$($file)'"
echo " alongside this script.`n"
# ensure this script's executing directory is correct
$mydir = Get-Location
Set-Location -Path $mydir
# initiate an empty arraylist to hold copied files
$foundFiles = [System.Collections.ArrayList]@()
# initiate an empty arraylist to hold copied files
$foundDocIDs = [System.Collections.ArrayList]@()
$file = "enumerated-files.txt"
$filePath = "$($mydir)\$($file)"
$documents = "enumerated-docIDs.txt"
$documentsPath = "$($mydir)\$($documents)"
echo "The following files were found with Bates Numbers:"
echo "==================================================="
# recursively find all files in this and lower directories
$allFiles = Get-ChildItem -Recurse -File
# filter to regex matches
$regexHits = echo $allFiles | Where-Object {$_.Name -match '(.{3})\.(\d{4})\.(\d{4})\.(\d{4})(.*(?=\.))\.(.*)'}
# trim down to just filenames
$hitFileNames = echo $regexHits | Select-Object -ExpandProperty Name
# iterate through each hit
foreach ($instance in $hitFileNames)
{
# add this instance to the list of found files
$foundFiles.Add($instance) | Out-Null
# write the name of the found file
echo $instance
# trim DocID from instance filename
$instanceDocID = $instance.Substring(0,18)
# add the DocID to the list
$foundDocIDs.Add($instanceDocID) | Out-Null
}
echo "==================================================`n"
echo "Preparing output.`n"
if ($foundFiles.Count -gt 0)
{
# warn if overwriting old file
if (Test-Path $filePath)
{
echo "Overwriting old '$($file)'`n"
Remove-Item -Path $filePath -Force | Out-Null
}
# iterate through array and write output
foreach ($instance in $foundFiles)
{
Add-Content -Path $filePath -Value $instance
}
}
if ($foundDocIDs.Count -gt 0)
{
# warn if overwriting old file
if (Test-Path $documentsPath)
{
echo "Overwriting old '$($documents)'`n"
Remove-Item -Path $documentsPath -Force | Out-Null
}
# get just the uniques
$uniqueDocIDs = echo $foundDocIDs | Sort-Object | Get-Unique
foreach ($instance in $uniqueDocIDs)
{
Add-Content -Path $documentsPath -Value $instance
}
}
# ending bits
echo "Found $($foundFiles.Count) files and wrote to '$($file)'`n"
echo "Found $($uniqueDocIDs.Count) unique Document IDs and wrote to '$($documents)'`n"
Read-Host "Press ENTER to exit"