How to create Auto-filenames ( Counter ) ?

How to create Auto-filenames ( Counter ) ?

Postby ukoenig » Fri Aug 05, 2011 5:35 pm

Hello,

is there a fast Solution, to create Autofilenames ( in NON-overwrite-mode )

Sample :
I start with Image1.jpg and a given new Start-filename ( save as ) _Image1.jpg
next created new Images ( from the Basic-image ) would be saved as => _Image1_1.jpg, _Image1_2.jpg .....

At new start and using the same Image and defined Output _Image1.jpg,
more changes of Image1.jpg must have Names : _Image1_3.jpg, _Image1_4.jpg .....

Maybe scanning the Directory with Wildcard to detect the last used Numer ?

scanning for :
_Image1_*.jpg

aDir := directory('.\Images\_Image1_*.jpg')
NewValue := Len( aDir ) + 1


Existing Files :
_Image1_1.jpg
_Image1_2.jpg
_Image1_3.jpg
_Image1_4.jpg


the next new Filname must be => _Image1_5.jpg

Any idea ?

Best Regards
Uwe :?:
Last edited by ukoenig on Fri Aug 05, 2011 6:14 pm, edited 1 time in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: How to create Auto-filenames ( Counter ) ?

Postby hmpaquito » Fri Aug 05, 2011 6:13 pm

Uko,

Whithout testing,

Code: Select all  Expand view

//--------------------------------------------------------------------------------------------//
// Return next filename image (without path)
// cPath. Optional
FUNCTION NextFileImage(cPath)
Local cImage, nImage:= 0
Local cLastImage

IF cPath == NIL
   cPath:= "."
ENDIF

#Define FILENAME_POS 1
#Define COUNTER_SEPARATOR "_"
#Define PATTERN_ "_Image1"+ COUNTER_SEPARATOR
#Define IMAGE_EXTENSION ".JPG"

DO WHILE .T.
   aDir:= Directory(cPath+ "\"+ PATTERN_+ "*"+ "IMAGE_EXTENSION)
   IF !Empty(aDir)
        aDir:= aSort(aDir, {|x, y| x[FILENAME_POS] < y[FILENAME_POS]})  // Ascending

        cLastImage:= Upper( aTail(aDir)[FILENAME_POS] )
        cLastImage:= Left(cLastImage, RAt(IMAGE_EXTENSION, cLastImage))              // No extension

        #Define RAT_COUNTER_SEPARATOR RAt(COUNTER_SEPARATOR, cLastImage)

        nImage:= Val(SubStr(cLastImage, RAT_COUNTER_SEPARATOR+ Len(COUNTER_SEPARATOR) ))
   ENDIF

   nImage++
   cImage:= PATTERN_+ LTrim(Str(nImage, 10, 0))+ IMAGE_EXTENSION

   IF File(cPath+ cImage)
       MsgInfo(cPath+ "\"+ cImage, "Errror !! File existing !!")
   ELSE
       EXIT
   ENDIF
ENDDO
RETURN cImage
 

Regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: How to create Auto-filenames ( Counter ) ?

Postby ukoenig » Fri Aug 05, 2011 11:36 pm

Thank You very much for Your help.
I changed some Lines :

Code: Select all  Expand view

// The Basic-File from File-selection => cOnlyName, cExtension
// -----------------------------------------------------------------------
ACTION  ( cFilter := "JPG (*.jpg)|*.jpg|" + ;
          "BMP (*.bmp)|*.bmp|" + ;
          "DIB   (*.dib)| *.dib|" + ;
          "PCX   (*.pcx)| *.pcx|" + ;
          "JPEG  (*.jpg)| *.jpg|" + ;
          "GIF   (*.gif)| *.gif|" + ;
          "TARGA (*.tga)| *.tga|" + ;
          "RLE   (*.rle)| *.rle|", ;
          cNewBITM := cGetFile32( cFilter,"Select a Picture" ,,  "\" + CurDir()+ "\Images" ), ;
          IIF( EMPTY( cNewBITM ), ;
                MsgAlert( "
No file selected !","ATTENTION" ), ;
                  ( cOnlyName := cFileNoExt( cNewBITM ), ;
                    cExtension := cFileExt( cNewBITM ) ) )  

// Return next filename image (without path)
// cPath. Optional

FUNCTION NextFile(cPath)
Local cImage, nImage:= 0
Local cLastImage

IF cPath == NIL
    cPath := "
."
ENDIF

// looks for Copys with Underline from Original ( no Underline )
// ------------------------------------------------------------------------------
aDir := Directory(cPath + "
\_" + cOnlyName + "_*." + cExtension)
IF Empty( aDir ) //  No Copys, only Original exists without Underline like => Image.jpg
    // create the 1. Underline
    cImage := cPath + "
\_" + cOnlyName + "_1." + cExtension // create the 1. Underline
ELSE
    aDir:= aSort( aDir, , , {|x,y| x[1] > y[1] } )  // Ascending
    cLastImage := cFileNoExt(aDir[1][1])
    nPos := RAT( "
_", cLastImage )
    nLen := Len(cLastImage)
    // define the next Copy-number
    // -------------------------------------
    nImage := VAL( SUBSTR( cLastImage, nPos + 1, nLen - nPos ) ) + 1
    // create a Copyname with new Number
    // -------------------------------------------------
    cImage:= cPath + "
\_" + cOnlyName + "_" + LTrim(Str(nImage)) + "." + cExtension
ENDIF

// msgalert( cImage )

RETURN cImage
 


Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: How to create Auto-filenames ( Counter ) ?

Postby hmpaquito » Sat Aug 06, 2011 7:34 am

Uko,

A litttle change.


Code: Select all  Expand view

// Return next filename image (without path)
// cPath. Optional
// cPatternFile: example Image.Jpg
FUNCTION NextFile(cPath, cPatternFile)
Local cImage, nImage:= 0
Local cLastImage
Local cExtension:= cFileExt(cPatternFile)
Local cOnlyName:= cFileNoExt(cPatternfile)
Local nPos

IF cPath == NIL
    cPath := "."
ENDIF

aDir := Directory(cPath + "\_" + cOnlyName + "_*." + cExtension)
IF !Empty( aDir )

    aDir:= aSort( aDir, , , {|x,y| x[1] > y[1] } )  // Ascending
    cLastImage := cFileNoExt(aDir[1][1])

    nPos := RAT( "_", cLastImage )
// not necessary !    nLen := Len(cLastImage)

    nImage := VAL( SUBSTR( cLastImage, nPos + 1))       // // not necessary !  , nLen - nPos ) )
 
ENDIF
nImage++

cImage:= cPath + "\_" + cOnlyName + "_" + LTrim(Str(nImage)) + "." + cExtension


// msgalert( cImage )

RETURN cImage
 


Regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: How to create Auto-filenames ( Counter ) ?

Postby nageswaragunupudi » Sat Aug 06, 2011 10:56 am

Mr. Uwe

Can you not use the already available FW function
cNewFileName( cRootName, cExt ) ?

I think making use of functions already available in the library reduces the size of your source code as well as saves your time. I do not understand why all of you are re-inventing the wheel.
Regards

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

Re: How to create Auto-filenames ( Counter ) ?

Postby hmpaquito » Sat Aug 06, 2011 11:33 am

Mr. nages,

NextFile() and cNewFileName() are diferents wheels :wink:

1st) cNewFileName() do not provide Path parameter
2nd) cNewFileName() do not use _xxxx_N.Ext pattern else xxxxN.Ext pattern
3rd) cNewFileName() have too much disk read in File() function when there is many pattern file

Regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: How to create Auto-filenames ( Counter ) ?

Postby ukoenig » Sat Aug 06, 2011 12:01 pm

Thank You very much for all Help.

The Logic of the File-handling I using :

1 ) I select a File
Image.jpg

2 ) make a copy ( Working-file for Painting, never touching the Original )
Image_P.jpg

3 ) after finished Painting, Export to new Format

3a ) in Overwrite-mode it will be :
_Image_1.pdf ( using only filepart < _1.pdf > )

3b ) In multiple File-Export ( saving all different Paintings ) it will be :
_Image_1.pdf
some changes on < Image_P.jpg > again
exported to :
_Image_2.pdf
some more changes on < Image_P.jpg >
exported to :
_Image_3.pdf
...
...

Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: How to create Auto-filenames ( Counter ) ?

Postby ukoenig » Sat Aug 06, 2011 11:43 pm

I still found a little Problem of Array-sorting
to bring the last one on Top, we have to add some Lines ( will count up to 1000 )

Code: Select all  Expand view

nImage++

IF nImage < 10
    cImage:= cPath + "\_" + cOnlyName + "_00" + LTrim(Str(nImage)) + "." + cExtension
ENDIF
IF nImage > 9 .and. nImage < 100
    cImage:= cPath + "\_" + cOnlyName + "_0" + LTrim(Str(nImage)) + "." + cExtension
ENDIF
IF nImage > 99
    cImage:= cPath + "\_" + cOnlyName + "_" + LTrim(Str(nImage)) + "." + cExtension
ENDIF
 


Image

Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: How to create Auto-filenames ( Counter ) ?

Postby Otto » Sun Aug 07, 2011 5:07 am

Hello Uwe,
try with:
PADL(<exp>, <nLength>, [<cFillChar>])
--> cPaddedString
Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6064
Joined: Fri Oct 07, 2005 7:07 pm

Re: How to create Auto-filenames ( Counter ) ?

Postby nageswaragunupudi » Sun Aug 07, 2011 6:14 am

Code: Select all  Expand view
cImage:= cPath + "\_" + cOnlyName + "_" + StrZero(nImage,3) + "." + cExtension
Regards

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

Re: How to create Auto-filenames ( Counter ) ?

Postby hmpaquito » Sun Aug 07, 2011 7:19 am

Uko,

Code: Select all  Expand view

#Define ORDER_(cImageNoExt) Val(SubStr(cImageNoExt, RAt("_", cImageNoExt)+ 1))    
aDir:= aSort( aDir, , , {|x,y| ORDER_(cFileNoExt(x[1])) > ORDER_(cFileNoExt(y[1])) } )  // Ascending
 

Regards
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: How to create Auto-filenames ( Counter ) ?

Postby ukoenig » Sun Aug 07, 2011 2:55 pm

Thanks a lot for all the Help.

I added :
cImage := cPath + "\_" + cOnlyName + "_" + StrZero(nImage,3) + "." + cExtension

that works fine for me.

I still have to check a list of round about 500 different Fileformats.
I will test the formats ( Export ) could be useful for us.
As well I found a Solution, adding Alpha-chanel to a BMP-export from a selected PNG.

Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: How to create Auto-filenames ( Counter ) ?

Postby nageswaragunupudi » Sun Aug 07, 2011 3:44 pm

Scanning an array to find maximum value is faster than sorting entire array just to find the maximum value.
Regards

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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 47 guests