Saving a photo with extension

Post Reply
User avatar
Silvio.Falconi
Posts: 7104
Joined: Thu Oct 18, 2012 7:17 pm

Saving a photo with extension

Post 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 ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Otto
Posts: 6380
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: Saving a photo with extension

Post 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()
 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Silvio.Falconi
Posts: 7104
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Post by Silvio.Falconi »

nice!
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Otto
Posts: 6380
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: Saving a photo with extension

Post by Otto »

Does it work?
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Silvio.Falconi
Posts: 7104
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Post by Silvio.Falconi »

Otto wrote:Does it work?
it seems ok
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Saving a photo with extension

Post 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
Regards

G. N. Rao.
Hyderabad, India
User avatar
Silvio.Falconi
Posts: 7104
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Post 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
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Saving a photo with extension

Post by nageswaragunupudi »

btw where is this function UrlImage() ?
Regards

G. N. Rao.
Hyderabad, India
User avatar
Silvio.Falconi
Posts: 7104
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Post by Silvio.Falconi »

nageswaragunupudi wrote:btw where is this function UrlImage() ?
see you email
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Post Reply