Edit
Attach
Printable
topic end
<!-- * Set TOPICTITLE = <nop>CoCreate Modeling code examples: Enumerating all drives on a Windows system --> <style type="text/css"> pre { background-color:#FFEECC; } </style> ---++ <nop>CoCreate Modeling: Enumerating all drives on a Windows system Even though Common Lisp has very broad functionality, sometimes you need to get a little help from your friends to accomplish a task. Let's say we want to enumerate all available drives on a Windows system. Neither Common Lisp nor <nop>CoCreate Modeling's Developer's Kit have any built-in inquiries for this purpose - but a solution can be built easily by glueing together an external Windows shell script and a short Lisp function! The following example will demonstrate important techniques in getting this done. An abundance of scripting solutions is available for the Windows platform: * [[http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/findstr.mspx][Batch programming]] using DOS command syntax. Highly underrated, probably because of its heritage and its peculiar syntax, but actually [[http://home7.inet.tele.dk/batfiles/][very powerful]], at least in Windows NT/2000/XP! Best of all: The command shell is available everywhere, so no special installation steps are necessary. * Platform-independent scripting languages such as [[http://www.perl.org][Perl]] or [[http://www.python.org/][Python]] * UNIX-like shell scripting languages, such as ksh or [[http://www.cygwin.com][bash]] * Last, but not least, scripting languages supported by the [[http://www.microsoft.com/scripting][Windows Scripting Host]]. In our example, we'll use the Windows Scripting Host to execute the following short script in <nop>VBScript syntax: <pre> set fs=CreateObject("Scripting.FileSystemObject") for each drive in fs.drives WScript.Echo(drive.DriveLetter) next </pre> In <nop>VBScript, it is possible to instantiate COM components such as the [[http://msdn.microsoft.com/library/en-us/script56/html/jsobjFileSystem.asp][<nop>FileSystemObject]], which provides functionality to enumerate drives, inquire drive properties and drill into a filesystem. Exactly what we need! Save this script into a file and then try running it using =cscript=: <pre> c:\home\clausb\>cscript /nologo drives.vbs A C D E </pre> Looks good! Now we have the drive information and only need to get it across to LISP. The second part of the solution is the following LISP code: <pre> (defun listdrives() (let (l (s (open "| cscript /nologo drives.vbs" :direction :input))) (prog1 (loop while (setf l (read-line s nil)) collecting (remove #\Return l)) (close s)))) </pre> The file name parameter syntax for the =open= command probably caught your eye: The "file name" starts with a pipe (=|=) character. This means that what follows is actually a command and not a file. With this syntax, the specified command is executed, and its output is then piped into the LISP stream =s= from where it can be read line by line using =read-line=. The return value of =listdrives= is a list containing the drive letters of all available drives. To build that list, the code above uses the [[http://www.lisp.org/HyperSpec/Body/mac_loop.html][loop]] statement's result accumulation capabilities ([[http://www-2.cs.cmu.edu/Groups/AI/html/cltl/clm/node246.html][collecting]]). -- Main.ClausBrod - 13 Feb 2005 Much nicer and shorter version of =listdrives=: <pre> (defun listdrives() (with-open-file (s "| cscript /nologo drives.vbs" :direction :input) (loop for l = (read-line s nil) while l collecting (remove #\Return l)))) </pre> -- Main.ClausBrod - 18 Feb 2009 %COMMENT{type="below" nonotify="on"}%
to top
End of topic
Skip to action links
|
Back to top
Edit
|
Attach image or document
|
Printable version
|
Raw text
|
Refresh
|
More topic actions
Revisions: | r1.12 |
>
|
r1.11
|
>
|
r1.10
|
Total page history
|
Backlinks
You are here:
CoCreateModeling
>
MacroEnumerateDrives
r1.12 - 24 Jul 2009 - 19:54 -
ClausBrod
to top
CoCreateModeling
CoCreate Modeling
FAQ
Introduction
Hardware
Operating system
Memory management
File handling
Installation
Licensing
Graphics
App. knowhow
Lisp
Learning
Programming
Debugging
DDE
Compiler
Customization
Troubleshooting
Links
Code examples
Viewbench
News
Changes
Index
Search
Impressum
Home
Webs
Atari
Blog
Claus
CoCreateModeling
Klassentreffen
Main
Sandbox
Sommelier
TWiki
Xplm
My links
edit
TWiki
Welcome
TWiki Web TWiki Web Home Changes Topics Index Search
TWiki Webs Atari Blog Main
OneSpaceModeling
?
Sandbox TWiki TWiki Webs Atari Blog Main
OneSpaceModeling
?
Sandbox TWiki
Jump:
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback