If file exists code but with some special features

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Otapi
Posts: 13
Joined: Thu Mar 13, 2008 10:43 am

If file exists code but with some special features

Post by Otapi »

Greetings!

I have some calc files with names:

20080516_64329_LASKU
20080617_65374_TARJOUS
20080713_67321_LASKU

These files are bills for the shop im currently as trial at.

First four numbers in name mean year, second two numbers after those mean month and two numbers after that mean day, so first eight numbers form date, then after flat line comes five numbers which mark the code of bill and the word after that mark the exact meaning of what kind of bill is it (LASKU is finnish and means bill, TARJOUS is an offer and so on)

I need a "if file exists" macro which only checks the code part (five numbers in middle) and informs (with msgbox or something) that there is a file with same code already, because there can be files with same date but not with same code.

I guess http://www.oooforum.org/forum/viewtopic ... aving+file solves a little of my problem but not everything.
User avatar
DrewJensen
Volunteer
Posts: 1734
Joined: Sat Oct 06, 2007 9:01 pm
Location: Cumberland, MD - USA

Re: If file exists code but with some special features

Post by DrewJensen »

You can use the DIR command for this ( NOTE you find information on this in the StarOffice 8 Basic Programmers Guide found at the SUN web site )

Code: Select all

function CheckForFile( aPath as string, aCode as String, aFileName as string ) as boolean
REM
REM  aPath is the directory to check in
REM  aCode is the coded part of the file name that we want to check for
REM  aFileName will hold the name of the found file, if one is found
REM
REM  CheckForFile returns true if a file with the code is found
REM  otherwise it will return false

  CehckForFile = FALSE
  aFileName = ""
  
  DIM CurrentFileName as string

  CurrentFileName = DIR( aPath, 0 ) 
  while CurrentFileName <> ""
  	if InStr(1, CurrentFileName, aCode) <> 0 then
  		CheckForFile = TRUE
  		aFileName = CurrentFileName
  		exit function
  	end if
    CurrentFileName = DIR
  wend
  	
end function
Let's say I have a file named 20080517_1234_drew.ods in my C:\tmp dierctory.
Then I could check for that with something like:

Code: Select all

Sub Main
  dim aFile as string
  
  if CheckForFile( "c:\tmp\", "_1234_", aFile ) then
  	msgBox( aFile )
  end if  
End Sub
HTH

Drew
Former member of The Document Foundation
Former member of Apache OpenOffice PMC
LibreOffice on Ubuntu 18.04
Post Reply