User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,444 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,620 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 829 | Replies: 3
Reply
Join Date: Oct 2007
Posts: 2
Reputation: rickshaw is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rickshaw rickshaw is offline Offline
Newbie Poster

Not sure if this is the right place...need windows script that will delete directorys

  #1  
Oct 25th, 2007
I'm unsure if this is exclusively a unix/linux scripting forum but I need help writing a windows script (I'm more of a perl/unix scripter).

I need to be able to periodically clean out the user profiles on a presentation PC in a conference room. Therefore I need a script which will remove directories recursively. However I don't know the name of the directories, except for a prefix. I know that windows rd /s /q won't do it, because it won't allow the use of wildcards (which is the obvious way to do it but being Micro$oft of course... ).

Therefore, is there someone who could supply me with a command that will remove directories and all the subdirectories under them, in windows? Pretty please?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Not sure if this is the right place...need windows script that will delete direct

  #2  
Oct 26th, 2007
You'll have to enable command extensions and use the for command. Works on XP.
:: change to the proper directory (probably C:\Documents and Settings\)
%SystemDrive%
cd "%ALLUSERSPROFILE%\.."

:: for each subdirectory name except "Administrator" and "All Users" do delete it
for /D %%dir in (*) do (
  if not "%%dir"=="Administrator" (
    if not "%%dir"=="All Users" rd /s/q "%%dir"
    )
  )
This is untested... you may have to tweak it some.

Good luck
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: rickshaw is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rickshaw rickshaw is offline Offline
Newbie Poster

Re: Not sure if this is the right place...need windows script that will delete direct

  #3  
Oct 26th, 2007
Originally Posted by Duoas View Post
You'll have to enable command extensions and use the for command. Works on XP.
:: change to the proper directory (probably C:\Documents and Settings\)
%SystemDrive%
cd "%ALLUSERSPROFILE%\.."

:: for each subdirectory name except "Administrator" and "All Users" do delete it
for /D %%dir in (*) do (
  if not "%%dir"=="Administrator" (
    if not "%%dir"=="All Users" rd /s/q "%%dir"
    )
  )
This is untested... you may have to tweak it some.

Good luck


Thanks. If I read that correctly, that will pass the value of %%dir, which is "*" to the command "rd", right?

Unfortunately rd, like rmdir, refuses to accept wildcards.

Its very elegant but all I get is the error:

%dir was unexpected at this time.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Not sure if this is the right place...need windows script that will delete direct

  #4  
Oct 26th, 2007
You are going to have a hard time shell scripting if you are unwilling to read about basic commands like for. Rd won't see "*" unless you somehow manage to name a directory "*".

I did also mention that basic testing was needed... which I have now done. Change every %%dir to read %%d . Apparently the command interpreter didn't like me using a variable name with more than one letter to its name.

Also, you might want to add @echo off to the top of the script so you don't see every command as it is executed.

VERY IMPORTANT
And before you run it make sure that you get the "Administrator" and "All Users" and any other directory you don't want deleted in there spelled correctly and capitalized correctly. If not you'll regret it.
Last edited by Duoas : Oct 26th, 2007 at 4:42 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC