Batch script (windows) for remove folder charecter length after x number character

Gon

Active Member
484
138
43
2014
4,680
there is multiple folder which character length is more than 200. can we remove all character after x number character using batch script?
 
3 comments

darkestblue

Active Member
680
369
63
2020
3,355
Code:
#powershell
$folder_path = "C:\Users\Admin\Desktop\temp"
$max_name_length = 200

#get all objects of type directoy from path
Get-ChildItem -path $folder_path -Directory | % {
    $folder_object = $_

    #check if foldername too long
    if($folder_object.Name.Length -gt $max_name_length){
        $folder_path_destination = $folder_object.Parent.FullName + "\" + $folder_object.Name.substring(0,$max_name_length)
        $folder_object | move-Item -Destination $folder_path_destination
    }
}
 

D4rk DeMoN

Active Member
864
714
93
2013
4,571
Code:
#powershell
$folder_path = "C:\Users\Admin\Desktop\temp"
$max_name_length = 200

#get all objects of type directoy from path
Get-ChildItem -path $folder_path -Directory | % {
    $folder_object = $_

    #check if foldername too long
    if($folder_object.Name.Length -gt $max_name_length){
        $folder_path_destination = $folder_object.Parent.FullName + "\" + $folder_object.Name.substring(0,$max_name_length)
        $folder_object | move-Item -Destination $folder_path_destination
    }
}

Nice one, I save it too, maybe I will need it in future.
 
Top Bottom