-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps-bates-enumerator-v6.ps1
130 lines (93 loc) · 3.53 KB
/
ps-bates-enumerator-v6.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# By Andre Abadi
# treat all non-terminating errors as terminating errors
$ErrorActionPreference = "STOP"
# 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)"
# starting bits & info
echo "`nRecursive Document Enumerator by Andre Abadi."
echo "`n Recurses this and all subdirectories"
echo " and lists all files with Bates numbers."
echo " EG: ABC.0001.0001.0001.pdf"
echo " Puts them in a file called '$($file)'"
echo " alongside this script."
echo " Puts their DocIDs in a file called '$($documents)'"
echo " alongside this script."
echo "`nThe following files were found with Bates Numbers:"
echo "==================================================="
# recursively find all files in this and lower directories
try {
$allFiles = Get-ChildItem -Recurse -File
}
# catch the old path-too-long error, give feedback and exit
catch [System.IO.PathTooLongException]
{
echo "`nOne of the specified path, file name, or both are too long."
echo " The fully qualified file name must be less than 260 characters,"
echo " and the directory name must be less than 248 characters."
echo "`nExiting because otherwise results would be incomplete."
}
# regurgitate any other errors
catch {
$_
}
# 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
# run fresh regex for a per-instance Matches table
$instance -match '(.*)(.{3})\.(\d{4})\.(\d{4})\.(\d{4})(.*(?=\.))\.(.*)'| Out-Null
# parse the DocID from the regex group matches
$instanceDocID = "$($Matches[2]).$($Matches[3]).$($Matches[4]).$($Matches[5])"
# add the DocID to the list
$foundDocIDs.Add($instanceDocID) | Out-Null
}
echo "==================================================`n"
if ($foundFiles.Count -gt 0)
{
# warn if overwriting old file
if (Test-Path $filePath)
{
echo "Overwriting old '$($file)'"
Remove-Item -Path $filePath -Force | Out-Null
}
echo "Writing found files to '$($file)'"
# iterate through array and write output
foreach ($instance in $foundFiles)
{
Add-Content -Path $filePath -Value $instance
}
echo "Wrote $($foundFiles.Count) files to '$($file)'`n"
}
if ($foundDocIDs.Count -gt 0)
{
# warn if overwriting old file
if (Test-Path $documentsPath)
{
echo "Overwriting old '$($documents)'"
Remove-Item -Path $documentsPath -Force | Out-Null
}
echo "Writing found DocIDs to '$($documents)'"
foreach ($instance in $foundDocIDs)
{
Add-Content -Path $documentsPath -Value $instance
}
echo "Wrote $($foundDocIDs.Count) Document IDs to '$($documents)'`n"
}
# ending bits
Read-Host "Press ENTER to exit"