•
•
•
•
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,571 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 3,609 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: 830 | Replies: 3
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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?
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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.
This is untested... you may have to tweak it some.
Good luck
:: 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"
)
)Good luck
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Re: Not sure if this is the right place...need windows script that will delete direct
#3
Oct 26th, 2007
•
•
•
•
You'll have to enable command extensions and use the for command. Works on XP.
This is untested... you may have to tweak it some.:: 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" ) )
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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
Also, you might want to add
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
antivirus apple browser cd choose computer console crack debian defender dell development download drm fiji france hoax install internet leopard linux microsoft mobile news office open operating os photo registry research root security server software source spyware survey system technology torvalds tweaks ubuntu unix upgrade vista windows windows update windows vista xp
- Reformating/reinstalling windows (Windows NT / 2000 / XP / 2003)
- windows script host - help me pls (HTML and CSS)
- windows error service spyware message (Viruses, Spyware and other Nasties)
- Problems with Windows Script Encoder & #include directive (ASP)
- I occasionally get hijacked when browsing. (Viruses, Spyware and other Nasties)
Other Threads in the Shell Scripting Forum
- Previous Thread: Paste files in order and columns
- Next Thread: script to query records from mysql and send emails?



Linear Mode