Script to Remove Trailing Spaces on File Server

Mac users have created numerous folders and files with leading and trailing spaces.  Does anyone know of a batch or VB script to recursively locate and remove the illegal spaces?

Example folder: "12 "
Example folder: " stuff"

The following works, but I'm hoping there's a better solution.
ren "\\?\path-to-illegal-folder" newname
ren "\\?\path-to-illegal-file" newname
dantali0nAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dantali0nAuthor Commented:
Tried NameMunger and an applescript with no success.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

nemws1Database AdministratorCommented:
I use a perl script for this sort of thing all the time - I call it "rename" and it lets you use regexs on the command line to rename files:
#!/usr/bin/perl
# Usage: rename perlexpr [files]

($op = shift) || die "Usage: rename perlexpr [filenames]\n";
if (!@ARGV) {
    @ARGV = <STDIN>;
    chop(@ARGV);
}
for (@ARGV) {
    $was = $_;
    eval $op;
    next if (-e $_);  # don't clobber existing files
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}

Open in new window

To remove leading/trailing spaces, run with:
rename.pl 's/(^\s+)|(\s+$)//' *

Open in new window

This will rename files or directories and get rid of any additional spaces.
0
dantali0nAuthor Commented:
Thanks for the script nemws1!  I'm new to Perl, so I'm wondering what I need to do to adjust the script to run on a win7 box, and to hit remote file shares (\\server\sharename).

Here's what I'm seeing:

C:\perl>perl -v
This is perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x86-multi-thread

C:\perl>rename.pl 's/(^\s+)|(\s+$)//' *
//' was unexpected at this time.

C:\perl>rename.pl 's/(^\s+)|(\s+$)//' "c:\testing"
//' was unexpected at this time.

Open in new window

0
nemws1Database AdministratorCommented:
Sorry for the delay.  Hmm..  You're using the right quotes and slashes.  I just tried this on my machine and it worked just fine, but I'm running Cygwin and that might set up a better environment.

Did you try double-quotes?  Also, the first command you tried is correct, the second is not.  You should pass 'rename.pl' a list of filenames (not a directory - it's not smart enough to handle that).

Try this:
rename.pl "s/(^\s+)|(\s+$)//" *.*

Open in new window

If that fails, try this:
rename.pl "s/(^ +)|( +$)//" *.*

Open in new window

0
dantali0nAuthor Commented:
I successfully ran the script but it didn't remove leading and trailing spaces on folders.  Regardless, thanks for the help nemws1, you're great.
0
dantali0nAuthor Commented:
This issue was caused by Mac OS 10.4 users, so I built a 10.4 box and used it to remove the spaces.
0
nemws1Database AdministratorCommented:
That was going to be my next suggestion - run the scripts from a Mac. Glad it worked out. :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dantali0nAuthor Commented:
More specifically, I built a OS 10.4 Tiger VM and used it to remove the spaces.  
0
dantali0nAuthor Commented:
FYI: 10.5, 10.6 and 10.7 were not able to remove the spaces.  Only 10.4 worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.