Page 1 of 1 1
Topic Options
#122468 - 2004-07-09 02:36 PM Hide output of shell "%Comspec% /c rd /s /q..." ?
Learic Offline
Fresh Scripter

Registered: 2004-02-16
Posts: 12
Frist off, I need to say that I know Kix offers the "RD" command to remove directories but it has to be empty. This is the main reason I have to use the Shell command to invoke the windows version of "RD" that will remove all files, subdirs, and the directory itself! Here's the code I have a question about.

RedirectOutput("NUL")
Shell "%comspec% /c rd /s /q $Directory"
RedirectOutput("CON")

$Driectory is actually a directory name in my code such as "C:\yadayadayada"

When I invoke this script, I get "The system cannot find the file specified." This is normal if the directory I'm deleting doesn't exist. I was wondering if there is a way to HIDE any output given from a command that is invoked with SHELL. Here's the reasoning..

It is possible for a directory to have a file existing which means the directory is still needed by the system because the executable is running as a service. If the specific file I'm looking for does not exist however and the directory is still there, I need to delete it and anything else that is contained within it. I'm basically trying to clean up an installed program that doesn't clean up well on it's own. The uninstall will always delete the main file that runs as a service but it is possible for it to leave a directory behind with other non-necessary files still intact. So here is the code I'm trying.

IF NOT EXIST("$Directory\Executable.exe")
RedirectOutput("NUL")
Shell "%comspec% /c rd /s /q $Directory"
RedirectOutput("CON")
Else
$Directory_Delete=FAILED
ENDIF


Top
#122469 - 2004-07-09 02:47 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
RedirectOutput("CON")

The textbook way to end redirection is:
RedirectOutput("")

BTW, RedirectOutput() will never work on SHELLed output. You need to use the DOS redirector > for that. There is a FAQ on that.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122470 - 2004-07-09 02:47 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Just redirect output to the bit bucket, like this:

Shell "%comspec% /c rd /s /q $Directory >nul 2>nul"

Might also suggest to wrap your statement differently, like this:

Shell '%comspec% /c rd /s /q "$Directory" >nul 2>nul'

with single/double-quotes - just incase $directory ever contains a long filename !

-Shawn


Top
#122471 - 2004-07-09 02:49 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I believe you will also get that error if your $Directory has a space in it, like "c:\program files\yadayada".

Give this a shot

Code:

$shell = '%comspec% /c rd /s /q "' + $Directory + '" >nul'
shell $shell

_________________________
Eric

Top
#122472 - 2004-07-09 02:51 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Shawn,
Don't you know that vars in strings are EVIL? THey will never read my FAQ if you give them the short answer.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122473 - 2004-07-09 02:57 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Thats why I'm wearing my Dr. Evil avatar today :0). Agreed Les - $vars in strings are evil. I can get away with it though, because I am old.
Top
#122474 - 2004-07-09 02:57 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Learic Offline
Fresh Scripter

Registered: 2004-02-16
Posts: 12
Thanks Shawn! That worked.. I tried the >NUL originally but removed it since it wasn't working! Didn't know that I could do >NUL 2>NUL and that works beautifully!

Top
#122475 - 2004-07-09 03:10 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Yes, but do you know why it works?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122476 - 2004-07-09 03:10 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Your welcome. No one ever wants to debate me on this, but if $vars in strings are evil, than %vars%'s in strings are equally evil.
Top
#122477 - 2004-07-09 03:16 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well... before we hyjaak this thread, the answer to 'why' is in the TechNet article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;110930

Shawn,
Use the SetOption('NoVarsinStrings','on') and see if %vars% are excluded. Same goes for @Macros.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122478 - 2004-07-09 03:19 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
They are excluded - but they shouldn't be (excluded) because they are vars.
Top
#122479 - 2004-07-09 03:25 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
K then... should HKCU and HKLM also be excluded cuz they too are macros of sorts.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122480 - 2004-07-09 03:49 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Your welcome. No one ever wants to debate me on this, but if $vars in strings are evil, than %vars%'s in strings are equally evil.




We did have a rambling debate on this, but I can't find it. I'd agree that "no vars" should include anything that might be expanded, including macros and environment strings.

I did turn up a quote from Ruud stating that he intended to have a NoMacrosInStrings.

Quote:

should HKCU and HKLM also be excluded cuz they too are macros of sorts.




Not really. They don't get expanded at all - HKCU is just HKCU regardless of how or where you use it. The only thing that is special about it is that if it is at the start of a string passed to a registry function it is recognised by the function as a shorthand or alias. The string that the function receives however remains unchanged.

Top
#122481 - 2004-07-09 03:57 PM Re: Hide output of shell "%Comspec% /c rd /s /q..." ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
imho they're different, they're not vars. I'm not saying I'm necessarily right or wrong on this point. Just wanted to point-out some inconsistencies in the "rules" and get some opinion. I posed this @macro/%var% question to Ruud ages ago when he first came out with NoVarsInStrings ... and he didn't bite. To be honest, think (he thinks) its a bit of a rat hole to go down.

The question is, is there really a fundamental difference between this:

$x = "$windir\system32"

and this:

$x = "%windir%\system32"

and if not, then should "best practice" be:

$x = %windir% + "\system32"

I myself don't get too worried about vars-in-strings - our corp login script (even the newest one we just did) are riddled with them. Were we (I) concerned about $vars being not supported in the future ? Sure was - but I truely think Ruud will never "go there" ... thoughts ?


By the way, the HKLM / HKCU thingy is different because if they were expressed outside of quotes, it would break another "rule". It would be an un-quoted string (which some have been quilty of doing too :0) )

$key = HKCU + "\Control Panel"

funny enough though, that syntax still works





Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1955 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.065 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org