Recent

Author Topic: copyfile routine containing apostrophes  (Read 20660 times)

cazzajay

  • Jr. Member
  • **
  • Posts: 94
copyfile routine containing apostrophes
« on: May 11, 2011, 10:05:11 am »
another query - thought id put it in a new topic in case anyone else does a search in the future for this:

Code: [Select]
var
fullpathsrc: string;
fullpathdest: string;
begin
copyfile(pchar(fullpathsrc), pchar(fullpathdest), false)
end;

if either fullpathsrc or fullpathdest contain an apostrophe above the procedure fails to copy the file.  I'm guessing this is because in pascal strings are terminated by an apostrophe, but is there a way around it - since Windows allows the use of an apostrophe in a filename?

One counter to my above assertion is that if you do a showmessage(fullpathdest) it shows the whole string not just up to the first apostrophe as I would have expected.  Leading me to think it could also be to do with the PChar conversion?

I have done my own research - but nobody else seems to have had this problem. Loads of articles explain how to use CopyFile (eg http://www.swissdelphicenter.ch/torry/showcode.php?id=101) but none seem to explain how to get around this?

Thanks again for any help  :)
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: copyfile routine containing apostrophes
« Reply #1 on: May 11, 2011, 10:37:31 am »
How about scanning the strings and duplicating every single quote you encounter.  AFAIK two single quotes in a string in Pascal is interpreted as one.

1.0/2.6.0  XP SP3 & OS X 10.6.8

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: copyfile routine containing apostrophes
« Reply #2 on: May 11, 2011, 11:10:06 am »
Maybe the underlying OS call interprets an apostroph?

What happens if you place quotes (") around the filename?

Which OS are you using?

Bart

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #3 on: May 11, 2011, 12:06:56 pm »
How about scanning the strings and duplicating every single quote you encounter.  AFAIK two single quotes in a string in Pascal is interpreted as one.



i thought about that but cant really think how to do it. this obviously doesnt work because of the odd number of apostrophes:

Code: [Select]
fullpathdest := stringreplace(fullpathdest, ''', '''', [rfReplaceAll]);
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #4 on: May 11, 2011, 12:08:17 pm »
Maybe the underlying OS call interprets an apostroph?

What happens if you place quotes (") around the filename?

Which OS are you using?

Bart

os=windows
placing quotes doesnt affect anything. i used the code below to do it
Code: [Select]
fullpathdest := '"'+fullpathdest+'"'
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: copyfile routine containing apostrophes
« Reply #5 on: May 11, 2011, 12:27:14 pm »
Windows doesn't allow " in filenames. It's used in terminals to indicate a filename with space characters in it as a single path, but that doesn't make sense in a programming language where strings are a known length

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: copyfile routine containing apostrophes
« Reply #6 on: May 11, 2011, 01:11:36 pm »
I guess when doing a replace you will have to do the same duplication of single quotes:

Code: [Select]
fullpathdest := stringreplace(fullpathdest, '''', '''''', [rfReplaceAll]);
1.0/2.6.0  XP SP3 & OS X 10.6.8

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #7 on: May 11, 2011, 01:56:54 pm »
I guess when doing a replace you will have to do the same duplication of single quotes:

Code: [Select]
fullpathdest := stringreplace(fullpathdest, '''', '''''', [rfReplaceAll]);

again, that doesn't work. I'm wondering if I could use an ascii reference. And even so, is the apostrophe the issue? Its not an issue when the variable is referenced in a showmessage, only in a copyfile (via Pchar conversion)
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: copyfile routine containing apostrophes
« Reply #8 on: May 11, 2011, 02:08:47 pm »
Have you tried doing the file copy with a TFileStream?  I don't know if it will work, but may be worth a try.
1.0/2.6.0  XP SP3 & OS X 10.6.8

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #9 on: May 11, 2011, 02:14:12 pm »
Have you tried doing the file copy with a TFileStream?  I don't know if it will work, but may be worth a try.
ok ive put a new procedure as follows:

Code: [Select]
Procedure TSFileCopy(sourcefilename, targetfilename: String);
Var
 S, T: TFileStream;
Begin
 S := TFileStream.Create( sourcefilename, fmOpenRead );
 try
   T := TFileStream.Create( targetfilename,
                            fmOpenWrite or fmCreate );
   try
     T.CopyFrom(S, S.Size ) ;
   finally
     T.Free;
   end;
 finally
   S.Free;
 end;
End; 

and then call this instead of copyfile

Code: [Select]
TSfilecopy(fullpathsrc, fullpathdest)

it works fine for lower ascii files, but it gives me an exception stating that it "cannot read from file" if the filename contains upper ascii chars (its a song by singer bjork - the file is called "Björk - Possibly Maybe.mp3". 

However, this song could be copied using the old copyfile.

so im guessing this may not work etiher.

btw- obtained code from http://www.awitness.org/delphi_pascal_tutorial/code_samples/file_copy_delphi.html
« Last Edit: May 11, 2011, 02:30:21 pm by cazzajay »
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: copyfile routine containing apostrophes
« Reply #10 on: May 11, 2011, 02:33:11 pm »
os=windows
placing quotes doesnt affect anything. i used the code below to do it
Code: [Select]
fullpathdest := '"'+fullpathdest+'"'

Back to the original question: Windows has no problem whatsoever to handle filenames with apostrophes ('single quotes').
You'd better check the actual contents of the 2 variables you use before executing the file copy.

Another question: why do you typecast the filenames to PChar?
Copyfile accepts normal string parms.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #11 on: May 11, 2011, 02:37:52 pm »
Back to the original question: Windows has no problem whatsoever to handle filenames with apostrophes ('single quotes').
You'd better check the actual contents of the 2 variables you use before executing the file copy.
I have, the strings are fine and true with the apostrophes intact. I verified this using a showmessage.  The issue is when this is passed to copyfile, it falls over.

Another question: why do you typecast the filenames to PChar?
Copyfile accepts normal string parms.
i must be doing something wrong?. see attached screenshot of v0.9.30

thanks all for your help and time with this!
« Last Edit: May 11, 2011, 02:39:54 pm by cazzajay »
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: copyfile routine containing apostrophes
« Reply #12 on: May 11, 2011, 02:43:00 pm »
Try the CopyFile function in the FileUtil unit.

BTW: were you using JwaWinBase.FileCopy?
If so, that function is not UTF8 compliant.
Before calling that function you should translate the source and destination name to ansi strings and then it does work (even with accented characters).
« Last Edit: May 11, 2011, 02:57:53 pm by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #13 on: May 11, 2011, 02:59:18 pm »
Try the CopyFile function in the FileUtil unit.

BTW: were you using JwaWinBase.FileCopy?
If so, that function is not UTF8 compliant.
Before calling that function you should translate the source and destination name to ansi strings and then it does work (even with accented characters).

omg that worked a charm! super thanks!!!

i was using the windows unit - not sure if thats the same as jwawinbase? anyway with fileutil it works a treat!  :D :D :D
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: copyfile routine containing apostrophes
« Reply #14 on: May 11, 2011, 03:39:00 pm »
hmm - just noticed that FileExists does not work with these accented chars, or apostrophes, too.  now i am using only fileutils.

in the following:

Code: [Select]
var
fullpathsrc: string;
begin
fullpathsrc := 'Björk - Possibly Maybe.mp3';
if fileexists('C:\music\' + fullpathsrc) = false then showmessage('File Does Not Exist');
end;

will return false and show the message even if the file does exist in the location.  But only exhibits this behaviour for the songs with upper ascii in the name, or an apostrophe.
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

 

TinyPortal © 2005-2018