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?

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.