-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlong_paths_v3.ps1
56 lines (51 loc) · 1.91 KB
/
long_paths_v3.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
$Path = "C:\" # Specify the root directory to start the search
$MaxLength = 260 # Define the maximum path length limit
$OutputFile = "C:\long_paths.txt" # Output file to store results
# Clear the output file at the beginning
if (Test-Path $OutputFile) {
Remove-Item $OutputFile
}
# Function to append to the output file
function Log-LongPath($path) {
Add-Content -Path $OutputFile -Value $path
}
# Custom function to handle long paths and skip inaccessible paths, logging directly to file
function Get-FilesWithLongPath {
param (
[string]$Path,
[int]$MaxLength
)
# Enumerate all files, logging paths longer than MaxLength directly to file
try {
[System.IO.Directory]::EnumerateFiles($Path, "*", [System.IO.SearchOption]::AllDirectories) | ForEach-Object {
try {
if ($_.Length -gt $MaxLength) {
Log-LongPath $_
}
} catch {
# Log inaccessible file paths if necessary
# Log-LongPath "Inaccessible file: $_"
}
}
} catch {
Write-Warning "Unable to access some directories or files."
}
# Enumerate all directories, logging paths longer than MaxLength directly to file
try {
[System.IO.Directory]::EnumerateDirectories($Path, "*", [System.IO.SearchOption]::AllDirectories) | ForEach-Object {
try {
if ($_.Length -gt $MaxLength) {
Log-LongPath $_
}
} catch {
# Log inaccessible directory paths if necessary
# Log-LongPath "Inaccessible directory: $_"
}
}
} catch {
Write-Warning "Unable to access some directories or files."
}
}
# Execute the function
Get-FilesWithLongPath -Path $Path -MaxLength $MaxLength
Write-Host "Results saved to $OutputFile"