Pax
(Getting the hang of it)
2006-12-01 10:51 AM
Directory creation based on date issue

Hey All,

Hoping you can help with my little issue.

I am writing a KiX script to run each day. This script rars up data on one network location and transmits it across the WAN to a new location and puts it in a directory based upon the date, then extracts the data.

While doing this, it logs all the information to a log file on the destination drive in a LOGS directory (funnily enough).

My problem is that while
Code:
MD "$ARCHIVEDRIVE\@YEAR@MONTHNO@MDAYNO"

works fine, where $ARCHIVEDRIVE is a declared drive letter at the top of the script and would produce J:\2006121 for today, I can get no variation of
Code:
$ARCHIVE = @YEAR + "-" + @MONTHNO + "-" + @MDAYNO
$ARCHIVE = @YEAR + @MONTHNO + @MDAYNO
$ARCHIVE = @YEAR@MONTHNO@MDAYNO


Work for producing the same result.

The first code option defines $ARCHIVE as being 2019 (riddle me why that happens), as does the second, while the third gives me 121 on one line and 2006 on the next.

I am using KiXtart 2010 4.50.

Can anyone either help me redefine $ARCHIVE obtaining the correct results as per the MD results, or figure out why my definition of $ARCHIVE isn't defining correctly given I want the output (for today, for example to be 2006121)???? I could even handle if it gave the result of 2006-12-1.

Pax

edit: I should note that I am using the $ARCHIVE variable as a base for creating the rar file, log file and copying the data, and don't really want to use @YEAR@MONTHNO@MDAYNO throughout the entire script if I can help it. Would this really work though considering I cannot get KiX to define it correctly at the top.


AllenAdministrator
(KiX Supporter)
2006-12-01 02:34 PM
Re: Directory creation based on date issue

See if this helps explain your problem.

Code:
? @YEAR + @MONTHNO + @MDAYNO
? vartypename(@YEAR + @MONTHNO + @MDAYNO)
?
? "" + @YEAR + @MONTHNO + @MDAYNO
? vartypename("" + @YEAR + @MONTHNO + @MDAYNO)
 


AllenAdministrator
(KiX Supporter)
2006-12-01 02:40 PM
Re: Directory creation based on date issue

... and you might even consider adding the following modifications so that the directory names are consistent:

Code:
? "" + @YEAR + right("0" + @MONTHNO,2) + right("0" + @MDAYNO,2)
 


Pax
(Getting the hang of it)
2006-12-04 09:44 AM
Re: Directory creation based on date issue

Hey Allen,

I'll have a look at that today. Thanks for the reply,

Pax

edit: seems the first value is defined as a long, while the second is a string, I can see due to the "null" space at the front. It is weird that KiX defines it differently depending on whether MD is used or we are defining a term internally to the script.


Pax
(Getting the hang of it)
2006-12-04 11:28 AM
Re: Directory creation based on date issue

OK, I've decided to leave the dates in the non-corrected format, otherwise I'd have to mess around with the MD command, and due to time pressures for this, I will leave it till a possible new revision of the script.

The next issue I've encountered is

ERROR : expected ')'!
Script: F:\.kix
Line : 103

the lines surrounding 103 are;
Code:
If $REPORTFILE <> $ARCHIVEFILE
	SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s File_Copy_Error -server " + $EMAILSERVER)
ENDIF
;
;---- Extracting files onto Archive
;
$VARSET = WRITELINE (1, "Extracting " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE + ".rar" +  @CRLF)
Shell ("cmd.exe /c " + $SCRIPTROOT + "\unrar.exe e -r " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE ".rar " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" )
	IF @ERROR = 0
		"....Done"


Line 103 is the Shell (unrar)line, not the 'Blat' shell command. I've got a similar line above which does the rar-ing of the data and can run the unrar command by itself fine, so I'm confused as to where there is a missing ')', or a rogue inserted '(',

Pax


NTDOCAdministrator
(KiX Master)
2006-12-04 11:37 AM
Re: Directory creation based on date issue

Quote:
I am using KiXtart 2010 4.50.


You may need to double-check confirm all code works as expected. There were numerous changes to copy/move/md/rd and all appear to be working well now in version 4.53 (best to update if you can)

As for the errors you will probably need to post the whole script so we can view where the errors might be.


Pax
(Getting the hang of it)
2006-12-04 12:11 PM
Re: Directory creation based on date issue

Script posted below as requested ....

As I alluded to, it gets all the way through up to the unraring part of the script. The servers sit in two untrusted domains.

If anything more is needed let me know. I will probably put the @error and @serror in if there was a failure before sending the email, but at the moment I want to get successfuls working,

Pax

Code:
;:KVIS:Edited on 04/12/2006 09:38:57 by Pax.|E:KVIE:
; This is a test script for moving the archive data
;
;--------------------------------------------------------------------------------------------------------------------
;
;---- Script version 0.2
;---- Created by Pax
;---- Last edit date 04/12/2006 by Pax
;
;---- Declare Variables -------
;
$SCRIPTROOT = "\\server1\path\"
$REPORTSTORE = "\\server2\path"
$ARCHIVESTORE = "\\server3\path"
$REPORTUSER = domain1\user
$ARCHIVEUSER = domain2\user
$REPORTDRIVE = I:
$ARCHIVEDRIVE = J:
$ARCHIVE = "" + @YEAR + @MONTHNO + @MDAYNO
? $ARCHIVE
$ARCHIVELOG = $ARCHIVEDRIVE + "\LOGS\" + $ARCHIVE + ".LOG"
? $ARCHIVELOG
$EmailServer = "our.emailserver.net"
$Sender = "sender@@emailserver.net"
$SupportEmail = "support@@emailserver.net"
$EmailAddress = "recipient@@emailserver.net"
;
;---- If the password for the REPORTUSER account changes on server2, change the password on the ARCHIVEUSER account, 
;---- and update the password in this script. Without this the script will fail
;
$USERPASS = password
;
;---- Mapping Archive drive first to create log file
;
? "Mapping the Archive Drive"
USE $ARCHIVEDRIVE $ARCHIVESTORE /USER:$ARCHIVEUSER /PASSWORD:$USERPASS
	IF @ERROR = 0
		"....Done"
	ELSE
		"..Failed"
		SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Archive_Mapping_failure -server " + $EMAILSERVER)
		GOTO END
	ENDIF
;
$VARSET = OPEN(1, $ARCHIVESTORE + "\LOGS\" + $ARCHIVE + ".LOG", 5)
? @DATE
$VARSET = WRITELINE (1, "The current date is " + @DATE + @CRLF)
;
;---- Do drive mapping for Report drive to move the data
;
$VARSET = WRITELINE (1, "Mapping the Report Drive")
USE $REPORTDRIVE $REPORTSTORE /USER:$REPORTUSER /PASSWORD:$USERPASS
	IF @ERROR = 0
		"....Done"
		$VARSET = WRITELINE(1, "....Done" + @CRLF)
	ELSE
		"..Failed"
		$VARSET = WRITELINE(1, "..Failed" + @CRLF)
		SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Report_Mapping_failure -server " + $EMAILSERVER)
		GOTO END
	ENDIF
? "Creating current date folder"
$VARSET = WRITELINE (1, "Creating current date folder")
MD "$ARCHIVEDRIVE@YEAR@MONTHNO@MDAYNO"
;MD "$ARCHIVEDRIVE@YEAR + right("0" + @MONTHNO,2) + right("0" + @MDAYNO,2)"
	IF @ERROR = 0
		"....Done"
		$VARSET = WRITELINE(1, "....Done" + @CRLF)
	ELSE
		"..Failed"
		$VARSET = WRITELINE(1, "..Failed" + @CRLF)
		SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Archive_directory_creation_failure -server " + $EMAILSERVER)
		GOTO END
	ENDIF
$VARSET = WRITELINE (1, "Compressing current KPI data on Report Drive")
Shell ("cmd.exe /c " + $SCRIPTROOT + "\rar.exe a -r -t -y " + $REPORTDRIVE + "\" + $ARCHIVE + ".Rar " + $REPORTDRIVE + "\*")
$VARSET = WRITELINE (1, "Copying report to current date folder")
COPY $REPORTDRIVE + "\" + $ARCHIVE + ".Rar" $ARCHIVEDRIVE + "\" + $ARCHIVE
	IF @ERROR = 0
		"....Done"
		$VARSET = WRITELINE(1, "....Done" + @CRLF)
	ELSE
		"..Failed"
		$VARSET = WRITELINE(1, "..Failed" + @CRLF)
		$VARSET = WRITELINE(1, GetDiskSpace ($ARCHIVEDRIVE) + @CRLF)
		SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Archive_Mapping_failure -server " + $EMAILSERVER)
		GOTO END
	ENDIF
;
;---- Compare copied filesizes
;
$REPORTFILE = GetFileSize ($REPORTDRIVE + $ARCHIVE + ".rar") /1024
$VARSET = WRITELINE (1, "Report RAR file size is " + $REPORTFILE + @CRLF)
$ARCHIVEFILE = GetFileSize ($ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE + ".rar") /1024
$VARSET = WRITELINE (1, "Archive rar file size is " + $ARCHIVEFILE + @CRLF)
If $REPORTFILE <> $ARCHIVEFILE
	SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s File_Copy_Error -server " + $EMAILSERVER)
ENDIF
;
;---- Extracting files onto Archive
;
$VARSET = WRITELINE (1, "Extracting " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE + ".rar" +  @CRLF)
Shell ("cmd.exe /c " + $SCRIPTROOT + "\unrar.exe e -r " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE ".rar " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" )
	IF @ERROR = 0
		"....Done"
		$VARSET = WRITELINE(1, "....Done" + @CRLF)
	ELSE
		"..Failed"
		$VARSET = WRITELINE(1, "..Failed" + @CRLF)
		$VARSET = WRITELINE(1, GetDiskSpace ($ARCHIVEDRIVE) + @CRLF)
		SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Issue_Extracting_Files_From_Archive -server " + $EMAILSERVER)
		GOTO END
	ENDIF
;
;---- Removing archive rar file and files from Report Drive
;
RD $REPORTDRIVE /Q
$VARSET = WRITELINE (1, "Removing archive RAR file and files from Report Drive" + @CRLF)
	IF @ERROR = 0
		"....Done"
		$VARSET = WRITELINE(1, "....Done" + @CRLF)
	ELSE
		"..Failed"
		$VARSET = WRITELINE(1, "..Failed" + @CRLF)
		SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Issue_removing_files_from_HR01 -server " + $EMAILSERVER)
		GOTO END
	ENDIF
;
;---- Email success report to maintaining group
;
SHELL ("cmd.exe /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $EmailAddress + " -f " + $Sender + " -s " + @DATE + "_KPI_Archive_Success -server " + $EMAILSERVER)
:END
;USE $REPORTDRIVE /DEL
;USE $ARCHIVEDRIVE /DEL


Mart
(KiX Supporter)
2006-12-04 12:40 PM
Re: Directory creation based on date issue

How about breaking that looooooong commented line so we can read the post on one screen instead of scrolling miles to the right \:\)

Pax
(Getting the hang of it)
2006-12-04 03:02 PM
Re: Directory creation based on date issue

Originally Posted By: Mart
How about breaking that looooooong commented line so we can read the post on one screen instead of scrolling miles to the right \:\)
It's that damn KixStarter I tells you...

fixed ;\)

Pax


NTDOCAdministrator
(KiX Master)
2006-12-05 02:59 AM
Re: Directory creation based on date issue

Well unless it is bad editing on your part the script HAS to fail.


PHP:
$REPORTUSER = domain1user $ARCHIVEUSER = domain2user $REPORTDRIVE = I: $ARCHIVEDRIVE = J:


Those vars need to be within quotes

I would also HIGHLY recommend that you place this type of header at the top of your script. It will force you to make sure you code cleanly.

PHP:
If Not @LogonMode Break On Else Break Off EndIf Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On') $SO=SetOption('WrapAtEOL','On')


Again, bad editing????

Code:
$USERPASS = password


Unless you REALLY have to, I would recode the script so that you don't keep issuing the WriteLine over and over. I would attempt to write the log one time when needed only with a single write.


You use a USE statement without checking if the drive is already mapped or not. You should check and verify.

You attempt to use NoVarsInStrings and then you go and do this:

Code:
MD "$ARCHIVEDRIVE@YEAR@MONTHNO@MDAYNO"


You're using GOTO END I would really recode and remove all GOTO statements.

You attempt to remove a file/dir without checking if it exists or if you're even in the correct path/location. Very easy to delete or RD an entire structure if LEN is not correct. i.e. You can start an RD from the root of a drive which will give you the opportunity to rebuild the system if something like that happens.

Please take a crack at making the suggested changes and let us know how it goes please.


NTDOCAdministrator
(KiX Master)
2006-12-05 03:03 AM
Re: Directory creation based on date issue

Oh... I see I mentioned it and you did not respond to it.

Again - HIGHLY recommend using KiXtart 4.53

This is NOT a logon script so I see no reason not to use the latest version of KiXtart.


Pax
(Getting the hang of it)
2006-12-05 01:00 PM
Re: Directory creation based on date issue

Originally Posted By: NTDOC
Well unless it is bad editing on your part the script HAS to fail.


PHP:
$REPORTUSER = domain1user $ARCHIVEUSER = domain2user $REPORTDRIVE = I: $ARCHIVEDRIVE = J:


Those vars need to be within quotes

The drive mappings have been working perfectly fine without using the quote marks up to this point. It is one reason I commented out the drive deletions in the END section so I can see what is being done when it fails.
Originally Posted By: NTDOC
I would also HIGHLY recommend that you place this type of header at the top of your script. It will force you to make sure you code cleanly.

PHP:
If Not @LogonMode Break On Else Break Off EndIf Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On') $SO=SetOption('WrapAtEOL','On')


Again, bad editing????

Code:
$USERPASS = password


Unless you REALLY have to, I would recode the script so that you don't keep issuing the WriteLine over and over. I would attempt to write the log one time when needed only with a single write.

I am using it for now so I can trouble shoot at what stage the script would fail providing there were no parsing errors, which currently there are. I shall consider moving the writeline events at a later stage once I know the script runs through with no errors. At the moment, it is part of my own error checking. As for the $USERPASS, no, this was for removing identifiable. Any testing I am doing is running as a locally logged on user as the user who runs the script(in the scheduled task) won't have the ability to logon to the machine, only to run the scheduled task.

Originally Posted By: NTDOC
You use a USE statement without checking if the drive is already mapped or not. You should check and verify.

You attempt to use NoVarsInStrings and then you go and do this:

Code:
MD "$ARCHIVEDRIVE\@YEAR@MONTHNO@MDAYNO"

I'll go back and review checking the drive mappings, thanks for the suggestion.

My original post in this thread was about why MD creates the directory how I wanted it created, yet pre-defining $ARCHIVE works differently, even through we are dealing with KiX internal commands and macros. The suggestions I've been given only gave a work around, and no reason why they seems to be dealt with differently, so I'm working only on what I know.

Originally Posted By: NTDOC
You're using GOTO END I would really recode and remove all GOTO statements.

You attempt to remove a file/dir without checking if it exists or if you're even in the correct path/location. Very easy to delete or RD an entire structure if LEN is not correct. i.e. You can start an RD from the root of a drive which will give you the opportunity to rebuild the system if something like that happens.

Please take a crack at making the suggested changes and let us know how it goes please.

What would you suggest to use instead of GOTO statements. If there is a failure in one section I don't want the entire script to continue running when I know it will fail.

I could move the failure condition commands to the a seperate section called FAIL to recode reused code, which would include the WRITELINE commands, but once again, this would require GOTO, unless there is a more elegant way to deal with this. I wouldn't say I am a great scripter, but this is all I know at the moment.

I have taken your comments on board about RD, but I've been told that the files, etc I have to move are a series of files, directories and subdirectories, and are dymanic, so this is the best way I can see of dealing with it, mapping to the root of the store, so using RD won't remove anything above this level, and I do have to remove all contents of the "dump" directory.

Should I use a variation of the DIR command for this instead? I don't know how this deals with directories and subdirectories as I've never used it, and only seen it referenced briefly in the forums,

Pax


Pax
(Getting the hang of it)
2006-12-05 01:01 PM
Re: Directory creation based on date issue

Originally Posted By: NTDOC
Well unless it is bad editing on your part the script HAS to fail.


PHP:
$REPORTUSER = domain1user $ARCHIVEUSER = domain2user $REPORTDRIVE = I: $ARCHIVEDRIVE = J:


Those vars need to be within quotes

The drive mappings have been working perfectly fine without using the quote marks up to this point. It is one reason I commented out the drive deletions in the END section so I can see what is being done when it fails.
Originally Posted By: NTDOC
I would also HIGHLY recommend that you place this type of header at the top of your script. It will force you to make sure you code cleanly.

PHP:
If Not @LogonMode Break On Else Break Off EndIf Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On') $SO=SetOption('WrapAtEOL','On')


Again, bad editing????

Code:
$USERPASS = password


Unless you REALLY have to, I would recode the script so that you don't keep issuing the WriteLine over and over. I would attempt to write the log one time when needed only with a single write.

I am using it for now so I can trouble shoot at what stage the script would fail providing there were no parsing errors, which currently there are. I shall consider moving the writeline events at a later stage once I know the script runs through with no errors. At the moment, it is part of my own error checking. As for the $USERPASS, no, this was for removing identifiable. Any testing I am doing is running as a locally logged on user as the user who runs the script(in the scheduled task) won't have the ability to logon to the machine, only to run the scheduled task.

Originally Posted By: NTDOC
You use a USE statement without checking if the drive is already mapped or not. You should check and verify.

You attempt to use NoVarsInStrings and then you go and do this:

Code:
MD "$ARCHIVEDRIVE\@YEAR@MONTHNO@MDAYNO"

I'll go back and review checking the drive mappings, thanks for the suggestion.

My original post in this thread was about why MD creates the directory how I wanted it created, yet pre-defining $ARCHIVE works differently, even through we are dealing with KiX internal commands and macros. The suggestions I've been given only gave a work around, and no reason why they seems to be dealt with differently, so I'm working only on what I know.

Originally Posted By: NTDOC
You're using GOTO END I would really recode and remove all GOTO statements.

You attempt to remove a file/dir without checking if it exists or if you're even in the correct path/location. Very easy to delete or RD an entire structure if LEN is not correct. i.e. You can start an RD from the root of a drive which will give you the opportunity to rebuild the system if something like that happens.

Please take a crack at making the suggested changes and let us know how it goes please.

What would you suggest to use instead of GOTO statements. If there is a failure in one section I don't want the entire script to continue running when I know it will fail.

I could move the failure condition commands to the a seperate section called FAIL to recode reused code, which would include the WRITELINE commands, but once again, this would require GOTO, unless there is a more elegant way to deal with this. I wouldn't say I am a great scripter, but this is all I know at the moment.

I have taken your comments on board about RD, but I've been told that the files, etc I have to move are a series of files, directories and subdirectories, and are dymanic, so this is the best way I can see of dealing with it, mapping to the root of the store, so using RD won't remove anything above this level, and I do have to remove all contents of the "dump" directory.

Should I use a variation of the DIR command for this instead? I don't know how this deals with directories and subdirectories as I've never used it, and only seen it referenced briefly in the forums,

Pax


NTDOCAdministrator
(KiX Master)
2006-12-06 03:51 AM
Re: Directory creation based on date issue

Well the reason your code doesn't supply the dates you're wanting is that it is adding the values together.

Here is some code that will give you the DATE and TIME into your var.


PHP:
Dim $Archive $Archive = Trim(Join(Split(@DATE,'/'),'-'))+'_'+Trim(Join(Split(@TIME,':'),'')) 'Archive Date for var is: ' + $Archive ?


.


NTDOCAdministrator
(KiX Master)
2006-12-06 03:57 AM
Re: Directory creation based on date issue

I have confirmed that you can seemingly use STRINGS without quotes for some reason. I would list that as a BUG but will wait for input from some of the other long term coders to get their input.

From the user manual:

Quote:

When using KiXtart, note the following rules:
  • Strings can contain any characters, except the \0 (NULL) and \x1a (end of file) characters.
  • Script commands should be separated by white space — that is, any combination of spaces, tabs, or new line characters.
  • Strings must be enclosed in quotation marks.

    For example:
    'String with a dash (-) in it.' ; String with a dash (-) in it.



.


NTDOCAdministrator
(KiX Master)
2006-12-06 04:58 AM
Re: Directory creation based on date issue

Okay here is an UNTESTED semi rewrite of your code.
Please review the code and update/modify/change as needed to function properly in your environment.

PHP:
;:KVIS:Edited on 04/12/2006 09:38:57 by Pax.|E:KVIE: ; This is a test script for moving the archive data ; ;-------------------------------------------------------------------------------------------------------------------- ; ;---- Script version 0.2 ;---- Created by Pax ;---- Last edit date 04/12/2006 by Pax ;---- Last edit date 12/5/2006 by NTDOC ; ;---- Declare Variables ------- ; If Not @LogonMode Break On Else Break Off EndIf Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On') $SO=SetOption('WrapAtEOL','On') Dim $SCRIPTROOT, $REPORTSTORE, $ARCHIVESTORE, $REPORTUSER, $ARCHIVEUSER, Dim $REPORTDRIVE, $ARCHIVEDRIVE, $ARCHIVE, $ARCHIVELOG Dim $EmailServer, $Sender, $SupportEmail, $EmailAddress Dim $USERPASS $SCRIPTROOT = '\\server1\path\' $REPORTSTORE = 'server2path' $ARCHIVESTORE = 'server3path' $REPORTUSER = 'domain1user' $ARCHIVEUSER = 'domain2user' $REPORTDRIVE = 'I:' $ARCHIVEDRIVE = 'J:' $ARCHIVE = Trim(Join(Split(@DATE,'/'),'-'))+'_'+Trim(Join(Split(@TIME,':'),'')) $ARCHIVE ? $ARCHIVELOG = $ARCHIVEDRIVE + 'LOGS' + $ARCHIVE + ".LOG" $ARCHIVELOG ? $EmailServer = 'our.emailserver.net' $Sender = 'sender@@emailserver.net' $SupportEmail = 'support@@emailserver.net' $EmailAddress = 'recipient@@emailserver.net' ; ;---- If the password for the REPORTUSER account changes on server2, change the password on the ARCHIVEUSER account, ;---- and update the password in this script. Without this the script will fail ; $USERPASS = 'password' ; ;---- Mapping Archive drive first to create log file ; "Mapping the Archive Drive" ? Dim $Err, $SErr USE $ARCHIVEDRIVE + $ARCHIVESTORE /USER:$ARCHIVEUSER /PASSWORD:$USERPASS $Err = @ERROR $SErr = @SERROR IF @ERROR = 0 '....Done' ? ELSE '..Failed: ' + $SErr ? SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Archive_Mapping_failure -server " + $EMAILSERVER) Quit $Err ENDIF ; Dim $VARSET $VARSET = OPEN(1, $ARCHIVESTORE + 'LOGS' + $ARCHIVE + '.LOG', 5) @DATE ? $VARSET = WRITELINE (1, 'The current date is ' + @DATE + @CRLF) ; ;---- Do drive mapping for Report drive to move the data ; $VARSET = WRITELINE (1, 'Mapping the Report Drive') Dim $Err, $SErr USE $REPORTDRIVE + $REPORTSTORE /USER:$REPORTUSER /PASSWORD:$USERPASS $Err = @ERROR $SErr = @SERROR IF @ERROR = 0 "....Done" ? $VARSET = WRITELINE(1, '....Done' + @CRLF) ELSE "..Failed: " + $SErr ? $VARSET = WRITELINE(1, '..Failed' + @CRLF) SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Report_Mapping_failure -server " + $EMAILSERVER) Quit $Err ENDIF "Creating current date folder" ? $VARSET = WRITELINE (1, 'Creating current date folder') Dim $Err, $SErr MD $ARCHIVEDRIVE + '' + $ARCHIVE $Err = @ERROR $SErr = @SERROR IF @ERROR = 0 '....Done' ? $VARSET = WRITELINE(1, '....Done' + @CRLF) ELSE '..Failed: ' + $SErr ? $VARSET = WRITELINE(1, '..Failed' + @CRLF) SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Archive_directory_creation_failure -server " + $EMAILSERVER) Quit $Err ENDIF $VARSET = WRITELINE (1, 'Compressing current KPI data on Report Drive') Shell ("%comspec% /c " + $SCRIPTROOT + "\rar.exe a -r -t -y " + $REPORTDRIVE + "\" + $ARCHIVE + ".Rar " + $REPORTDRIVE + "\*") $VARSET = WRITELINE (1, 'Copying report to current date folder') Dim $Err, $SErr COPY $REPORTDRIVE + '' + $ARCHIVE + '.Rar' $ARCHIVEDRIVE + '' + $ARCHIVE $Err = @ERROR $SErr = @SERROR IF @ERROR = 0 '....Done' ? $VARSET = WRITELINE(1, '....Done' + @CRLF) ELSE '..Failed: ' + $SErr ? $VARSET = WRITELINE(1, '..Failed' + @CRLF) $VARSET = WRITELINE(1, GetDiskSpace ($ARCHIVEDRIVE) + @CRLF) SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Archive_Mapping_failure -server " + $EMAILSERVER) Quit $Err ENDIF ; ;---- Compare copied filesizes ; $REPORTFILE = GetFileSize ($REPORTDRIVE + '' + $ARCHIVE + '.rar') /1024 $VARSET = WRITELINE (1, 'Report RAR file size is ' + $REPORTFILE + @CRLF) $ARCHIVEFILE = GetFileSize ($ARCHIVEDRIVE + '' + $ARCHIVE + '' + $ARCHIVE + '.rar') /1024 $VARSET = WRITELINE (1, 'Archive rar file size is ' + $ARCHIVEFILE + @CRLF) If $REPORTFILE <> $ARCHIVEFILE SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s File_Copy_Error -server " + $EMAILSERVER) ENDIF ; ;---- Extracting files onto Archive ; $VARSET = WRITELINE (1, 'Extracting ' + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE + ".rar" + @CRLF) Dim $Err, $SErr Shell ("%comspec% /c " + $SCRIPTROOT + "\unrar.exe e -r " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" + $ARCHIVE ".rar " + $ARCHIVEDRIVE + "\" + $ARCHIVE + "\" ) $Err = @ERROR $SErr = @SERROR IF @ERROR = 0 '....Done' ? $VARSET = WRITELINE(1, '....Done' + @CRLF) ELSE '..Failed: ' + $SErr ? $VARSET = WRITELINE(1, '..Failed' + @CRLF) $VARSET = WRITELINE(1, GetDiskSpace ($ARCHIVEDRIVE) + @CRLF) SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Issue_Extracting_Files_From_Archive -server " + $EMAILSERVER) Quit $Err ENDIF ; ;---- Removing archive rar file and files from Report Drive ; If LEN($REPORTDRIVE)>2 Dim $Err, $SErr RD $REPORTDRIVE /Q $VARSET = WRITELINE (1, 'Removing archive RAR file and files from Report Drive' + @CRLF) $Err = @ERROR $SErr = @SERROR IF @ERROR = 0 '....Done' ? $VARSET = WRITELINE(1, '....Done' + @CRLF) ELSE '..Failed: ' + $SErr ? $VARSET = WRITELINE(1, '..Failed' + @CRLF) SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $SupportEmail + " -f " + $Sender + " -s Issue_removing_files_from_HR01 -server " + $EMAILSERVER) Quit $Err ENDIF Else 'The REPORTDRIVE var was empty so did not write' ? EndIf ; ;---- Email success report to maintaining group ; SHELL ("%comspec% /c " + $SCRIPTROOT + "Blat " + $ARCHIVELOG + " -t " + $EmailAddress + " -f " + $Sender + " -s " + @DATE + "_KPI_Archive_Success -server " + $EMAILSERVER)


.


Pax
(Getting the hang of it)
2006-12-06 10:39 AM
Re: Directory creation based on date issue

Originally Posted By: NTDOC
Okay here is an UNTESTED semi rewrite of your code.
Please review the code and update/modify/change as needed to function properly in your environment.


Hey NTDOC,

Thanks for that. Most unexpected, and I do say I'm quite shocked that you'd spend the time on it. Thank you muchly for your time.

I will go over it now and compare to what I've got now to see what I can learn from it. There are quite a few functions I've never used before, so will have to read up on what they do.

5 * for you \:\)

Pax


Pax
(Getting the hang of it)
2006-12-06 11:44 AM
Re: Directory creation based on date issue

Just had another thought on your suggestion about the WRITELINEs I seem to be doing everywhere.

Would it be a better choice to open the logfile, and immediately below it use
PHP:
RedirectOutput($ARCHIVELOG)

and remove the WRITELINE commands? This would mean I could capture the output from Blat, RAR and UNRAR directly to the log file, as I think this would be served well in the file.

The benefits would be that if there are problems, then I can just comment out that line and see it on screen running it manually. Also the script would be smaller and easier to diagnose if problem turn up instead of having screen output and log file output seperately?

Ideas?

Pax


Mart
(KiX Supporter)
2006-12-06 12:26 PM
Re: Directory creation based on date issue

RedirectOutput will send all screen output to the log file. So if you do not output the stuff from a function or command to the screen it will not show in the log file.

Pax
(Getting the hang of it)
2006-12-06 12:46 PM
Re: Directory creation based on date issue

Originally Posted By: Mart
RedirectOutput will send all screen output to the log file. So if you do not output the stuff from a function or command to the screen it will not show in the log file.


No that's fine, the script is going to run as a scheduled task, so won't be seen by anyone anyway.

If this means I won't need to double up on writing to the screen and log seperately and it will capture all SHELL run programs' output, then this is what I want.

I just wanted to check that this is the case, and then if I have to run it manually I can comment out the RedirectOutput line and see everything to screen to troubleshoot.

I know that if there is the something I specifically want to appear on screen I can do a RedirectOutput("") before it, and then return to dumping to the logfile using RedirectOutput($ARCHIVELOG) after the section I want on screen.

Pax


Glenn BarnasAdministrator
(KiX Supporter)
2006-12-06 01:46 PM
Re: Directory creation based on date issue

If you don't fix the date string to include leading zeros as Allen suggested, you won't be able to tell the difference betweeen 2007121 (January 21) and 2007121 (December 1), assuming you keep your archives that long. Also, it makes the dates all 8 char strings, not something between 6 and 8, which makes sorting easier.

Glenn


Pax
(Getting the hang of it)
2006-12-06 02:09 PM
Re: Directory creation based on date issue

Originally Posted By: Glenn Barnas
If you don't fix the date string to include leading zeros as Alan suggested, you won't be able to tell the difference betweeen 2007121 (January 21) and 2007121 (December 1), assuming you keep your archives that long. Also, it makes the dates all 8 char strings, not something between 6 and 8, which makes sorting easier.

Glenn
Hey Glenn,

I've taken the script NTDOC (Is this Alan?) and he redefined the $Archive value correctly, and this value is used to create the directory and to move, create and expand the files correctly.

We are only keeping the files for 3 months, but since the issue is resolved, it should not pose a problem.

Pax


NTDOCAdministrator
(KiX Master)
2006-12-06 08:02 PM
Re: Directory creation based on date issue

Glenn,

I gave him this code to use.

PHP:
Dim $Archive $Archive = Trim(Join(Split(@DATE,'/'),'-'))+'_'+Trim(Join(Split(@TIME,':'),'')) 'Archive Date for var is: ' + $Archive ?


On my system the DATE is 2 character even for months 1 - 9

.


Glenn BarnasAdministrator
(KiX Supporter)
2006-12-07 01:05 AM
Re: Directory creation based on date issue

Yeah, understood, but I didn't see anyone give reasons why the date format is important. Allen came close with his example, but - again - examples are better when you describe WHY you do something a certain way, especially in the scripts or this Starters forum. Other people will read these messages and learn, too, so - it's not just Pax who will benefit.

Just my (not so humble) opinion. ;\)

Glenn


AllenAdministrator
(KiX Supporter)
2006-12-07 01:17 AM
Re: Directory creation based on date issue

Who is this Alan character?

Les
(KiX Master)
2006-12-07 01:28 AM
Re: Directory creation based on date issue

Looks like we might have at least seven members that might be named Alan.
http://www.kixtart.org/forums/ubbthreads.php?ubb=showmembers&sb=1&like=a&page=7

Maybe Ron's middle name is Alan.


NTDOCAdministrator
(KiX Master)
2006-12-07 02:35 AM
Re: Directory creation based on date issue

Okay Glenn - here is some explanation of what I'm doing in the code per your suggestion.



@DATE
Returns this for me: 2006/12/06
@TIME
Returns this for me: 17:24:16


So we use the function SPLIT and assign it to our variable (in this case $Archive)
Quote:

SPLIT
Returns a zero-based, one-dimensional array containing a specified number of substrings.
Syntax: Split (“string”, “delimiter”, count)


We use the delimiter of / in the code: Split(@DATE,'/') which basically removes the /

We then use the JOIN function to replace the / with a - (because a / on Windows is not a valid file/folder name)
Quote:

JOIN
Creates a string by concatenating the elements of an array.
Syntax: Join (array, “delimiter”, count)


Then to get the TIME we do similar but this time we use the : as the delimiter for SPLIT and then replace it with nothing.
If we did not use the JOIN function then our var would still contain an array which is not as easily used for what we're trying to do here.
I use the +'_'+ to place the _ character between the DATE and TIME

For my system I have the dates display 2 characters for all months and days so this coding works well for me.
If your system returns single characters for months, or days 1 - 9 then you should be able to manage that in the Control Panel settings of your system.

Although according to the manual the KiXtart @DATE will return the date in this format every time: Date (in the format YYYY/MM/DD)



.


Glenn BarnasAdministrator
(KiX Supporter)
2006-12-07 04:24 AM
Re: Directory creation based on date issue

What on earth are you talking about, Allen? ;\)

Glenn BarnasAdministrator
(KiX Supporter)
2006-12-07 04:30 AM
Re: Directory creation based on date issue

Sweet, Doc!

The original code Pax used employed the individual date macros, which don't have leading zeros. That's why Allen padded the string with leading zeros and then took the rightmost 2 chars - changing "1" to "01", for example.

By using the @DATE with Join(Split()) functions, you already have 2-digit values for month and day. Once again, ya illustrate that there's more than one (right) way to do things. \:\)

Glenn


AllenAdministrator
(KiX Supporter)
2006-12-07 06:22 AM
Re: Directory creation based on date issue

Originally Posted By: Glenn Barnas
What on earth are you talking about, Allen? ;\)


hmmm... missed this one? ;\)
http://www.kixtart.org/forums/ubbthreads...true#Post170560

{edit: Ack! its worse than I thought ;\) }
http://www.kixtart.org/forums/ubbthreads...true#Post133850


LonkeroAdministrator
(KiX Master Guru)
2006-12-07 06:31 AM
Re: Directory creation based on date issue

lol.

Les
(KiX Master)
2006-12-07 06:49 AM
Re: Directory creation based on date issue

Didn't Pax call DOC Alan?
Quote:
I've taken the script NTDOC (Is this Alan?)

Will the real Alan please step forward?


Les
(KiX Master)
2006-12-07 06:53 AM
Re: Directory creation based on date issue

All these name confusions... How... er, Rich... er, Les... er, Alan? I just can't work another "er" into Alan. \:\(

Pax
(Getting the hang of it)
2006-12-07 11:16 AM
Re: Directory creation based on date issue

Would it be possible to steer clear of which Alan/Allen/Allan/Alen is which for 2.5 seconds, and I was getting confused as to where this thread was going?? I understand what NTDOC did, but yes explaining it for the masses who don't understand what I do now is a great benefit for this thread.

Back to post #171095
Quote:
I just had another thought on your suggestion about the WRITELINEs I seem to be doing everywhere.

Would it be a better choice to open the logfile, and immediately below it use
PHP:
RedirectOutput($ARCHIVELOG)

and remove the WRITELINE commands? This would mean I could capture the output from Blat, RAR and UNRAR directly to the log file, as I think this would be served well in the file.

The benefits would be that if there are problems, then I can just comment out that line and see it on screen running it manually. Also the script would be smaller and easier to diagnose if problem turn up instead of having screen output and log file output seperately?

Ideas?

Pax


I got a comment confirming that RedirectOutput does mean nothing will appear on the console, which is fine as this will run as a scheduled task, BUT, is this the better method than having comments and errors echoing to screen, and again writing them to the file.

It would just down on the script size, and possible mean less errors as there is less code for me to leave out a rogue ",),; mark??

Pax

EDIT: Answered my own questions partially by trying it, but I still have the problem with redirecting RAR and UNRAR output as using >> tell me that it doesn't understand the command, so I'm exploring using the option -ierr which re-directs to STDERR.

It will need refining more, but I'll post here when I am happy enough for it to go into production.

Because I am trying to email the log file using blat, I have found that I have to do a RedirectOutput("") before emailing the file to close the file, otherwise the file cannot be accessed to it being help open. It is an issue with blat, but at least I can work around it.


Witto
(MM club member)
2006-12-07 03:20 PM
Re: Directory creation based on date issue

the @-sign in strings should not be doubled if NoMacrosInStrings is On
Code:
...
$SO=SetOption('NoMacrosInStrings','On')
...
$Sender = 'sender@emailserver.net'
$SupportEmail = 'support@emailserver.net'
$EmailAddress = 'recipient@emailserver.net'
...


Pax
(Getting the hang of it)
2006-12-07 04:06 PM
Re: Directory creation based on date issue

Originally Posted By: Witto
the @-sign in strings should not be doubled if NoMacrosInStrings is On

Yeah I know, was part of me "testing" NTDOC's script and integrating it into my original.

I found another few syntax issues throughout which I've corrected \:\)

Thanks for pointing that out though,

Pax


Björn
(Korg Regular)
2006-12-07 04:32 PM
Re: Directory creation based on date issue

wow. A bunch of replies. Still
Code:
Shell ("monkey")


????
reeemove the brackets. it's shell "monkey" ;\)

(
Code:
Shell "monkey"
)

Second, what's up with using the php-enclosures? and third, what's up with not cathing the linebreaks- thus copy'n'paste turns into a mess *_*


NTDOCAdministrator
(KiX Master)
2006-12-07 09:12 PM
Re: Directory creation based on date issue

1. I'm the one using the PHP tag a lot as it looks and formats nice on the board.

2. I would use store what I want to see in a log file to VARS then write at the end.

3. In order to see the output from RAR you would need to launch from %comspec% first and then make sure you end the RAR command, then redirect.

4. Sorry for the "syntax" errors, but I did not write the whole script as though it were going to be in production by myself.

5. Sorry to go "off topic" but pretty much it was assumed your concerns were answered and the rest was in good fun (this is not a business and all are here as volunteers).

6. The explanation is "ON TOPIC" regardless of other noise in the post.

7. If you have other questions please let us know.

8. LOOSEN up bit - you got a lot of good free advice that would be difficult to come by without the efforts of other members on the board.

9. NTDOC should also loosen up a bit - sorry but your tone sounded a bit harsh and struck a cord with me I suppose. Sorry

10. HAPPY HOLIDAYS


.


Glenn BarnasAdministrator
(KiX Supporter)
2006-12-07 09:37 PM
Re: Directory creation based on date issue

Allen,

My deepest, most humble apology.

Gleeen


AllenAdministrator
(KiX Supporter)
2006-12-09 09:35 PM
Re: Directory creation based on date issue

No apologies necesary... just messin with ya.

Take care Glyn