Jump to content

Renaming Long File Names


fhanna
 Share

Recommended Posts

Am looking for a UDP or routine that would process each file name in a directory, return the path and length and if the path exceeds a specific length, restructure the path as to reduce the path below 260 characters.  The renaming of the path/file to be determined.  The file would be saved with the new path.  Would need to process the nested folders.  IOW trying to process directory/files and determine if long file names exist and, if so, rename. 

Link to comment
Share on other sites

Like this?

 

#include <File.au3>

Local $sFilePath = "C:\Windows\WinSxS\x86_microsoft-windows-healthattestation-csp_31bf3856ad364e35_10.0.10586.0_none_5b80efe9ad4f9bd6\Manifests\msil_microsoft.applicati..framework.resources_31bf3856ad364e35_10.0.10586.0_en-us_5ebbe270ea8ccbb9tmsil_microsoft.build.tasks.resources_b03f5f7f11d50a3a_10.0.10586.0_en-us_576ee33f8416a7f3.manifest"

Local $sNewFilePath = _GetNewShortPath($sFilePath)
If $sFilePath <> $sNewFilePath Then FileMove($sFilePath, $sNewFilePath, 1)

Func _GetNewShortPath($sFilePath)
    Local $sDrive, $sDir, $sFileName, $sExtension
    If StringLen($sFilePath) < 260 Then Return $sFilePath
    _PathSplit($sFilePath, $sDrive, $sDir, $sFileName, $sExtension)
    Local $sNewFilePath = $sDrive & $sDir & "NewName" & Random(9, 99) & $sExtension
    Return $sNewFilePath
EndFunc   ;==>_GetNewShortPath

 

Edited by Trong

Regards,
 

Link to comment
Share on other sites

Trong

Thank you so much for the response.  This is a start and if the members can develop this UDP I believe it can have tremendous usage for the Windows limitation of long file names.  This definitely is a great start but the additional request is:  Process a root folder.  Determine if any files have a length issue and if so rename/move based on a new name procedure.  Once the files in that directory has been processed go to the next directory and do the same.  Do this for all root and nested directories/files.  So, I do not have the expertise to proccess all directories/files.  Request - how do you read all the files at the root and files nested within the drill down folders?  I think I could handle the rename/move function; it's just the processing of all the files via an array, etc.  Due to 1000s of long file it is not feasable to navigate to the end folder and rename to a lesser length.  We handle copies at this point with Robocopy but restoring files from backup, other functions utilized by the standard copy function of Windows necessitate an alternative.  Our situation would probably result in actually manipulating folder names also and insuring that type of rename/move.  I do believe a great approach to this issue is a big plus.  Thank you all and hopefully, have not displeased anyone with my request for support.  I am experienced with AutoIt but not to this level.   

Link to comment
Share on other sites

  • Moderators

@fhanna this forum is dedicated to helping users with their own scripts; it is not a place where you put in a request and someone barfs up code for you ;)

Why don't you give it a try on your own and see what you can come up with? You state you're "not to this level" with AutoIt; no better way to learn. If you run into issues, feel free to post your code and we can assist.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

_FileListToArrayRec would be the function you're looking for.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

With respect to the moderator and the mission of this website I have begun my coding process.  I am doing baby steps and my first routine is utilizing the _FileListToArrayRec function. My first issue is the array returned (asking for the complete path) will return millions of array entries.  I am only looking for the records over 260 characters long.  Question; how can I filter and check the path lengths before the record is loaded into the array. 

fourm.au3

Link to comment
Share on other sites

You have to filter the results AFTER the function returns, there's no capability in that function to do that. You would have to loop through the returned array and look for any path/filename longer than your target and act accordingly.

If you require it to look for the long filenames prior to inserting them into the array, you'd have to write your own function, which would be exceedingly difficult for you to do if you're just starting out.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

You could do something like this, but I'm guessing FileListToArray is going to be faster over the course of millions of files:

#include <Array.au3>

Local $hFirstFile = FileFindFirstFile("C:\Windows\WinSXS\*.*")
Local $sFileName = "", $aArray[0]

    While 1
        $sFileName = FileFindNextFile($hFirstFile)
        If @error Then ExitLoop
        If StringLen($sFileName) < 80 Then _ArrayAdd($aArray, $sFileName)
    WEnd

    FileClose($hFirstFile)
    _ArrayDisplay($aArray)

 

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Thanks to all.  I will utilize a freeware package called "Path Scanner" which lets you create a csv file by selecting all records with a path greater than a desired length.  I will then read the individual records created via a FileReadLine command and then do my bidding.  I have utilized AutoIt3 and can handle (may ask a question or 2) but was hoping there was an easier way than _FileListToArrayRec.  I'm looking at a 5% selection of records exceeding the max length so this appears my best option.  Sorry the moderator got involved but was hoping there was a developted UDP addressing this. 

Link to comment
Share on other sites

Just remember with file name and paths that are longer than 260 characters, you may need to use Unicode addressing ("\\?\")to get to those files.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ok, here it is.  I cannot get the filemove function to work.  I get a 0 return code.  I have used with and without the Unicode addressing scheme.  Recap:  I am utilizing a package called Path Scanner to capture into a .csv file all files with a path of 261 or more.  After created I am reading that file record by record and creating a new random file name and utilizing the existing path.  So, the new path/file name is shortened to 260 characters.  Utilizing a message display to verify and all looks good.  Just cannot get the filemove to work.  Any suggestions. 

Rename.au3

Link to comment
Share on other sites

FileGetShortName

 

and then filemove the return of that.

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

For our file servers the 8.3 short name capability has been disabled.  Could not get it to work.  Have no control over Group Policies as a higher headquarters control.  Assume done so for speed purposes.  So, any other ideas?  I can use a command function and run Robocopy but have no idea how to delete the original file.  Cannot find any reference to deleting long file names; Robocopy or otherwise.  Any other suggestions?

Link to comment
Share on other sites

I was looking through your code and noticed this:

For $J = 1 To Random($FileNameLength, $FileNameLength, 1)

As the min and max are the same value, the return will be $FileNameLength from the Random function, in other words there's no point in using Random here at all.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

sorry for the late reply, 

For our file servers the 8.3 short name capability has been disabled.  Could not get it to work.  Have no control over Group Policies as a higher headquarters control.  Assume done so for speed purposes.  So, any other ideas?  I can use a command function and run Robocopy but have no idea how to delete the original file.  Cannot find any reference to deleting long file names; Robocopy or otherwise.  Any other suggestions?

robocopy has no issue with handling long paths, either for copying or removing files. there is a trick to use robocopy to remove too long path names, but you won't be wanting that.

 

there is a utility i wrote long time ago (as a recursion practice, actually, and also because i couldn't find a decent alternative) which lists too long paths. you can easily modify that to rename the file instead of just print it to a log. see here:

http://sourceforge.net/projects/tlpd/

what you have done so far is also a good starting point; however, what i'm really wondering is -

why are you troubled by long paths?

you mention that for your servers, 8.3 support is disabled. you correctly assume that this is done for performance reasons, but that's not the whole story. since most, if not all, modern applications should have no issue whatsoever handling long paths, then in practice, one side effect is that if you do encounter any issues, it means you are probably using an obsolete software (or a poorly-written one), and you should do something about that, not about the long paths.

that said, a remark about AutoIt: AutoIt itself supports long paths - partially, at least, and given the appropriate prefix. if you need a more complete support for an AutoIt script to handle long paths, there is this UDF:

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Unless I am not aware of a parameter for the Windows Copy function it will not provide for the copying of LFNs.  My initial request was for an existing UDF but did misquote as UDP.  I definitely will review your suggestion.  BTW, could you provide the ability with Robocopy to delete LFNs.  You stated there is a trick to do this as I cannot find any refrerence to deleting LFNs or any files with Robocopy.  Thank you again for your expertise.

Link to comment
Share on other sites

the native copy command does not support long paths, nothing you can do about that.

as for robocopy, the trick is to "mirror" (use the /mir switch) an empty folder onto the folder infected with long file names. see here for the simplest explanation, but google "robocopy delete long path" should yield a results galore.

if you wish to delete only the long paths and leave the legitimate length paths intact, then first perform a regular copy (that will copy the legitimate paths only), then use robocopy to mirror it back, so the files that were not copied will be removed.

but first thing first - please consider my wondering, as i expressed it two posts above. why are you troubled by long paths? what exactly are you trying to do that is failing due to long paths?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • 9 months later...
On 11/19/2015 at 1:45 PM, fhanna said:

Am looking for a UDP or routine that would process each file name in a directory, return the path and length and if the path exceeds a specific length, restructure the path as to reduce the path below 260 characters.  The renaming of the path/file to be determined.  The file would be saved with the new path.  Would need to process the nested folders.  IOW trying to process directory/files and determine if long file names exist and, if so, rename. 

Long too path error!
"Long path tool" is very helpful for this problem. You can solve this problem like copy, delete, long path files by using this tool.
I used to have similar problems too, but after using "long path tool" everything was solved. 

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...