ExifTool Forum

ExifTool => Newbies => Topic started by: brandon on March 17, 2017, 04:08:39 PM

Title: Undo Rename
Post by: brandon on March 17, 2017, 04:08:39 PM
Hello.  I just renamed some pictures using a cmd string I had used before (see below). For some reason, this time it changed the file name extension and now the files appear to be unusable. Is there any way to undo the naming and restore the files to a usable state? Thanks

exiftool -d %Y-%m-%d" "%H.%M.%S "-filename<${datetimeoriginal;}".":${subsectimeoriginal;$_.=0 x(3-length)}.%e"  -ext jpg .
Title: Re: Undo Rename
Post by: StarGeek on March 17, 2017, 04:54:51 PM
Unfortunately, you can't undo a rename unless you specifically save the filename beforehand.

Luckily, it's easy to fix the lack of extension.
exiftool -ext * "-filename<%f.$fileTypeExtension" DIR
Title: Re: Undo Rename
Post by: StarGeek on March 17, 2017, 05:43:05 PM
Is this on Windows?  You have a colon in your filename which is an illegal character for Windows.  Everything from that point on is lost.
Title: Re: Undo Rename
Post by: StarGeek on March 17, 2017, 05:49:36 PM
Upon further testing, the problem is more serious than I previously though.  Even though the file shows up in the directory, Windows is unable to access it, through gui and command line.  Even using wildcards doesn't help.

Edit: I just noticed that the filesize is 0 bytes.  Running CHKDSK didn't help, I'm unable to remove the file.
Title: Re: Undo Rename
Post by: brandon on March 17, 2017, 06:04:13 PM
Ok. Yeah. So looks like there is nothing I can do to recover the files, right?
Title: Re: Undo Rename
Post by: StarGeek on March 17, 2017, 06:16:22 PM
Yeah, I think the file is gone.  I figured out how to remove the file, but couldn't figure out a way to get any data out of it, since windows doesn't think there is any data there. 

You might try a file recovery program, such as Recuva (https://www.piriform.com/recuva), but I wouldn't have much hope for that.

If you want to remove the empty files, you can use the command line and delete them using their 8dot3 filenames.  See this video (https://www.youtube.com/watch?v=89wlEfRqa6U).  The tl;dw version is
dir /x directory to get the 8.3 filename
del 8.3filename
Title: Re: Undo Rename
Post by: brandon on March 17, 2017, 07:14:27 PM
Ok. Thanks for the help. Are you able to tell what the exif tool command I used did to the file? As you state, windows thinks the file doesn't exist anymore.
Title: Re: Undo Rename
Post by: StarGeek on March 17, 2017, 08:23:46 PM
The command you posted has a colon : in it right before subsectimeoriginal.  The colon is an illegal character if it is anywhere except at the beginning for the drive designation.

Actually, wait.  Your files might be recoverable.  It looks like the colon treated your file as an NTFS Stream.  Let me test...

edit  Nope, I was wrong, sorry.
I tried dir /R directory to see what files had ADS (Alternate Data Stream) and the file didn't have one.  I then tried ADS Manager (http://dmitrybrant.com/adsmanager) and it couldn't find anything either.
Title: Re: Undo Rename
Post by: brandon on March 17, 2017, 10:11:12 PM
Ok.  Thanks for the help and thanks for trying.
Title: Re: Undo Rename
Post by: Phil Harvey on March 18, 2017, 09:17:35 AM
I don't know why this happens, but I have seen it before.  From this page (https://exiftool.org/filename.html):

Writing illegal file names in Windows can have unpredictable results and may result in data loss.

If someone can come up with a filter algorithm that catches filenames that are illegal in Windows, I can apply a Windows-specific check to ExifTool.

- Phil

Edit:  This page gives some useful information (https://msdn.microsoft.com/en-us/library/aa365247.aspx) -- I'll study this in detail when I get a chance.
Title: Re: Undo Rename
Post by: Phil Harvey on March 18, 2017, 11:03:48 AM
OK, here is what I have so far.  I propose to add this code as a check before renaming any file in Windows:

    if ($^O eq 'MSWin32') {
        if ($newName =~ /[\0-\x1f<>"|*]/) {
            $self->Warn('New file name not allowed in Windows (contains reserved characters)');
            return -1;
        }
        if ($newName =~ /:/ and $newName !~ /^[A-Z]:[^:]*$/i) {
            $self->Warn("New file name not allowed in Windows (contains ':')");
            return -1;
        }
        if ($newName =~ /\?/ and $newName !~ m{^[\\/]{2}\?[\\/][^?]*$}) {
            $self->Warn("New file name not allowed in Windows (contains '?')");
            return -1;
        }
        if ($newName =~ m{(^|[\\/])(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.[^.]*)?$}i) {
            $self->Warn('New file name not allowed in Windows (reserved device name)');
            return -1;
        }
        if ($newName =~ /([. ])$/) {
            $self->Warn("New file name not recommended for Windows (ends with '$1')", 2) and return -1;
        }
        if (length $newName > 259 and $newName !~ /\?/) {
            $self->Warn('New file name not recommended for Windows (exceeds 260 chars)', 2) and return -1;
        }
    }


The last two warnings are minor, and may be ignored with the -m option.

If anyone has any comments about this I would be happy to hear them.

- Phil

Edit: Updated code to reflect what was released in ExifTool 10.47
Title: Re: Undo Rename
Post by: Phil Harvey on March 18, 2017, 07:45:26 PM
More work to do.  Apparently a path starting with "\\?\" is legal -- I'll have to add support for this.

- Phil
Title: Re: Undo Rename
Post by: Stephen Marsh on March 18, 2017, 09:38:49 PM
I'm sorry to hear of your problem brandon. I know that this does not help you, however for any others out there that are new to ExifTool (or any other software for that matter)... Work on copies of the original files while testing, even more so for batches (even if using code that previously worked).


If you wish to copy the original filename before renaming files, I would use the Adobe Bridge tag that is supported by ExifTool:


Add/Copy filename to Adobe Preserve Original Filename (including file extension) metadata entry:

     exiftool -r '-XMP-xmpMM:PreservedFileName<${filename}' 'DIR-or-FILE'


Add/Copy filename to Adobe Preserve Original Filename (sans file extension) metadata entry:

     exiftool -r '-XMP-xmpMM:PreservedFileName<${filename;s/\.[^.]*$//}' 'DIR-or-FILE'


Rename/restore files using their preserved filename metadata entry:

     exiftool -r '-FileName<XMP-xmpMM:PreservedFileName' 'DIR-or-FILE'


(The above commands are formatted for the Mac OS, use double straight quotes " for Windows)