FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to create Auto-filenames ( Counter ) ?
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
How to create Auto-filenames ( Counter ) ?
Posted: Fri Aug 05, 2011 05: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 :-)
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.
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Fri Aug 05, 2011 06:13 PM
Uko,

Whithout testing,

Code (fw): Select all Collapse
//--------------------------------------------------------------------------------------------//
// 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
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Fri Aug 05, 2011 11:36 PM
Thank You very much for Your help.
I changed some Lines :

Code (fw): Select all Collapse
// 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 :-)
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.
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sat Aug 06, 2011 07:34 AM
Uko,

A litttle change.


Code (fw): Select all Collapse
// 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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to create Auto-filenames ( Counter ) ?
Posted: 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
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sat Aug 06, 2011 11:33 AM
Mr. nages,

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

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
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: 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 :-)
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.
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: 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 (fw): Select all Collapse
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




Best Regards
Uwe :-)
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.
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sun Aug 07, 2011 05:07 AM

Hello Uwe,
try with:
PADL(<exp>, <nLength>, [<cFillChar>])
--> cPaddedString
Best regards,
Otto

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sun Aug 07, 2011 06:14 AM
Code (fw): Select all Collapse
cImage:= cPath + "\_" + cOnlyName + "_" + StrZero(nImage,3) + "." + cExtension
Regards



G. N. Rao.

Hyderabad, India
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sun Aug 07, 2011 07:19 AM
Uko,

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

Regards
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sun Aug 07, 2011 02: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 :-)
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.
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to create Auto-filenames ( Counter ) ?
Posted: Sun Aug 07, 2011 03: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

Continue the discussion