Tesdall
(Getting the hang of it)
2010-09-14 07:10 PM
Redirectoutput

Im trying to pipe informaton out to a text file and have it working in a BAT file. BUt im trying to convert it to a KIX script. Please let me know what im doing wrong.
 Code:
Break on

DIM $server
Dim $settings
DIm $CD

$Server = server1
$Settings = server2
$OS = @PRODUCTTYPE

IF $OS = 'Windows XP Professional'

? "Change CD-ROM Drive Letter"
shell "diskpart.exe /s \\" + $Settings + "\it\newcomputer\Driveletter\chkdisk.txt"
? "Enter Volume Number for CDROM: "
Gets $CD

IF REDIRECTOUTPUT("\\" + $Settings + "\it\newcomputer\driveletter\chgdisk.txt",1) = 0
? "select Volume $CD"
? "assign letter Q noerr"
ENDIF

shell "diskpart.exe /s \\" + $Settings + "\it\newcomputer\Driveletter\chgdisk.txt"


Batch file
 Code:
Echo Change CD-ROM Drive Letter
diskpart.exe /s "\\server2\it\tesdall\driveletter\chkdisk.txt"
set /p number= Enter Volume Number for CDROM:
Echo select disk 1 > "\\server2\it\newcomputer\driveletter\chgdisk.txt"
Echo select Volume %number% >> "\\usserve2\it\newcomputer\driveletter\chgdisk.txt"
Echo assign letter Q noerr>> "\\server2\it\newcomputer\driveletter\chgdisk.txt"
diskpart.exe /s "\\server2\it\newcomputer\driveletter\chgdisk.txt"


If i look in the chgdisk.txt file it shows things from farther down in the script, which is weird to me. Since, i though it would be stopping at the Endif.


Tesdall
(Getting the hang of it)
2010-09-14 08:03 PM
Re: Redirectoutput

Bahhhh apparently that command doesn't do what i think it should do. So i changed my script. Everything appears to work now.

Here is the script that works (for me)
 Code:
Break on

DIM $server
Dim $settings
DIm $CD
Dim $outfile
DIM $Writeline

$Server = server1
$Settings = server2
$OS = @PRODUCTTYPE
$OutFile = ('\\$Settings\it\newcomputer\driveletter\chgdisk.txt')

; Test open the output file
If Open( 2 , $OutFile, 5) <> 0
  'Failed to open ' $OutFile ' - aborting!' ?
  Exit 1
EndIf

IF $OS = 'Windows XP Professional'

? "Change CD-ROM Drive Letter"
shell "diskpart.exe /s \\" + $Settings + "\it\newcomputer\Driveletter\chkdisk.txt"
? "Enter Volume Number for CDROM: "
Gets $CD
$Writeline = Writeline(2, "Select Volume " + $CD + @CRLF + "Assign Letter Q noerr")
shell "diskpart.exe /s \\" + $Settings + "\it\newcomputer\Driveletter\chgdisk.txt"
$Writeline = close(2)
DEL '\\$settings\it\newcomputer\Driveletter\chgdisk.txt'


I should add this is a going to be a very large script. That is the reason it doesn't have the ELSE, endif and exit stuff at the bottom.

Hope this helps someone else.


Glenn BarnasAdministrator
(KiX Supporter)
2010-09-14 08:05 PM
Re: Redirectoutput

Couple of things I see right away..

You aren't clearing the file, so every time you run the script, it will keep appending to the output file. Start by deleting the output file before the first RedirectOutput command. Your BAT file does clear the file, as it starts with ">" and continues with ">>".

Second, you aren't closing the redirection. Before the EndIf, add
$ = RedirectOutput('')
to terminate the redirection. Kix will continue to append all output to the file until you close it.

Finally, putting "?" at the start of a line is usually a matter of preference, but there are times it can bite you. Unlike BASIC, it is not a shortcut for the PRINT statement, but actually a shortcut to @CRLF. This character sequence naturally comes at the end of lines, not at the beginning. When doing file redirection, it causes your files to begin with a blank line and end without a CR/LF sequence. This can affect certain types of input files, especially those used as command input to other appls (ie: diskpart). Not saying it IS a problem, but it COULD become one if you aren't careful.

I rarely use "?" anymore, and always place it or @CRLF at the end of lines for this reason.

Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2010-09-14 08:09 PM
Re: Redirectoutput

 Originally Posted By: Tesdall
Bahhhh apparently that command doesn't do what i think it should do.
Clearly, you need to change your thinking, not your script! \:D

RedirectOutput is very easy if you remember 2 things:
1. It ALWAYS APPENDS - you need to delete any prior file manually.
2. It keeps going until you shut it up/off.

I almost never use Open/WriteLine for log output because RedirectOutput is so much easier. Yeah, I still get bit every once in a while. ;\) Then I remember the 2 rules and fix my process.

Glenn

PS - I'm stealing, er - yeah - stealing! that CDROM rename code!


Tesdall
(Getting the hang of it)
2010-09-14 08:36 PM
Re: Redirectoutput

Im glad i could help. I am using the Manual to look up the commands and a lot of them dont have the "close command" for that command.