Page 1 of 1

Saving a photo with extension

Posted: Wed Sep 18, 2024 4:02 pm
by Silvio.Falconi
I need to save a file in the archive with the extension ( I not Know wich is the extension of the file) ,
the user selects the file i.e. a photo,
the procedure must rename the photo with the student's matriculation number (nMatricola)

I made :

Code: Select all | Expand

            nMatricola:= oAlunno:matricola      // student's matriculation number

           cExt := ltrim(Right(cfotografia,4))  // the extension of file 

           cRelativePath:= RelativePath(cfotografia)    // give the folder of file loaded 

           cFileNew:=cRelativePath+alltrim(nMatricola)+cExt     //save the new name file with extension

         
to rename the file cfotografia into new file cFileNew I made

Code: Select all | Expand

           while FRename( cfotografia, cFileNew ) != 0
              SysWait()
           end
to save I use the copyfile function (found in fwh sample)

Code: Select all | Expand

AaDd(aSource,(cFileNew) )
          AaDD(aTarget,cImgPath+UrlImage(cFileNew))
          CopyFiles(aSource,aTarget,)
it happens that sometimes it works and sometimes it saves the file in the cImgPath folder without extension but only with the file name i.e. with the student's matriculation number (nmatricola) and then saves it in the oAlunno:foto field

Code: Select all | Expand

 oAlunno:foto:= UrlImage(cFileNew)
          oAlunno:Save()



the function RelativePath() - it gives me the name of the folder without the file
the function UrlImage() - it gives me the file name without the folder name


How I can resolve it ?

Re: Saving a photo with extension

Posted: Wed Sep 18, 2024 9:04 pm
by Otto
Silvio, although you can currently only ask 4.5 questions per day on o1, I will give up one for myself and test with your question.
Please post whether the result was correct.
Best regards,
Otto


ChatGPT preview o1
The issue you're encountering stems from the method used to extract the file extension. By using Right(cfotografia,4), you're assuming that the file extension is always three characters long (e.g., .jpg, .png), which may not be the case. Some file extensions are longer (like .jpeg, .tiff) or shorter. This incorrect assumption can lead to the extension being improperly extracted, resulting in files being saved without an extension.


Code: Select all | Expand

// Student's matriculation number
nMatricola := oAlunno:matricola

// Find the last dot in the file name to get the extension
nDotPos := RAT(".", cfotografia)
IF nDotPos > 0
   cExt := SUBSTR(cfotografia, nDotPos)  // Extract extension
ELSE
   cExt := ""  // No extension found
ENDIF

// Get the folder path
cRelativePath := RelativePath(cfotografia)

// Construct the new file name
cFileNew := cRelativePath + ALLTRIM(nMatricola) + cExt

// Rename the original file
WHILE FRename(cfotografia, cFileNew) != 0
   SysWait()
END

// Prepare for copying
AaDd(aSource, cFileNew)
AaDD(aTarget, cImgPath + UrlImage(cFileNew))

// Copy the file
CopyFiles(aSource, aTarget)

// Update the student's record
oAlunno:foto := UrlImage(cFileNew)
oAlunno:Save()
 

Re: Saving a photo with extension

Posted: Thu Sep 19, 2024 9:07 am
by Silvio.Falconi
nice!

Re: Saving a photo with extension

Posted: Thu Sep 19, 2024 9:31 am
by Otto
Does it work?

Re: Saving a photo with extension

Posted: Thu Sep 19, 2024 10:52 am
by Silvio.Falconi
Otto wrote:Does it work?
it seems ok

Re: Saving a photo with extension

Posted: Sat Sep 21, 2024 3:08 am
by nageswaragunupudi

Code: Select all | Expand

cFileNew := StrTran( cfotografia, cFileNoExt( cfotografia ), AllTrim( oAlunno:matricola ) )
if FRename( cfotografia, cFileNew ) == 0
   oAlunno:foto := cFileNoPath(cFileNew)
   oAlunno:Save()
endif

Re: Saving a photo with extension

Posted: Sun Sep 22, 2024 10:13 am
by Silvio.Falconi
nageswaragunupudi wrote:

Code: Select all | Expand

cFileNew := StrTran( cfotografia, cFileNoExt( cfotografia ), AllTrim( oAlunno:matricola ) )
if FRename( cfotografia, cFileNew ) == 0
   oAlunno:foto := cFileNoPath(cFileNew)
   oAlunno:Save()
endif

thanks

Re: Saving a photo with extension

Posted: Sun Sep 22, 2024 5:12 pm
by nageswaragunupudi
btw where is this function UrlImage() ?

Re: Saving a photo with extension

Posted: Sun Sep 22, 2024 8:16 pm
by Silvio.Falconi
nageswaragunupudi wrote:btw where is this function UrlImage() ?
see you email