Updated:
We're under the assumption that "clip-2009-10-01 21;26;00.mov" is not the actual filename; one possibility is that the actual filename is "clip-2009-10-01 21:26:00.mov". However, we can't verify that under Windows.
We may not need to.
Failsafe Method:
Boot to a Linux LiveCD. Ubuntu 9.04 has good NTFS support, and Linux handles a lot more wonky-characters-in-filenames than Windows. The perl rename script may be included as the system's rename command.
This-Might-Actually-Work Batch Method (New Script!)
The DOS command DIR/X shows short filenames, if they exist on your system.
$ cmd
c:\test> dir /x
Volume in drive E is NUVOL
Volume Serial Number is 80D3-A96D
Directory of e:\tor\test
10/04/2009 05:15 AM <DIR> .
10/04/2009 05:15 AM <DIR> ..
10/04/2009 05:11 AM 0 CLIP-2~1.MOV clip-2009-10-01 21;26;00.mov
1 File(s) 0 bytes
2 Dir(s) 5,201,670,144 bytes free
If they do exist, the REN command will move them to a new name; the new name can be a new (valid) long filename.
c:\test> ren CLIP-2~1.MOV "clip-2009-10-01_21-26-00.mov"
That's how to fix one.
To batch process all of them, you need to 1) grab a listing of all the files you want to move; 2) run a short perl script to convert your listing into a batch file with the appropriate REN commands; and 3) run the resulting batch script.
c:\test> dir /x > mybrokenfiles.lst
$ cat mybrokenfiles.lst | perl -lne 'next if not /MOV/; s/^.{1,39}//; s/^/ren /; s/ (\d\d);(\d\d);(\d\d)/_$1-$2-$3/; print' > fixmybrokenfiles.bat
c:\test> fixmybrokenfiles.bat
The perl commandline assumes a very particular input format, so if the DOS listing shows long filenames in something other than the "21;26;00.mov" format, it probably won't do exactly what you want. If you try it, double-check that the batch script looks right before running it.
If you are comfortable with perl (or sed/awk, python, whatever), you can script this yourself. But if DIR/X doesn't show the short filenames, your system has them disabled, and this solution won't help.
Original answer: not useful with what we know now, but if you copy this sort of file off of OSX again, you can use this BEFORE the copy as a preventative step.
I use the commandline a lot on both Windows and Linux systems. There's a handy perl script floating around the internet that allows batch file renames using standard perl regex's (google for rename.pl to find it).
Under Cygwin on windows, use this in the directory your files are located in to rename them:
$ ls
clip-2009-10-01 21;26;00.mov
$ rename.pl 'tr/ ;/_-/;' *
$ ls
clip-2009-10-01_21-26-00.mov
Pretty sure my version came from the Perl Cookbook:
#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
Edit: To answer the clarified question:
If you can ensure that original
is always the same, and is only in the path once, use:
@Echo Off
Set "Find=original"
Set "Replace=replaced"
Set "OldPath=%~1"
Call Set "NewPath=%%OldPath:\%Find%\=\%Replace%\%%"
Echo %NewPath%
This will replace the first instance of \original\
with \replaced\
. Testing:
C:\> ChangePath.bat "Alice\original\Clive"
Alice\replaced\Clive
C:\> ChangePath.bat "Alice\original\Clive\Denver"
Alice\replaced\Clive\Denver
C:\> ChangePath.bat "Alice\Bob\original\Clive"
Alice\Bob\replaced\Clive
Previous answer
To change the second section of the path, you can use:
@Echo Off
Set "Replace=Replacement Path"
Set "PathABC=%~1"
Set "PathBC=%PathABC:*\=%"
Call Set "PathA=%%PathABC:\%PathBC%=%%"
Set "PathC=%PathBC:*\=%"
Set "NewPath=%PathA%\%Replace%\%PathC%"
Echo %NewPath%
Testing:
C:\> ChangePath.bat "Alice\Bob\Clive"
Alice\Replacement Path\Clive
This relies on there being no leading slash. It replaces the text between the first and second slash.
Edit: To explain what %%
means.
%%
is a method to escape the percentage sign. If I wrote the following line it would treat % Green 50%
as a variable. Since that variable is undefined, it will write the following output.
C:\> Echo Red 90% Green 50% Blue 5%
Red 90 Blue 5%
What I need to write is:
C:\> Echo Red 90%% Green 50%% Blue 5%%
Red 90% Green 50% Blue 5%
The following line goes through a few transformations. Here is each step of its transformation.
:: Original line
Call Set "NewPath=%%OldPath:\%Find%\=\%Replace%\%%"
:: The variables encased in single `%` are evaluated:
Call Set "NewPath=%%OldPath:\original\=\replaced\%%"
:: `Call` runs the rest of the line as a command. The `%%` are evaluated to `%`.
Set "NewPath=%OldPath:\original\=\replaced\%"
:: The search and replace on `OldPath` occurs.
Set "NewPath=Alice\replaced\Clive"
:: The final command is processed.
Best Answer
If you can use Bash, then the following script should do what you want:
It tries to replace filename with new string, if resulted filename doesn't match original, then it renames the file to resulted filename.
Usage, if saved as
replacestr
and given mode to be executed:It will try to rename all. You can use
[[ ! -f $f ]]
to skip non-file. You can also wrap it in function and source it from your~/.bashrc
if you need this very often.