Eu escrevi um script que funcionou. Obrigado ao jftuga pela sugestão do subst.
# This script searches through the search_path, finds file paths that are longer than total_path_length_threshold, and truncates the filename
### user settings ###
$test_run = $true # set to $false to start renaming for real
$search_path = "C:\my_folder\" # the path to search for long names
$total_path_length_threshold = 260 # the length threshold to test
$characters_to_truncate = 5 # the number of characters to strip from the end of long filenames (a new identifier will also be added)
$log_file_path = "c:\my_folder\long_filename_log.txt" # log file location
$virtual_drive = "v:" # used for temporary processing using subst. Must look like "v:" and not like "v:\"
### end of user settings ###
Out-File $log_file_path
"Test run = $test_run" | Tee-Object $log_file_path -Append
"search_path = $search_path " | Tee-Object $log_file_path -Append
"total_path_length_threshold = $total_path_length_threshold " | Tee-Object $log_file_path -Append
"characters_to_truncate = $characters_to_truncate " | Tee-Object $log_file_path -Append
$counter = 0 # counter for creating new unique filenames
$collection = cmd /c dir $search_path /s /b |? {$_.length -gt $total_path_length_threshold }
$count_paths = ($collection | measure).count - 1
foreach ($path in $collection) {
echo "found long path: $path"
# get the parent's path
$parent_path = Split-path -path $path
# mount the parent to a virtual drive
subst $virtual_drive $parent_path
# get leaf element
$leaf = split-path -leaf $path
# make new short path
$short_virtual_path = join-path $virtual_drive $leaf
# get the long-name item
$item = Get-Item -LiteralPath $short_virtual_path
if ($item -is [System.IO.fileinfo]) {
# log long filename path
"item $counter : Long filename path: $path" | Out-File $log_file_path -Append
$filename = $item.name #get the full filename
# get filename extension
$filename_extension = $item.Extension
# get filename without extension:
$basename = $item.BaseName
$basename_length = $basename.length
# give the new filename a unique index, to avoid duplicate filenames
$new_index = "X" + $counter + "X"
# the actual number of characters to cut is characters_to_truncate + new_index length
$adjusted_characters_to_truncate = $characters_to_truncate + $new_index.length
if ( $basename_length -gt $adjusted_characters_to_truncate ) {
$length_to_use = $basename_length - $adjusted_characters_to_truncate
# shorten the filename by truncating it by specified num of char
$new_filename = $basename.substring(0, $length_to_use ) + $new_index + $filename_extension
# log the new filename path
$new_path = $parent_path + $new_filename
"item $counter : new filename path: $new_path " | Out-File $log_file_path -Append
if (!$test_run) {
# rename the file
Rename-Item -LiteralPath $short_virtual_path $new_filename
}
}
}
$counter = $counter + 1
#unmount the virtual drive
subst v: /d
}
"Found $count_paths paths." | echo