Page 1 of 1 1
Topic Options
#198172 - 2010-03-24 04:32 PM Regional Date Issue in Script
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
HI, I have inherited a script that to me seems complex. I'm new to kix and any help appreciated. Basically the script sets a bunch of variables to get the date of a file from a listing. This used to be fine however, we have just moved the script to a new server that has been built and is set to use UK Regional Settings. So my question is how do I go about changing the script to do this the other way round now.

See extract from the script. If you need any more details please just shout.

many thanks for any help that I get.

Create date filename for logging
$yr=left(@date,4)
$mn1=right(@date,5)
$mn=left($mn1,2)
$dy=right($mn1,2)
@crlf
"Branch Journal File copy to G: Drive"
@crlf
"Do not close this window - It will close when the task is complete"

;****** Set date and create output file

$SY = 1 + left(@date,4) - 1
$SystemYear = 1 + right($SY,2) - 1
$mn1 = 1 + right(@date,5) - 1
$SystemMonth = 1 + left($mn1,2) - 1
$SystemDay= 1 + right(@date,2) - 1
$SystemDays=($SystemYear*365)+($SystemMonth*30.4167)+$SystemDay

Top
#198173 - 2010-03-24 04:57 PM Re: Regional Date Issue in Script [Re: roberm]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Welcome to the board.

No changes needed - the macro @DATE *always* returns the date in YYYY/MM/DD order.

Top
#198183 - 2010-03-25 05:36 PM Re: Regional Date Issue in Script [Re: Richard H.]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Thanks... Maybe I need to show more of the script. You could be completely right. My script isn't running at the moment and KIX32 just hangs ... the first day that it did this was the 13th of the month so I automatically assummed that it was due to the date format as there is clearly no 13th month? Whats the best way to show the whole script as its 11 A4 pages long?

Thanks very much for your help

Top
#198184 - 2010-03-25 07:25 PM Re: Regional Date Issue in Script [Re: roberm]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
You might want to simply tell us what you are trying to accomplish.. when I run the code you posted, it returns a number around 3760, which is kind of meaningless to me at first glance.

Let's look at your code:
 Code:
; Get a 4-digit year from @DATE
$yr=left(@date,4)
; get the part of the date where month is on the left and day is on the right
$mn1=right(@date,5)
; now take just the 2 left or 2 right values to get Month number and Day nubmber
$mn=left($mn1,2)
$dy=right($mn1,2)
; that was the hard way - easy way is:
$yr = @YEAR
$mn = @MONTHNO
$dy = @MDAYNO
Keep in mind that the "easy way" has 1 or 2 digit values, while the "hard way" will always have 2 digit strings.. What's odd is that you don't use any of those variables in the part of the script you've shown. Since most macros take no more effort to use than variables, I'd consider replacing any reference to your vars with the macros, unless you need the 2-digit strings. Even then, I'd create the 2-digit string for the month and day with the form
 Code:
$Value = Right('0' + @MONTHNO, 2)
Let's continue..
 Code:
; This takes the value 1, adds the year portion of @DATE as a string..
; starting the formula with a number causes Kix to convert the string
; to a number. The value 1 is then subtracted. Again, the long way to get there..
; $SY = Val(Left(@DATE, 4)) is less obscure, but $SY = @YEAR gets the job
; done directly
$SY = 1 + left(@date,4) - 1

; This converts to a 2-digit year, again wrapping it in a formula to 
; return a number. Just as strange, and can be simplified to
; $SystemYear = Val(Right(@YEAR, 2))
$SystemYear = 1 + right($SY,2) - 1

; more of the same.. simplify to 
;$SystemMonth = @MONTHNO
; and 
;$SystemDay = @MDAYNO
$mn1 = 1 + right(@date,5) - 1
$SystemMonth = 1 + left($mn1,2) - 1
$SystemDay= 1 + right(@date,2) - 1

; Given that today is 2010/03/25, your formula works out to:
; $SystemYear = 10, $SystemMonth = 3, and $SystemDay = 25
; (by whatever method you choose)
; $SystemDays = (10 * 365) + (3 * 30.4167) + 25
; $SystemDays = 3650 + 91.25 + 25, or simply 3766.25
$SystemDays=($SystemYear*365)+($SystemMonth*30.4167)+$SystemDay
Good gosh, is this supposed to be the number of days since 1/1/2000??? If so, the value is off by 30.4167! It is actually the number of days since 1999/12/01.

If this is indeed what you're after, using one of the available UDFs would simplify this greatly. Both TimeConvert and TimeDiff can accomplish this in one line:
 Code:
; Using TimeConvert and specifying the base date as the epoch value:
$SystemDays = TimeConvert(@DATE + ' 00:00:00', '2000/01/01') / 86400
; Since TimeConvert returns a cTime value (seconds of elapsed time) we need to divide by 86400 to get days

; using TimeDiff, specifying the start date, current date, and requesting "days" be returned
$SystemDays = TimeDiff('2000/01/01', 'today', 'D')
The whole "1 + some_string - 1" method is very obscure, not to mention unnecessary. Simplifying the code will make it easier to support.

If you choose to use one of the Time UDFs, make sure you download it and include it in your script. The latest versions of both UDFs are posted in the Kix Library on my web site.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198188 - 2010-03-26 12:17 AM Re: Regional Date Issue in Script [Re: Glenn Barnas]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Hi Glenn, thank you very much for explaining that. I thought that there would be a simpler way. I inherited this script and it is basically supposed to look in a folder for files older than 30 days and move them to a specifically named folder by year which it creates as it goes along. Originally there were files to get from before 2000 hence the 1999 date, however this is no longer really needed.

SO what it is supposed to do is check a filelist txt file that is created from a dir for a date string to find files older than 30 days and move these to the relevant month/year folder..

I have placed the entire code below so you can see what its doing, my issue I think is the way that it is now formatting the mm/dd as dd/mm due to the new server this now runs on as being set with UK Regional settings whereas the old server was set with US regional settings, so I need it to be able to see the dates and create in the correct Regional setting.

The folder output is supposed to be servername\year\01-january etc..

 Code:
 ;	Variables used

;	$HasSpace		Used to determine if branch code < 3 digits
;	$Srvah			Local machine name
;	$FName			Current file
;	$SFile			Source file for copy
;	$DFile			Destination for copy
;	$LogText		Used for output to log file
;	$LogLoc			Central location of log file for current branch

;	Date variables

;	$yr $mn1 $dy $mn	No longer used (need to be cleaned up)
;	$sy			System date interim value
;	$SystemYear		Local current year
;	$SystemMonth		Local current month
;	$SystemDay		local current day
;	$SystemDays		Local days value.  Used in calc for file age.
;	$DateStamp		File date

;****** Setup
	
	debug off	; Allows batch debug
	break on	; Allows batch to be stopped
	
;******	Create date filename for logging
	$yr=left(@date,4)
	$mn1=right(@date,5)
	$mn=left($mn1,2)
	$dy=right($mn1,2)
	@crlf
	"Branch Journal File copy to G: Drive"
	@crlf
	"Do not close this window - It will close when the task is complete"

;******	Set date and create output file

	$SY = 1 + left(@date,4) - 1
	$SystemYear = 1 + right($SY,2) - 1
	$mn1 = 1 + right(@date,5) - 1
	$SystemMonth = 1 + left($mn1,2) - 1
	$SystemDay= 1 + right(@date,2) - 1
	$SystemDays=($SystemYear*365)+($SystemMonth*30.4167)+$SystemDay

;******************************************************************************
;************************************* 022 ************************************
;******************************************************************************
;******	Gets server name from system variable

	:GetServer
	$Srvah = "HO-022"
	
;****** Server Selected
	:SSel
	shell "cmd.exe /c dir f:\JRN022 > f:\JRN022\files.txt"
	Open(3, "f:\JRN022\filemove.log", 4)
	Open(4, "f:\JRN022\files.txt", 2)
	

;****** Gets filename and gets age in days

	:GetFile
	$f = ReadLine(4)
	if instr ($f,"free") 
	goto TheEnd
	else
	if right($f,4) > ".jnl" or right($f,4) < ".jnl"
	goto GetFile
	else
	$Fname=Right($f,12)
	$LogText=$Fname
	$DateStamp=left($f,8)
	$LogText=$LogText+$DateStamp

;****** +1 -1 in this section is to create a numeric variable

	$FileYear = 1 + right($DateStamp,2) - 1
	$LogText=$LogText+"  "+$FileYear
	$mn2 = right($DateStamp,5)
	$LogText=$LogText+" "+$mn2
	$FileMonth = 1+ left($DateStamp,2) - 1
	$LogText=$LogText+" "+$FileMonth
	$FileDay= 1 + left($mn2,2) - 1
	$LogText=$LogText+" "+$FileDay
	$FileDays=($FileYear*365)+($FileMonth*30.4167)+$FileDay
	$LogText=$LogText+" "+$FileDays
	$FileAge=$SystemDays-$FileDays
	$LogText=$LogText+" "+$FileAge

;******	Establish if file move is needed
;******	This routine will IGNORE files that match the specified criteria

	if $FileAge < 30
	goto GetFile
	Else

;******	Analyse FileName

	$FDay1 = right($Fname,6)
	$FDay = left($FDay1,2)
	$FMonth1 = right($Fname,7)
	$FMonth = left($FMonth1,1)
	$FYear1 = right($Fname,9)
	$FYear = left($FYear1,2)

;******	Convert month letter to Month name
;******	Added 01, 02 etc to month name to sort directories.

	:Month
	if instr ($FMonth,"A") > 0
	$FMonth = "01-January"
	goto FileN
	endif
	if instr ($FMonth,"B") > 0
	$FMonth = "02-February"
	goto FileN
	endif
	if instr ($FMonth,"C") > 0
	$FMonth = "03-March"
	goto FileN
	endif
	if instr ($FMonth,"D") > 0
	$FMonth = "04-April"
	goto FileN
	endif
	if instr ($FMonth,"E") > 0
	$FMonth = "05-May"
	goto FileN
	endif
	if instr ($FMonth,"F") > 0
	$FMonth = "06-June"
	goto FileN
	endif
	if instr ($FMonth,"G") > 0
	$FMonth = "07-July"
	goto FileN
	endif
	if instr ($FMonth,"H") > 0
	$FMonth = "08-August"
	goto FileN
	endif
	if instr ($FMonth,"I") > 0
	$FMonth = "09-September"
	goto FileN
	endif
	if instr ($FMonth,"J") > 0
	$FMonth = "10-October"
	goto FileN
	endif
	if instr ($FMonth,"K") > 0
	$FMonth = "11-November"
	goto FileN
	endif
	if instr ($FMonth,"L") > 0
	$FMonth = "12-December"
	goto FileN
	endif
	  
	$FMonth = "All"
 
; ***** Make directory and copy for old files

	$FilePath = "g:\Journal\"+$Srvah+"\PreFeb2000"
	
; ***** If filenames are less than 12 (8.3) characters then remove the blank characters
; *****	This is used for files older than feb2000



	:DirExist
	If exist ($FilePath) 
	goto CpyFile
	else md $FilePath
	endif
	goto CpyFile
	
	:FileN
	$HasSpace = instr ($FName," ")
	if $HasSpace = 0
	goto MakDir
	else
	$Fname=right($FName,(12-$HasSpace))
	endif
	
	:MakDir
	$FilePath = "g:\Journal\"+$Srvah+"\20"+$FYear+"\"+$FMonth
	If exist ($FilePath) 
	goto CpyFile
	else
	md $FilePath
	endif

; *****	Copy file

	:CpyFile
	$SFile="f:\JRN200\"+$Fname
	$DFile=$FilePath+"\"+$Fname
	$LogText=$LogText+" "+$DFile
	copy $SFile $DFile
	
	if exist ($DFile)
	goto GoodCopy
	else
	:BadCopy
	$LogText=$LogText+" COPY FAILED"+@crlf
	writeline(3,$LogText)
	goto GetFile
	endif
	
	:GoodCopy
	$LogText=$LogText+" copied"
	del $SFile

	If exist ($SFile)
	goto NoDelete
	else
	$LogText=$LogText+" and deleted"+@crlf
	writeline(3,$LogText)
	goto GetFile
	endif
	
	:NoDelete
	$LogText=$LogText+" but not deleted"+@crlf
	writeline(3,$LogText)
	goto GetFile
	
; ***** Program end and clean up

	:TheEnd
;	close open files
	close (3)
	close (4)
	
;	make copy of log file on central area

	$LogLoc="g:\journal\"+$Srvah+"\FileMove.log"
	del $LogLoc
	copy f:JRN200\FileMove.log $LogLoc
	del f:\JRN200\files.txt                                                          

Top
#198189 - 2010-03-26 10:54 AM Re: Regional Date Issue in Script [Re: roberm]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
That's got to be one of the nastiest scripts we've been presented with in a while.

I've tried to keep to the spirit of the script in the example below, but as I cannot tell what is important in the original code and what is just clumsy coding you will have to tread carefully.

Two observations:
  • The script mixes the use of dates from the file timestamp and from the file name. It would be far more sensible to pick one or the other and stick to it for consistency.
  • The source directory paths F:\JRN200 and F:\JRN022 are mixed in the script which as far as I can tell means that it won't work as expected. I've picked one path, but only you know which is correct.


Some example filenames would have made things easier - all that Right() and Left() made me think I was on a parade ground.

The following sample is untested:
 Code:
Break ON	; Allows batch to be stopped

$=SetOption("Explicit","ON")

Dim $sSRVAH		$sSRVAH="HO-022"
Dim $iArchiveDays	$iArchiveDays=30
Dim $iSourceDir		$sSourceDir="F:\JRN022\"
Dim $sTargetDir		$sTargetDir="G:\Journal\"+$sSRVAH+"\"
Dim $sLogFile		$sLogFile=$sSourceDir+"FileMove.log"
Dim $sArchivePath
Dim $sFile
Dim $sFileDate
Dim $sFileYear
Dim $iFileAge
Dim $iMonthIndex
Dim $sMonthLetters	$sMonthLetters="ABCDEFGHIJKL"

Dim $asMonths		$asMonths=Split("ALL January February March April May June July August September October November December")

Dim $fhLog		$fhLog=FreeFileHandle()


"Branch Journal File copy to G: Drive"+@CRLF
"Do not close this window - It will close when the task is complete"+@CRLF

If Open($fhLog,$sLogFile,4+1)
	"ERROR: Cannot open log file for writing, reason: ["+@ERROR+"] "+@SERROR+@CRLF
	"Aborting."+@CRLF
	Exit @ERROR
EndIf

$sFile=Dir($sSourceDir+"*.jnl")
While Not @ERROR
	$sFileDate=Split(GetFileTime($sSourceDir+$sFile))[0]
	$iFileAge=CInt(SerialDate(@DATE))-SerialDate($sFileDate)
	If $iFileAge > $iArchiveDays
		$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,6,1)
		$sFileYear="20"+SubStr($sFile,4,2)
		$sArchivePath=$sTargetDir+$sFileYear+"\"+Right("0"+$iMonthIndex,2)+"-"+$asMonths[$iMonthIndex]
		$=WriteLine($fhLog,"Archiving "+$sFile+" to "+$sArchivePath+@CRLF)
		If Not Exist($sArchivePath) MD $sArchivePath EndIf
		Move $sSourceDir+$sFile $sArchivePath
		If @ERROR
			$=Writeline($fhLog,"  FAILED with error ["+@ERROR+"] "+@SERROR+@CRLF)
		Else
			$=WriteLine($fhLog,"  Moved OK"+@CRLF)
		EndIf
	EndIf
	$sFile=Dir()Split(GetFileTime($sSourceDir+$sFile))[0]
Loop

$=Close($fhLog)

Move $sLogFile $sTargetDir

Exit 0

; *********************** PAY NO ATTENTION TO THE MAN BEHIND THE CURTAIN ********************************
;
;FUNCTION      SerialDate
;
;ACTION        Convert dates to numbers (and back) for the purpose of performing date math
;
;AUTHOR        ScriptLogic (http://www.scriptlogic.com)
;
;CONTRIBUTOR   Jens Meyer (sealeopard@usa.net)
;
;VERSION       1.1
;
;SYNTAX        SERIALDATE(DATE)
;
;PARAMETERS    DATE or NUMBER
;              if a date is used, it must be in the form of "YYYY/MM/DD"
;              if a number is used, it must be a number previously derived from this function.
;
;RETURNS       If a date is passed to this function, the function returns a number. If a number is
;              passed to this function, a date "YYYY/MM/DD" is returned
;
;REMARKS       This function was developed as a core routine for the DateMath( ) function. In
;              normal usage, you would most like just use the DateMath( ) function which depends
;              on this function.
;              Algorithms used in the development of this routine were obtained from:
;              http://www.capecod.net/~pbaum/date/date0.htm
;
;              Fixed a couple of inconsistencies in the returned values and formatting
;
;              Original UDF is posted at http://www.scriptlogic.com/kixtart/FunctionLibrary_ViewFunction.aspx?ID=SerialDate
;
;DEPENDENCIES  none
;
;EXAMPLE       $rc=serialdate('2001/07/01')
;
;KIXTART BBS   http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000089
;
function serialdate($ExpD)
  dim $z,$h,$a,$b,$c,$y,$m,$d
  if instr($ExpD,'/')
    $ExpD=split($ExpD,'/')
    $y=val($ExpD[0])
    $m=val($ExpD[1])
    $d=val($ExpD[2])
    if $m<3
      $m=$m+12
      $y=$y-1
    endif
    $SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
  else
    $z=0+$ExpD+306
    $h=100*$z-25
    $a=$h/3652425
    $b=$a-$a/4
    $y=(100*$b+$h)/36525
    $c=$b+$z-365*$y-$y/4
    $m=(5*$c+456)/153
    $d=$c-(153*$m-457)/5
    if $m>12
      $y=$y+1
      $m=$m-12
    endif
    $SerialDate=right('0000'+$y,4)+'/'+right('00'+$m,2)+'/'+right('00'+$d,2)
  endif
endfunction

Top
#198190 - 2010-03-26 11:54 AM Re: Regional Date Issue in Script [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
 Originally Posted By: Richard H.
That's got to be one of the nastiest scripts we've been presented with in a while.
I looked at this last night, thought something along the same lines, and went to sleep, thinking that I could be more diplomatic in the morning. \:D

It might get an award for the most number of GOTOs per inch. I had scribbled somthing similar on paper before rolling off into a fitful sleep - I'm glad you were able to look at this with fresh eyes, Richard! I hope you earn yourself a pint!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198208 - 2010-03-26 04:56 PM Re: Regional Date Issue in Script [Re: Glenn Barnas]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Hi, I think we are getting there.. I have run this in the test environment now and had to make a couple of changes due to file locations etc... Filenames that we use here would be like this:

2206L11.jnl
2208C10.jnl
2208C11.jnl

File structure that I have is F:\Journal\HO-022 and then I need the files passed into F:\Journal\Archive\HO-022\Month (this is created from script) if it doesn't exist? Does that make sense, as not much of this does to me!!! Thanks muchly for all your help to date.

These would be date/time stamped 04/01/2010 or 16/12/2009 for example..

When I run the script now I get an error at line 38 saying..

ERROR : invalid method/function call: missing ')'!
Script: C:\apps\schedule\newjournal.kix
Line : 38

My amendments in your script now make it look like the below...

 Code:
  Break ON	; Allows batch to be stopped

$=SetOption("Explicit","ON")

Dim $sSRVAH		$sSRVAH="HO-022"
Dim $iArchiveDays	$iArchiveDays=30
Dim $iSourceDir		$iSourceDir="F:\Journal\HO-022\"
Dim $sTargetDir		$sTargetDir="F:\Journal\Archive"+$sSRVAH+"\"
Dim $sLogFile		$sLogFile=$iSourceDir+"FileMove.log"
Dim $sArchivePath
Dim $sFile
Dim $sFileDate
Dim $sFileYear
Dim $iFileAge
Dim $iMonthIndex
Dim $sMonthLetters	$sMonthLetters="ABCDEFGHIJKL"

Dim $asMonths		$asMonths=Split("ALL January February March April May June July August September October November December")

Dim $fhLog		$fhLog=FreeFileHandle()


"Branch Journal File copy to F: Drive"+@CRLF
"Do not close this window - It will close when the task is complete"+@CRLF

If Open($fhLog,$sLogFile,4+1)
	"ERROR: Cannot open log file for writing, reason: ["+@ERROR+"] "+@SERROR+@CRLF
	"Aborting."+@CRLF
	Exit @ERROR
EndIf

$sFile=Dir($iSourceDir+"*.jnl")
While Not @ERROR
	$sFileDate=Split(GetFileTime($iSourceDir+$sFile))[0]
	$iFileAge=CInt(SerialDate(@DATE))-SerialDate($sFileDate)
	If $iFileAge > $iArchiveDays
		$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,6,1)
		$sFileYear="20"+SubStr($sFile,4,2)
		$sArchivePath=$sTargetDir+$sFileYear+"\"+Right("0"+$iMonthIndex,2)+"-"+$asMonths[$iMonthIndex]
		$=WriteLine($fhLog,"Archiving "+$sFile+" to "+$sArchivePath+@CRLF)
		If Not Exist($sArchivePath) MD $sArchivePath EndIf
		Move $iSourceDir+$sFile $sArchivePath
		If @ERROR
			$=Writeline($fhLog,"  FAILED with error ["+@ERROR+"] "+@SERROR+@CRLF)
		Else
			$=WriteLine($fhLog,"  Moved OK"+@CRLF)
		EndIf
	EndIf
	$sFile=Dir()Split(GetFileTime($iSourceDir+$sFile))[0]
Loop

$=Close($fhLog)

Move $sLogFile $sTargetDir

Exit 0

function serialdate($ExpD)
  dim $z,$h,$a,$b,$c,$y,$m,$d
  if instr($ExpD,'/')
    $ExpD=split($ExpD,'/')
    $y=val($ExpD[0])
    $m=val($ExpD[1])
    $d=val($ExpD[2])
    if $m<3
      $m=$m+12
      $y=$y-1
    endif
    $SerialDate=$d+(153*$m-457)/5+365*$y+$y/4-$y/100+$y/400-306
  else
    $z=0+$ExpD+306
    $h=100*$z-25
    $a=$h/3652425
    $b=$a-$a/4
    $y=(100*$b+$h)/36525
    $c=$b+$z-365*$y-$y/4
    $m=(5*$c+456)/153
    $d=$c-(153*$m-457)/5
    if $m>12
      $y=$y+1
      $m=$m-12
    endif
    $SerialDate=right('0000'+$y,4)+'/'+right('00'+$m,2)+'/'+right('00'+$d,2)
  endif
endfunction                                                       

Top
#198209 - 2010-03-26 05:04 PM Re: Regional Date Issue in Script [Re: roberm]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
My typo.

This line:
 Code:
		$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,6,1)


is missing a trailing parenthesis:
 Code:
		$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,6,1))


Sorry 'bout that.

I'm off home for a weekend of fitting a new kitchen <sigh> but there are plenty of helpful people who should be able to assist in correcting any other of my school-boy errors.

Top
#198211 - 2010-03-26 05:25 PM Re: Regional Date Issue in Script [Re: roberm]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Hi, sorry, forget the previous error, spotted that, just a missing bracket... Thats sorted now. So my only issue now is to get the folder structure correct for the Archive...

So the structure that I am trying to achieve is:

F:\Journal\Archive\HO-022\2010\01-January\*.jnl files

What I currently get is:

F:\journal\Archive\HO-022\206L\00-ALL\*.jnl this looks to be the filename being broken up to create the directories as the actual file placed in the above folder is called 2206L11.jnl So is this the timesatmp that needs to be used to create the fodler structure instead?

Mucho Gracias for all your help in advance..

Top
#198212 - 2010-03-26 05:26 PM Re: Regional Date Issue in Script [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Richard: It's funny/scary how similar your posted code and the stuff I penciled last night are. Biggest difference is that I use a single call to TimeDiff('today', $FileDate. 'd') instead of 2 calls to SerialDate. \:D

Roberm:

It's not a good idea to remove the header from UDFs. The comments don't hurt anything and will identify how to use the code (and prevent the temptation to modify it.)

I'd also go back and review Richard's code very carefully and insert LOTS of comments so you know exactly what each line or section of code is doing. This will help when you come back in 6 months to change something, or you get promoted and your replacement has to figure it out. ;\) Some of my larger scripts (16,000+ lines) are 55% comments by weight (char count).

There's a UDF called Sanity that will examine your code and point out common errors, such as missing or mismatched parens, quotes, etc. The KixDev package on my web site provides a KGen script that automatically runs Sanity on your script files. Very helpful for finding those common yet hard to find errors.

FYI - one thing that I have successfully done to deal with multi-national time formats is dynamically change the time display format to YYYY/MM/DD and perform all my calculations based on that format. When the script ends, it restores the previous format. It is a user setting, so won't affect anything else on the system.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198213 - 2010-03-26 05:41 PM Re: Regional Date Issue in Script [Re: Glenn Barnas]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Hi Glenn,

Thanks again for the comments, I really do appreciate the help here, I am a total noob at this kix scripting and on a steepo learning curve. I only removed the commenst about the function to save a bit of space on here to display the code.. I have left it in my script and I think that your comment about adding comments is a very valid one, something that I will definately do as it will be me for a while that looks at this.

The only issue I have is to make the directory structure work as it used to now, any ideas on how that can be achieved, its all so close and Richard has done a fantastic job so far, and now has a weekend of Kitchen fitting, I really dont envy him that!!!

Thanks again

Top
#198214 - 2010-03-26 05:42 PM Re: Regional Date Issue in Script [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
This seems incorrect - just prior to the "Loop" statement:
 Code:
$sFile=Dir()Split(GetFileTime($iSourceDir+$sFile))[0]
likely a result of an errant paste operation. Change it to
 Code:
$sFile=Dir()
This shouldn't harm anything, but will likely result in strange screen output.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198215 - 2010-03-26 05:51 PM Re: Regional Date Issue in Script [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
OK - here's some comments to explain Richard's code
 Code:
; read the first directory entry
$sFile=Dir($iSourceDir+"*.jnl")

; Loop until an error occurs (no more files)
While Not @ERROR

        ; get the file timestamp, and split off the date portion only
	$sFileDate=Split(GetFileTime($iSourceDir+$sFile))[0]

        ; Calculate the difference between today and the file timestamp
	$iFileAge=CInt(SerialDate(@DATE))-SerialDate($sFileDate)

        ; If the age is more than the archive limit, process it
	If $iFileAge > $iArchiveDays

                ; get the character that represents the month
		$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,6,1)

		; Not too sure about this.. results in "20" plus the 4th and 5th chars of the file name?
		; Do any files date back to before 2000?
		$sFileYear="20"+SubStr($sFile,4,2)

		; Define the path where the file will be archived
		; path is $sTargetDir + $sFileYear defined above, then a 2-digit month number, a dash, and finally the month-name
		$sArchivePath=$sTargetDir+$sFileYear+"\"+Right("0"+$iMonthIndex,2)+"-"+$asMonths[$iMonthIndex]
'DEBUG - Archive Path:' ?
$sArchivePath ?

		; log the action
		$=WriteLine($fhLog,"Archiving "+$sFile+" to "+$sArchivePath+@CRLF)

		; create the archive path if it doesn't exist
		If Not Exist($sArchivePath) MD $sArchivePath EndIf

		; move the file to the archive folder and log success or failure
		Move $iSourceDir+$sFile $sArchivePath
		If @ERROR
			$=Writeline($fhLog,"  FAILED with error ["+@ERROR+"] "+@SERROR+@CRLF)
		Else
			$=WriteLine($fhLog,"  Moved OK"+@CRLF)
		EndIf
	EndIf

	; get the next file from the directory
	$sFile=Dir()
Loop
I'd pay close attention to how the $sFileYear value is being defined. Note that I added a Debug statement at that point. That's where it is messing up, and you'll need to tweak the value based on intimate knowledge of the file names/paths.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198227 - 2010-03-29 09:37 AM Re: Regional Date Issue in Script [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Glenn's right, the substring indexes are at fault.

You will recall that I said the original script was mixing dates from the file timestamp and the file name. The timestamp code will always work OK, but the code which extracts the month and year from the file will only work so long as the file is exactly the right pattern. Having not seen the filenames at the start I calculated them based on the comments in the original script, and one of us was off \:\)

The month and year are extracted from the filename in two places, and neither of these match your sample filenames at the moment.

Are the filenames *always* in the form of the sample that you provided, i.e. 2 digit year in positions 3-4, and month code in position 5?

If that is the case you need to change the following two lines:

 Code:
$sFileYear="20"+SubStr($sFile,4,2)

...change substring start position 4 to 3...
 Code:
$sFileYear="20"+SubStr($sFile,3,2)


And
 Code:
$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,6,1)

...change substring start position 6 to 5...
 Code:
$iMonthIndex=InStr($sMonthLetters,SubStr($sFile,5,1)


BTW, I used "00-All" rather than "All" for the directory name for invalid month codes as the original script stated a requirement for a natural directory sort. If this is a problem then we can fix it up quite simply.

Top
#198297 - 2010-03-31 03:58 PM Re: Regional Date Issue in Script [Re: Richard H.]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Hi Richadr and Glenn, I just want to say thank you very much for all your help here, I have the script working now having made the few tweaks you suggested.

I have also commented up the script so that I know for any future changes, again thanks for that. I think that I am going to look at this kix scripting thing a bit more, seems like I have a large learning curve ahead.

Hope the kitchen went in Richard and I hope you find your rocket Glenn!!

Thanks again

Top
#198307 - 2010-03-31 06:49 PM Re: Regional Date Issue in Script [Re: roberm]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
You might find my Intro to Programming document helpful. I use it when running small Admin Scripting classes. The references are generic, but the examples are Kix-based.

It should provide enough about the basics to get you moving.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198314 - 2010-04-01 05:47 PM Re: Regional Date Issue in Script [Re: Glenn Barnas]
roberm Offline
Fresh Scripter

Registered: 2010-03-24
Posts: 8
Loc: Bristol - UK
Thanks Glenn.... I se some study days coming on for me \:\)
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 601 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.069 seconds in which 0.016 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org