The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Networking (http://hintsforums.macworld.com/forumdisplay.php?f=14)
-   -   Count Path Characters before upload to server (http://hintsforums.macworld.com/showthread.php?t=101195)

jeremyoliver 04-30-2009 04:04 PM

Count Path Characters before upload to server
 
I need to be able to count the number of characters in my longest file paths before I upload the folders to my server. My limit is 255. I'm not sure if I reach that amount however I'd like to start checking each folder before I upload to avoid any issues in the future. How do I do this in osx?

thanks.

tlarkin 04-30-2009 04:28 PM

I guess you could write a shell script that parses the amount of characters in the full path and if it is under 255 it can rysnc but if over it will error out?

I am not sure exactly how to count out characters in a path though. I guess I would start with the cd command and then maybe the pwd command but how to count the characters from there is escaping me at the moment. I am sort of a novice scripter though.

and of course I figure out a way to do it right when I hit post...

Code:


/bin/pwd | /usr/bin/wc -m

This will print the amount of characters in the current directory.

jeremyoliver 04-30-2009 05:06 PM

sorry, what do I do with that code? copy and paste it into terminal? I'm obviously clueless when it comes to scripting.

tlarkin 04-30-2009 05:09 PM

Hmm, OK, well is this network share already mapped? Or are you mapping it or how is it set up?

The code I posted will just print the amount of characters in the current directory you are in. So if you are in /Users/jeremyoliver/documents, and you run that command it will print the number of characters in that file path.

To script it you would have to add a few lines, it seems simple enough and perhaps I can actually help you do it. I just would need some more info.

tw 05-01-2009 12:10 AM

how are you uploading the files? though applescript, a shell script, an ftp app? and when you say that the 'file path' can only be 255 characters, do you mean the 'actual' file path can't be more than 255 characters (including the folder heirarchy? this seems unlikely), or do you mean that the file name itself cannot be more than 255 characters?

hayne 05-01-2009 02:03 AM

You could use my "reportLongPaths" script: http://hayne.net/MacDev/Perl/reportLongPaths
Sample usage:
reportLongPaths 100 ~/Documents

If you aren't familiar with running scripts in Terminal, see this Unix FAQ

jeremyoliver 05-01-2009 11:56 AM

thanks for the reply everyone. Here's the reply from my IT guy when I asked him if file name length was an issue I should be conscious of. I have access to our server from My mac and I just drag and drop files into a folder titled "creative". The file name length doesn't seem to be an issue, it's the total character count of the file path.

"The maximum file path length is 255 characters on a Windows server. For example, if you make a subfolder in the creative folder, the path length is already 17 because on the server it appears as “E:\DATA\Creative\”. So any files in this directory must be 238 characters or less. The more subdirectories you have, the shorter the filename can be. In the end, all of the characters in the subfolders to reach the file, plus the characters in the filename must be less than 255.
We have seen this issue before when one of our clients went nuts with the amount of subfolders they had. It causes tons of issues, files can’t be accessed, backed up, or even deleted. It was a huge nuisance.
You are the first person to ever proactively ask this question. I really appreciate it because in the end you are saving (wasteful) maintenance time on the server, and saving money. Nice……."

cwtnospam 05-01-2009 12:31 PM

Quote:

Originally Posted by jeremyoliver (Post 531020)
"The maximum file path length is 255 characters on a Windows server. For example, if you make a subfolder in the creative folder, the path length is already 17 because on the server it appears as “E:\DATA\Creative\”. So any files in this directory must be 238 characters or less. The more subdirectories you have, the shorter the filename can be. In the end, all of the characters in the subfolders to reach the file, plus the characters in the filename must be less than 255.

:rolleyes:
Windows. You're great-grandfather's OS.

This is going to be a bit tricky because it's the destination path that can't be more than 255 characters, not the source. That means you need to add the number of characters in the destination folder path to the number of characters in the file name and keep that below 256.

tw 05-01-2009 01:29 PM

try this droplet. open the Script Editor, paste the following code in, and save it on the desktop as a script application. it will show up as a droplet, and you can drag files and folders onto it. it should check through all of the files folders and calculate whether the new filepath length will go beyond 255 chars, and alert you if one does. uncomment the 'display alert' line to test it; uncomment the 'tell application "Finder" to move...' line once you've set the correct path the the server folder and you want to start moving things.

I haven't tested it beyond basic functionality though.

Code:

property serverPathLength : 17
property theServerFolder : "" --file system path to the server folder

on open theItems
        repeat with thisItem in theItems
                tell application "System Events"
                        set parentPath to POSIX path of container of thisItem
                end tell
                iterate(thisItem, length of parentPath)
        end repeat
        --tell application "Finder" to move theitems to theServerFolder
end open

to iterate(thisItem, parentPathLength)
        set itemInfo to info for thisItem
        set isFolder to folder of itemInfo
        tell application "System Events"
                set theFilePath to POSIX path of thisItem
                set finalPathLength to (length of theFilePath) - parentPathLength + serverPathLength
                -- display alert finalPathLength
                if finalPathLength ≥ 255 then
                        set fileName to name of thisItem
                        if isFolder then
                                set folderAlert to " This does not include the contents of the folder, which will (obviously) be longer."
                        else
                                set folderAlert to ""
                        end if
                        display alert fileName & " (" & theFilePath & ") will exceed the 255 character file path limit on the server by " & (finalPathLength - 255) & " characters." & folderAlert & " Please shorten the file name, or remove excess folders to resolve the problem" as critical
                        tell me to quit
                end if
        end tell
        if isFolder then
                tell application "Finder" to set folderContents to every item of folder (thisItem as text) as alias list
                repeat with thisNewItem in folderContents
                        iterate(thisNewItem, parentPathLength)
                end repeat
        end if
end iterate


tlarkin 05-01-2009 02:15 PM

You could also have launchd watch the path and any time a file or folder it put into that path it can execute a script.

tw 05-01-2009 02:27 PM

Quote:

Originally Posted by tlarkin (Post 531039)
You could also have launchd watch the path and any time a file or folder it put into that path it can execute a script.

that's an interesting thought. would it work when the folder in question is a remote folder on a windows system?

tlarkin 05-01-2009 02:36 PM

Quote:

Originally Posted by tw (Post 531041)
that's an interesting thought. would it work when the folder in question is a remote folder on a windows system?

Technically yes it should, it will be running from the mac and the files are created from the Mac so it should be full Unix POSIX. Unless the server side setup has some other things going on.

Just set launchd to watch the specified path and whenever the path is modified it can execute a script. You may have to do some wild carding and loops to make sure it hits every sub folder but I think it may be worth a shot if this is really an issue.

Of course it is is a multi user issue you would want the server to run it and perhaps set up some sort of ACLs so that a system daemon can watch that path.

I don't have any Windows servers that are part of my Mac infrastructure at work so I have never needed to file share really where a Mac was writing files to a Windows box. All the files I need the Macs to hit are for read only purposes.

cwtnospam 05-01-2009 02:52 PM

Doesn't the script need to run before the file is copied to the server? If the file path is too long, it's already to late when it gets there.

jeremyoliver 05-01-2009 03:04 PM

Damn this is confusing....

What I've been doing so far is using the finder 3 column view, select the file in the deepest sub folder and estimate the amount of characters in the file path. 1" is approx 13-15 characters. lol. I haven't found any file paths that are even close to 200 characters long. I know this seems tedious but I'm completely lost with regards to the suggestions posted above.

tlarkin 05-01-2009 03:20 PM

Quote:

Originally Posted by cwtnospam (Post 531047)
Doesn't the script need to run before the file is copied to the server? If the file path is too long, it's already to late when it gets there.

Well I believe the limitation is on the Windows side not the Mac, hence the Mac has the ability to create those and read those over 255 characters, where as the Windows machines don't.

You could even set up a launchd item that just periodically scans that directory and anything over the limit it moves to a "move this" folder.

cwtnospam 05-01-2009 03:47 PM

What I meant was that you need to determine that it's going to be over 255 before it gets moved to the Windows side because if it is and it gets moved, you're going to have problems.

tlarkin 05-01-2009 03:55 PM

Quote:

Originally Posted by cwtnospam (Post 531059)
What I meant was that you need to determine that it's going to be over 255 before it gets moved to the Windows side because if it is and it gets moved, you're going to have problems.

Only on the Windows side though, and the second you write to that path, it will execute the script via launchd. You set launchd to watch for any sort of modification. So even editing files will trigger the launchd item. If you use the keepalive option it will run constantly, or you can set a set time it runs.

cwtnospam 05-01-2009 07:37 PM

I think I misunderstood how the script works. I thought it was checking the files at their destination. It checks their new path before moving them.

tw 05-01-2009 08:45 PM

Quote:

Originally Posted by cwtnospam (Post 531085)
I think I misunderstood how the script works. I thought it was checking the files at their destination. It checks their new path before moving them.

no, actually, I think a launchd item won't run until after the file is moved to the windows box. launchd wont trigger until there's a change in the filepath, and the filepath won't be changed until after the move, so (and that's even assuming launchd can recognize a change on a Windows file path in the first place). that's why I like my droplet idea: the droplet will check file paths and flag problems before any attempt is made to move the item over.

tlarkin 05-02-2009 04:39 AM

Quote:

Originally Posted by tw (Post 531090)
no, actually, I think a launchd item won't run until after the file is moved to the windows box. launchd wont trigger until there's a change in the filepath, and the filepath won't be changed until after the move, so (and that's even assuming launchd can recognize a change on a Windows file path in the first place). that's why I like my droplet idea: the droplet will check file paths and flag problems before any attempt is made to move the item over.

You are right, 100%. However, I think that launchd can detect any change in a path regardless of where the source is from. I think it would probably work.

cwtnospam 05-02-2009 09:26 AM

Launchd would probably work, but Windows would screw up the path for any files whose path was greater than 255, so the script would fail.

tw 05-02-2009 11:07 AM

well, solving one problem, I've discovered it's actually very easy to identify files whose paths will be too long. the following applescript (which takes a normal file system alias for theItem) should do it:

Code:

set theItemPath to POSIX path of theItem
set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set chop to -2
if last text item of theItemPath is "" then set chop to -3
set theParentPath to text items 1 thru chop of theItemPath
set AppleScript's text item delimiters to tid
set theServerBasePathLength to 17
set theCurrentBasePathLength to length of theParentPath
set cmd to "find " & quoted form of theItemPath & " | awk 'length($0) - " & theCurrentBasePathLength & "+" & theServerBasePathLength & " >255' -"
set theFileList to do shell script cmd

now it's just a question of when and how the OP wants to call it, and what to do with the files that are too long.

jeremyoliver 05-14-2009 11:34 AM

I'm not sure what to think of this discussion. Again, I wouldn't know what to do with any of that coding. I don't want the server to have to do anything.

I want to do a quick character check on certain files before I throw them in my backup folder on the server. So far, it seems like my primitive method of using the triple column view and estimating character count based on 1" is approx 13-15 characters is the quickest and least complicated. agreed?

thanks everyone.

tw 05-14-2009 12:03 PM

Quote:

Originally Posted by jeremyoliver (Post 533036)
I'm not sure what to think of this discussion. Again, I wouldn't know what to do with any of that coding. I don't want the server to have to do anything.

I want to do a quick character check on certain files before I throw them in my backup folder on the server. So far, it seems like my primitive method of using the triple column view and estimating character count based on 1" is approx 13-15 characters is the quickest and least complicated. agreed?

Jeremy, you're the Original Poster, so what you want is what matters. however, if you just wanted a way to do a guesstimate you didn't need to post a question here. you simply haven't yet explained the problem sufficiently for us to come to a technical solution. questions:
  • Is this just for your personal use, or is it for the use of a number of people who all use macs?
  • would you prefer to drag your files to a droplet, use a menu item to select them, run an application?
  • do you want the script to upload the files, or just check to see if it will work?
  • what do you want the script do if it finds a file that will be too long? skip it, skip the whole move, rename the file?
gotta specify what you want before we can give it to you

jeremyoliver 05-26-2009 03:13 PM

* Is this just for your personal use, or is it for the use of a number of people who all use macs?
- 4 designers, we see the server shortcut on our macs. We drop files there. We want to check if the file path character count is more than 250 characters. on our macs, before transferring.

* would you prefer to drag your files to a droplet, use a menu item to select them, run an application?
- just drag and drop the files in a design folder we have on the server.

* do you want the script to upload the files, or just check to see if it will work?
- count the file path characters.

* what do you want the script do if it finds a file that will be too long? skip it, skip the whole move, rename the file?
- nothing. we'll fix them ourselves if they're too long.

thanks for the help.

tw 05-26-2009 05:08 PM

ok, well you can't just drop the files on the server folder - once you start the transfer to the windows machine, the process is out of the Mac's hands. what you'll need to do instead is drop the files on a droplet which checks their lengths. so do this:
  1. open Script Editor and copy/paste the following script into it
  2. save the script as an application
  3. move the application to the desktop, place it in the dock, put it in the script menu, or all of the above. eventually you'll want a copy of this script on each Mac.
  4. when you want to transfer files, drag and drop them onto this droplet, or select them in the finder and run the droplet from the script menu. if it's all clear, it will give you the option (which needs debugging) of moving the files to the server automatically, if there are issues it will give you the local (Mac) paths of the files that will go over the limit.
Code:

property serverBasePath : "E:"
-- or whatever the base path on the Windows machine is
property mountedDiskName : "E"
-- or whatever name the server disk appears as on the desktop
property maxLength : 255

global tooLong

on run
        tell application "Finder"
                set theItems to selection as alias list
        end tell
        mainLoop(theItems)
end run

on open theItems
        mainLoop(theItems)
end open

on mainLoop(theItems)
        set tooLong to {}
        repeat with thisItem in theItems
                checkLengths(thisItem as text)
        end repeat
        if tooLong is not {} then
                set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
                set tooLongList to tooLong as text
                set AppleScript's text item delimiters to tid
                tell application "Finder"
                        activate
                        display alert "Path Length Warning" message "The following folders and/or files will produce paths of more than " & maxLength & " characters on the Windows server.  Please fix." & return & return & tooLongList as warning
                end tell
        else
                tell application "Finder"
                        activate
                        display alert "Path Length Warning" message "This transfer should work correctly.  Move files to server?" as informational buttons {"Quit", "Move Files"} default button 2
                        if button returned of the result is "Move Files" then
                                move theItems to disk mountedDiskName
                        end if
                end tell
        end if
end mainLoop

to checkLengths(theItem)
        set itemInfo to info for alias theItem
        set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
        set baseFolderCount to (count of text items of (theItem)) - (folder of itemInfo as integer)
        set AppleScript's text item delimiters to tid
        iterate(theItem, baseFolderCount)
end checkLengths

to iterate(theItem, baseFolderCount)
        set itemInfo to info for alias theItem
        set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
        set serverPath to serverBasePath & (text items baseFolderCount thru -1 of theItem)
        set AppleScript's text item delimiters to "\\"
        set serverPath to serverPath as text
        set AppleScript's text item delimiters to tid
        if length of serverPath > maxLength then
                set end of tooLong to theItem
        else
                if folder of itemInfo is true then
                        display dialog theItem
                        tell application "System Events"
                                set theItems to (get every disk item of folder theItem whose visible is true)
                        end tell
                        repeat with thisItem in theItems
                                iterate(thisItem as alias as text, baseFolderCount)
                        end repeat
                end if
        end if
end iterate

This probably will need tweaking - experiment a bit with some throw-away copies of files, and let us know what needs to be revised.

jeremyoliver 05-27-2009 10:13 AM

Thanks tw.

So, I pasted the code into script editor, saved it as an application on my desktop. Dragged a working folder that I want to upload (one that has less than 255 character paths for sure) and this pops up:
http://i426.photobucket.com/albums/p...eroliver/1.jpg

I click OK, then this pops up:
http://i426.photobucket.com/albums/p...eroliver/2.jpg

I click "OK" and nothing happens. So I tried it again and clicked "edit", it opened the app in script editor.

So, I assumed this meant the character length was <255 characters. (I didn't see any indication it was less or more but I knew this particular folder path was <255 characters.)

Next, I created an extremely long file path with definitely more than 255 characters and I got the exact same pop ups:
http://i426.photobucket.com/albums/p...eroliver/3.jpg

I tried just clicking the script editor created application as well and I got this:
http://i426.photobucket.com/albums/p...r/Picture5.png

I don't understsand this. I didn't select any folder, it never gave me a chance to select any particular folder.
If I select "move files" I get this:
http://i426.photobucket.com/albums/p...r/Picture6.png

I don't need the script to move the files anywhere. I'll copy them over once I know there isn't any file paths in the folder that exceed 255 characters.

tw 05-27-2009 10:27 AM

that's because I left a debugging line in the script - 10 lines from the bottom, says display dialog theItem. you can delete that line (moderators, if you see this, please remove that line from the code above - I can't edit it for some reason). sorry, always doing things like that.

if you don't even want the option to move stuff, then change this section:
Code:

        display alert "Path Length Warning" message "This transfer should work correctly.  Move files to server?" as informational buttons {"Quit", "Move Files"} default button 2
        if button returned of the result is "Move Files" then
                move theItems to disk mountedDiskName
        end if

to read:
Code:

        display alert "Path Length Warning" message "This transfer should work correctly." as informational
I just thought it would be a convenience. :)

jeremyoliver 05-27-2009 02:27 PM

I altered both sections of the code you mentioned, however I get exactly the same messages except for the move files option. :S

tw 05-27-2009 02:34 PM

is this machine running Tiger or Leopard?

tw 05-27-2009 02:47 PM

ah, never mind. change this bit of code (about 9 lines from the bottom):
Code:

tell application "System Events"
        set theItems to (get every disk item of folder theItem whose visible is true)
end tell
repeat with thisItem in theItems
        iterate(thisItem as alias as text, baseFolderCount)
end repeat

into this:
Code:

tell application "System Events"
        set theItems to (get every disk item of folder theItem whose visible is true)
        repeat with thisItem in theItems
                set thisItemPath to path of thisItem
                my iterate(thisItemPath, baseFolderCount)
        end repeat
end tell

system events is a picky sucker sometimes.

jeremyoliver 05-27-2009 04:18 PM

sweeeeet. Works perfect. thanks a lot tw.


All times are GMT -5. The time now is 07:26 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.