#139398 - 2005-05-09 09:36 AM
Question on how to ignore a CRLF
|
viking
Fresh Scripter
Registered: 2005-05-06
Posts: 12
|
Hello, In the following script I would like to write to a file just the directory names of a given directory structure. The output file includes on the top always a CRLF which I try to eleminate. Does anyone have an idea how to solve this? Thanks a lot in advance.
Code:
$BaseDir = Dir("C:\WINNT\") $Name = Dir("$BaseDir")
If Exist ("KATEGORIEN.TXT") Del "KATEGORIEN.TXT" EndIf
While $Name <> "" And @ERROR = 0
If ($Name <> ".") And ($Name <> "..") And ($Name <> @CRLF) And (GetFileAttr($basedir+"\"+$name) & 16) ? $name EndIf
$Name = Dir() $RC=RedirectOutput("KATEGORIEN.TXT")
Loop
|
|
Top
|
|
|
|
#139399 - 2005-05-09 10:11 AM
Re: Question on how to ignore a CRLF
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Code:
Break ON $=SetOption("Explicit","ON") $=SetOption("WrapAtEOL","ON") $=SetOption("ASCII","ON") Dim $sBaseDir,$sDirEnt $sBaseDir="C:\WINNT\" $=RedirectOutput("KATEGORIEN.TXT",1) $sDirEnt=Dir($sBaseDir) While $sDirEnt <> "" And @ERROR = 0 If ($sDirEnt <> ".") And ($sDirEnt <> "..") And (GetFileAttr($sBaseDir+"\"+$sDirEnt) & 16) $sDirEnt+@CRLF EndIf $sDirEnt = Dir() Loop $=RedirectOutput("")
Look at FreeFileHandle(), Open(), WriteLine() and Close() for better methods of writing file data.
|
|
Top
|
|
|
|
#139400 - 2005-05-09 11:15 AM
Re: Question on how to ignore a CRLF
|
viking
Fresh Scripter
Registered: 2005-05-06
Posts: 12
|
Hello Richard, thanks a lot for the very fast and great help; works great!!!  Before I have seen the RedirectOutput command I tried it with the open and writeline commands; but I got just an output file with no entries. Below you can see the code with FileHandle, Open, Writeline and the Close. Not sure what I'm doing wrong. Thanks, Bjoern
Code:
Break ON
$=SetOption("Explicit","ON") $=SetOption("WrapAtEOL","ON") $=SetOption("ASCII","ON") Dim $sBaseDir Dim $sDirEnt Dim $Handle
$Handle = FreeFileHandle() $sBaseDir="C:\WINNT\"
Open($Handle, "KATEGORIEN.TXT", 1) $sDirEnt=Dir($sBaseDir)
While $sDirEnt <> "" And @ERROR = 0
If ($sDirEnt <> ".") And ($sDirEnt <> "..") And (GetFileAttr($sBaseDir+"\"+$sDirEnt) & 16) WriteLine($Handle, $sDirEnt+@CRLF) EndIf $sDirEnt = Dir() Loop
Close($Handle)
|
|
Top
|
|
|
|
#139401 - 2005-05-09 02:41 PM
Re: Question on how to ignore a CRLF
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
You are so very very close.
You just needed to specify that the file is opened in "write" mode.
I've updated you script, adding some error checking and comments. Code:
Break ON $=SetOption("Explicit","ON") $=SetOption("WrapAtEOL","ON") $=SetOption("ASCII","ON") Dim $sBaseDir Dim $sDirEnt Dim $Handle Dim $sOutput $Handle = FreeFileHandle() $sOutput="KATEGORIEN.TXT" $sBaseDir="C:\WINNT\" ; WriteLine() will *always* append to an existing file, so you must ; explicitly delete it if you want to overwrite an existing file. If Exist($sOutput) Del $sOutput EndIf ; Mode "1" will create a file if it does not exist, but you will also need ; to specify that you want write access otherwise the file will be opened in ; the default read access mode. Write mode is "4" - see Open() in the manual ; for all acceptable modes. If Open($Handle, $sOutput, 1+4) ; Handle file open errors "Sorry, could not open '"+$sOutput+"' for writing."+@CRLF "Error: ["+@ERROR+"] "+@SERROR+@CRLF Exit @ERROR EndIf $sDirEnt=Dir($sBaseDir) While $sDirEnt <> "" And @ERROR = 0 If ($sDirEnt <> ".") And ($sDirEnt <> "..") And (GetFileAttr($sBaseDir+"\"+$sDirEnt) & 16) $=WriteLine($Handle, $sDirEnt+@CRLF) EndIf $sDirEnt = Dir() Loop ; If you are not interested in the return value of a function, silence the "0" or "1" that would ; appear on the screen by assigning it to a dummy variable. Most people use the "$" variable. $=Close($Handle) Exit 0
|
|
Top
|
|
|
|
#139402 - 2005-05-09 03:46 PM
Re: Question on how to ignore a CRLF
|
viking
Fresh Scripter
Registered: 2005-05-06
Posts: 12
|
Richard, thanks a lot !  Bjoern
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|