#1 2021-06-26 17:55

PacSap
Member
Registered: 2021-06-26
Posts: 3

Spaces and Special chars on a folder

Hi all,

I'm using the ReNamer tool since some time now and I'm also using some scripts I found in the forum and try to adapt them to my needs.
Recently I changed my laptop and my script stop working to certain folders (took me a while to understand that)

It's also important to say that I'm not a programmer expert so please be gentle smile tongue

I'm using exiv2.exe and exiftool.exe files in my script and the reasons for these are
exiv2.exe
-> seems faster to get the date from the file
-> only gets few date fields from the file
-> seems to work only with image files
-> faster to rename a lot of files

exiftool.exe
-> seems slower to get the date from the file
-> gets more date fields from the file
-> works with all the files
-> heavier to rename a lot of files

at least this is my point of view...

The goal of this script is to rename any file in the folder (I just drag and drop the folder inside ReNamer and the magic happens big_smile ).
I try to use exiv2.exe to the images and the exiftool.exe to the other type of files.
In case there is an error, then FileTimeModified should be used.

The issue I've found recently is related with the path of the file.
If I do a showmessage for the file in a folder on my desktop, I get the following and the command is executed:

"C:\Program Files (x86)\ReNamer\exiv2.exe" "C:\Users\pauca\Desktop\Teste\DSC_4128.JPG"

If I do a showmessage for the same file in the original folder on my desktop, I get the following and the command is not executed:

"C:\Program Files (x86)\ReNamer\exiv2.exe" "C:\Users\pauca\Desktop\Várias fotos a rever\tlm\DSC_4128.JPG"

or

"C:\Program Files (x86)\ReNamer\exiftool.exe" -d "%Y.%m.%d" -CreateDate "C:\Users\pauca\Desktop\teste\DSC_4128.JPG"

And it's this second command that does not work to the exiv2.exe and exiftool.exe.
Pay attention that there is a word with a special char, the 'á' as also there are spaces in a folder
If I test all the 3 commands through a command line, all of them work but seems that the application interprets these commands in a different way.

At the begin I though it was related with my regional settings of the laptop. So I have set the keyboard to English and the application to English but even so it's not working.

So my question is, does anyone knows why below script does not work with folders that may contain spaces and/or special chars on it?

Here it is the script which I'm aware it's not optimized but as I said, I'm not a programmer.


//http://www.den4b.com/forum/viewtopic.php?id=349&p=3

const
  //EXE = 'exiftool.exe  -d "%Y-%m-%d %H%M%S" -CreateDate ';
  //https://sno.phy.queensu.ca/~phil/exiftool/
  EXE_EXIFTOOL = 'exiftool.exe" -d "%Y.%m.%d" -CreateDate "';
  EXE_EXIV = 'exiv2.exe';

  //Find a line in the output, starting with "CreationDate".
  TAG_EXIFTOOL = 'Create Date\s+:\s+(.+)';
  //TAG_EXIFTOOL = 'File Modification Date/Time\s+:\s+(.+)';
  TAG_EXIV = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
  //	Matches are count from the left:  (0)
  //	Use that parts to compose the wanted NewName: Matches[0]

  //Extension of the images
  //DO NOT ADD .png
  SUB_EXT_IMG = '.jpg, .jpeg';//, .bmp, .gif, .tiff';

var
  FileExt, Command, Output: String;
  Matches, DateEXIF: TWideStringArray;
  DateTime: TDateTime;

begin
  //Get file extension
  FileExt := WideExtractFileExt(FileName);

  //showmessage('FileExt: ' + FileExt + chr(13) + chr(10) + 'FileExt found: ' + +IntToStr(pos(FileExt, SUB_EXT_IMG)));
  if pos(FileExt, SUB_EXT_IMG) > 0 then
    //It's an image file
    begin
      Command := '"'+WideGetCurrentDir+'\'+EXE_EXIV+'"'+' "'+FilePath+'"';
      //showmessage('Command: ' + Command);
      //showmessage('ExecConsoleApp: '+IntToStr(ExecConsoleApp(Command, Output)));
      if ExecConsoleApp(Command, Output) = 0 then
		    begin
          Output:= trim(Output);
          //showmessage('1 - Output: __'+Output+'__');
			    Matches := SubMatchesRegEx(Output, TAG_EXIV, False);
          //showmessage('Length Matches: '+IntToStr(Length(Matches)));
			    DateEXIF := SubMatchesRegEx(Matches[0], '(.+)\:(.+)\:(.+)\ (.+)\:(.+)\:(.+)', False);
          if DateEXIF[0] <> '0000' then
            begin
              if Length(DateEXIF) > 0 then
			        FileName := DateEXIF[0] + '.' + DateEXIF[1] + '.' + DateEXIF[2]
                //+ '__' + DateEXIF[3] + '.' + DateEXIF[4] + '.' + DateEXIF[5]
                + ' -' + WideExtractFileExt(FileName);
            end
          else
            begin
              //showmessage('1 - Something went wrong!!' + Command);
              DateTime := FileTimeModified(FilePath);
	      //FileName := WideExtractBaseName(FileName) +
		FileName := FormatDateTime('yyyy.mm.dd -', DateTime) + WideExtractFileExt(FileName);
		end;
	end;
    end
  //not an image file
  else
    begin
      //Command := '"'+WideGetCurrentDir+'\'+EXE_EXIFTOOL+'"'+' -d "%Y.%m.%d" -CreateDate "'+FilePath+'"';
      Command := '"'+WideGetCurrentDir+'\'+EXE_EXIFTOOL+FilePath+'"';
      //showmessage('Command: ' + Command);
      //showmessage('ExecConsoleApp: '+IntToStr(ExecConsoleApp(Command, Output)));
      if ExecConsoleApp(Command, Output) = 0 then
        begin
          Output:= trim(Output);
          //showmessage('2 - Output: __'+Output+'__');
          Matches := SubMatchesRegEx(Output, TAG_EXIFTOOL, False);
          //showmessage('Length Matches: '+IntToStr(Length(Matches)));
          if Length(Matches) = 1 then
            begin
              //showmessage('Matches: '+ Matches[0]);
              FileName := Matches[0] + ' -' + WideExtractFileExt(FileName);
            end
          else
            begin
              //showmessage('CreateDate not found in the file!!' + Command);
		          DateTime := FileTimeModified(FilePath);
		          //FileName := WideExtractBaseName(FileName) +
		          FileName := FormatDateTime('yyyy.mm.dd -', DateTime) + WideExtractFileExt(FileName);
            end;
        end;
    end;
end.

Thanks,
Paulo

Last edited by PacSap (2021-06-29 19:08)

Offline

#2 2021-06-26 21:23

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Spaces and Special chars on a folder

Hi and welcome!




Try changing

var
  FileExt, Command, Output: String;

to

var
  FileExt, Command, Output: WideString;



http://www.den4b.com/wiki/ReNamer:Pasca … ring_types


 


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#3 2021-06-26 21:26

PacSap
Member
Registered: 2021-06-26
Posts: 3

Re: Spaces and Special chars on a folder

When I enable the command showmessage('ExecConsoleApp: '+IntToStr(ExecConsoleApp(Command, Output))); for the command

"C:\Program Files (x86)\ReNamer\exiv2.exe" "C:\Users\pauca\Desktop\Várias fotos a rever\tlm\DSC_4128.JPG"

the result should be 0 instead the result is:

[Window Title]
ReNamer

[Content]
ExecConsoleApp: 255

[OK]

To test again the script, I've recreated the same structure on my old laptop and the script works well.

Old laptop settings:
    Win PRO in Portuguese with Portuguese Keyboard
    Renamer version 6.9
    exiv2.exe 0.25 001900 (32 bit build)
    Exiftool.exe version 11.9.9.0

New laptop settings:
    Win PRO in English with Swiss French Keyboard
    Renamer version 7.3
    exiv2.exe 0.25 001900 (32 bit build)
    Exiftool.exe version 12.2.8.0

I've copy the ReNamer folder from my old laptop to a USB, executed Renamer.exe from the USB and the issues disappear.
I've also copy the ReNamer folder from the USB to C:\Program Files (x86)\ReNamer and it works.

So it seems that the issue is on the new Renamer version...

Offline

#4 2021-06-26 21:33

PacSap
Member
Registered: 2021-06-26
Posts: 3

Re: Spaces and Special chars on a folder

Stefan wrote:

Hi and welcome!

Thank you smile

Stefan wrote:

Try changing

var
  FileExt, Command, Output: String;

to

var
  FileExt, Command, Output: WideString;

I have an error if I change that value:

[Window Title]
Error

[Content]
Pascal Script Compile:
[Line 36] Error: Type mismatch

[OK]

and the line concerned is if ExecConsoleApp(Command, Output) = 0 then

Last edited by PacSap (2021-06-26 21:35)

Offline

Board footer

Powered by FluxBB