Necos has asked for the wisdom of the Perl Monks concerning the following question:

I haven't written a root node in a while, so here goes.

Here, at my job, we use Office 2000, as well as Office XP. That's all fine and dandy. However, Word has this habit of saving unnmaed files (Document1, etc.) with names that are gathered from the first line of a long paragraph (for example: 'This is a very long paragraph that I am going to write, just as a proof of concept'). Again, that's all fine and dandy, even if the users save their data on our fileservers.

The problem comes in when I run backups at night. The backup program I use, named Backup Plus (very simple data backup utility), uses Zip compression to archive the data. If it runs into a file that has a name that's longer than 64 characters (I believe that's the limit of the NTFS file system in NTFS 4.0), the program barfs. Thus, my backups fail. This can be hazardous to my health if someone deletes their work and needs it restored.

I thought about renaming the files before the backups run. However, I'm not sure about how to rename files from within a Perl program.

I'm also kind of stuck on how to rename a binary file in Windows, considering that I wouldn't know whether or not a file is binary or not during runtime (I have to process several thousand files). I was thinking Win32::OLE would be of use here, but, then again, I don't know.

Theodore Charles III
Network Administrator
Los Angeles Senior High
4650 W. Olympic Blvd.
Los Angeles, CA 90019
323-937-3210 ext. 224
email->secon_kun@hotmail.com
perl -e "map{print++$_}split//,Mdbnr;"

Replies are listed 'Best First'.
Re: The Office suite and long file names
by graff (Chancellor) on Oct 24, 2002 at 00:30 UTC
    Well, you might consider a different tool for doing backups... But maybe there is a simpler method where perl can help. I'm not a windows officianado, but here are some ideas.

    First, I don't see why the text/binary file distinction should have anything to do with assigning file names. All you should need to worry about is preserving the original file extension, since this always matters for windows apps, and if you happen to shorten any names, make sure you keep track of the original and what it was shortened to, in case users think their original names had some importance.

    Maybe what you want is a two-stage backup process, which would be easy to script sensibly in perl:

    1. for a specified list of directories, create a corresponding set of zip files using a recent version of a good zip utility; make sure that easy names are assigned to the zip files, and make sure that internal subdirectory structure and file names are preserved.
    2. use your existing backup utility to backup the zip files.

    You would probably want a nice tabulation or database as a side-product, so when some poor shmuck loses his file, you can figure out which zip file contains it.

Re: The Office suite and long file names
by BrowserUk (Patriarch) on Oct 24, 2002 at 00:44 UTC

    Have a look at perlfunc:rename. It seems to work for any long filename I tried (Upto 250+chars) under NT4 and NTFS from AS 5.6.1.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: The Office suite and long file names
by particle (Vicar) on Oct 24, 2002 at 00:54 UTC

    to add to the existing answers... consult the docs

    >perldoc -q rename a file
    Found in perlfaq5.pod
      How can I reliably rename a file?
    
                Well, usually you just use Perl's rename() function. That may
                not work everywhere, though, particularly when renaming files
                across file systems. Some sub-Unix systems have broken ports
                that corrupt the semantics of rename()--for example, WinNT does
                this right, but Win95 and Win98 are broken. (The last two parts
                are not surprising, but the first is. :-)
    
                If your operating system supports a proper mv(1) program or its
                moral equivalent, this works:
    
                    rename($old, $new) or system("mv", $old, $new);
    
                It may be more compelling to use the File::Copy module instead.
                You just copy to the new file to the new name (checking return
                values), then delete the old one. This isn't really the same
                semantically as a real rename(), though, which preserves
                metainformation like permissions, timestamps, inode info, etc.
    
                Newer versions of File::Copy exports a move() function.
    

    ~Particle *accelerates*

Re: The Office suite and long file names
by seattlejohn (Deacon) on Oct 24, 2002 at 07:32 UTC
    Others have already answered your specific question, but as a general reminder... When you don't know how to do it in Perl, but you do know how to do it at your DOS prompt (or equivalent), it's useful to know that backticks execute a system/shell command: my $result = `ren $original_filename $new_filename`; $result contains any output from the ren command (which is silent if it succeeds), and $? contains the numeric exit code from the command.

    The command in the backticks isn't portable, of course, but since you're running into a Windows-specific problem that's unlikely to be a concern here.

            $perlmonks{seattlejohn} = 'John Clyman';