[Dev-luatex] writing a rmdir function in texlua

Hans Hagen pragma at wxs.nl
Thu Apr 3 18:24:30 CEST 2008


Norbert Preining wrote:
> Hi all,
> 
> for the TeX Live uninstaller I am trying to write a rmdir function for
> win32 in texlua which should work like rm -rf on unix.
> 
> My first try was:
> function rmdir(dirn)
>   if os.type == 'windows' then
>     -- we have to replace all / with \
>     foo = string.gsub(dirn, '/', '\\')
>     ret = os.spawn({"rmdir", "/s", "/q", foo})
>   else
>     ret = os.spawn({"rm", "-rf", dirn})
>   end
>   if ret then
>     return ret
>   else
>     io.stderr:write(filename..': removing '..dirn.." didn't work\n")
>   end
>   return ret
> end
> 
> (No checks by now, will come later ...)
> 
> But that didn't work and did spit out always the io.stderr message.

rmdir is a system command

(1) use os.execute for system commands
(2) in the case of gnutools (you can never be sure it they are 
installed, use the funny cygwin syntax or so)

anyhow, this works:

function rmdir(name)
     if os.type == 'windows' then
         os.execute("rmdir /s /q " .. name:gsub('/', '\\'))
     else
         os.spawn({"rm", "-rf", dirn})
     end
     local ok = lfs.attributes(name)
     if ok then
         io.stderr:write(string.format("removing '%s' didn't work our 
well\n",name))
     end
     return ok
end

concerning your code:

 >   if os.type == 'windows' then
 >     foo = string.gsub(dirn, '/', '\\')
 >     ret = os.spawn({"rmdir", "/s", "/q", foo})

     local ret
     if os.type == 'windows' then
       local foo = string.gsub(dirn, '/', '\\')
       ret = os.spawn({"rmdir", "/s", "/q", foo})

in order not to polute the global namespace


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the dev-luatex mailing list