Saving a photo with extension

Saving a photo with extension

Postby Silvio.Falconi » Wed Sep 18, 2024 4:02 pm

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 view

            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 view

           while FRename( cfotografia, cFileNew ) != 0
              SysWait()
           end


to save I use the copyfile function (found in fwh sample)

Code: Select all  Expand view
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 view
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
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Postby Otto » Wed Sep 18, 2024 9:04 pm

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 view
// 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
Otto
 
Posts: 6312
Joined: Fri Oct 07, 2005 7:07 pm

Re: Saving a photo with extension

Postby Silvio.Falconi » Thu Sep 19, 2024 9:07 am

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
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Postby Otto » Thu Sep 19, 2024 9:31 am

Does it work?
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6312
Joined: Fri Oct 07, 2005 7:07 pm

Re: Saving a photo with extension

Postby Silvio.Falconi » Thu Sep 19, 2024 10:52 am

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
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Postby nageswaragunupudi » Sat Sep 21, 2024 3:08 am

Code: Select all  Expand view
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
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Saving a photo with extension

Postby Silvio.Falconi » Sun Sep 22, 2024 10:13 am

nageswaragunupudi wrote:
Code: Select all  Expand view
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
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Saving a photo with extension

Postby nageswaragunupudi » Sun Sep 22, 2024 5:12 pm

btw where is this function UrlImage() ?
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Saving a photo with extension

Postby Silvio.Falconi » Sun Sep 22, 2024 8:16 pm

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
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 52 guests