.bat To Do Some Deleting


Recommended Posts

I want to make a small script to delete cookies, temp, temp internet, recent, and history.

I want to carry this on a thumb drive, use it to clean client machines. The user directories will be different.

Can I do that?

How?

Made a few batch files before but nothing to brag about.

Mike

I want to be able to use it on different machines.

Machine 1 - 2 users.

C:\Documents and Settings\user1\Local Settings\History

C:\Documents and Settings\user1\Cookies

C:\Documents and Settings\user2\Local Settings\History

C:\Documents and Settings\user2\Cookies

different machine:

C:\Documents and Settings\useA\Local Settings\History

C:\Documents and Settings\userA\Cookies

Users will be different, because it will be different macnines.

M

Edited by mikex
Link to post
Share on other sites

Sigh. I don't have access to my Windows box at the moment, unfortunately.

The USERPROFILE environment variable is set to the directory of the current user profile (equivalent to %HOMEDRIVE%%HOMEPATH% if Google is correct). The location of the files should be the same for every profile, so the batch file could be structured a series of

del %USERPROFILE%\somedirectory\files

or deltree or whatever.

It's probably more complicated if you need to support both WinNT and Win9x.

Edited by jcl
Link to post
Share on other sites
Could have a .bat for 9X and another for XP.

<{POST_SNAPBACK}>

You can detect the operating system with, um... *googlegoogle* OS. %OS% is set to 'Windows_NT' on NT/2k/XP and not defined on 9x. The batch file can detect the system and adjust the target paths appropriately.

Link to post
Share on other sites

is there a way to do command substitution with batch? for instance the output of

dir C:\documents and settings

I was wanting to use that output with a for statment.

for example

for %%i in `dir C:\documents and settings` do this

Edited by shanenin
Link to post
Share on other sites

Yes. The syntax is something like

for /F %%i in ('dir C:\documents and settings') do ...

or

for /F "usebackq" %%i in (`dir C:\documents and settings`) do ...

Edit: Got my W2k machine back on the LAN. The above works but the for (or cmd.exe, or something) does automatic field splitting, so you have use set the 'delims' option if you want to whole line, like so

for /F "delims=" %%i in ('dir C:\') do echo %%i

or

for /F "usebackq delims=" %%i in (`dir C:\`) do echo %%i

Edited by jcl
Link to post
Share on other sites

This works. I'm not sure why it works, but it does.

: Hygiene
setlocal                          

: the value of 'targets' is a space-seperated list of paths to be mangled
set targets="%USERPROFILE%\Cookies" "%USERPROFILE%\Local Settings\History"

: iterate over the paths in targets, doing whatever needs to be done
for %%i in (%targets%) do mangle %%i

: Hygiene
endlocal

If I replace 'mangle' with 'dir' I get

C:\Documents and Settings\jcl\My Documents\src\batch>f.bat         
Volume in drive C has no label.                                      
Volume Serial Number is DCAE-1139                                          

Directory of C:\Documents and Settings\Default User\Cookies    

02/28/2005  01:13a              16,384 index.dat                      
              1 File(s)         16,384 bytes                                  
              0 Dir(s)     471,162,880 bytes free                          
Volume in drive C has no label.                                            
Volume Serial Number is DCAE-1139                                      

Directory of C:\Documents and Settings\Default User\Local Settings\History

File Not Found

which is almost correct, so I assume it's working. I have no idea why %USERPROFILE% is set to 'Default User'.

Edited by jcl
Link to post
Share on other sites

my thought was to use a for statment to go thru and delete the needed data for all users. Since all of the users are listed under c:\documents and settings\ I could do something like this. I was just testing this in the shell. first I am changing to the directory documents and settinds then trying this

for %i in (*) do echo %i%

it is running with out echoing anything or giving me an error. I thought one time it worked for me, but I forgot the syntax I used. I read in a doc you use %%i for scripts, but in the shell you use %i . Does my for statment look wrong?

edit added//

this seems to work

for /d %i in (*) do echo %i

Edited by shanenin
Link to post
Share on other sites

The problem with deleting the files for all accounts is that the batch file would need to run under Administrator or another account with ridiculous privs. If that's reasonable then yeah, it can be done easily by iterating over the contents of Documents and Settings. You do have be careful to hit only the accounts that really should be cleansed. And you'll miss accounts if they don't have a profile in the usual place.

Edited by jcl
Link to post
Share on other sites

if he is making a batch script to clean customers computers, which he is going to carry on a usb drive. I would think he would be running as admin. There is still the problem of use profiles in odd spots.

Edited by shanenin
Link to post
Share on other sites

I think you're over-complicating this.

the %1 variable is all you need.

If the file CLEAN.BAT has these lines:

DEL C:\Documents and Settings\%1\Local Settings\History *.*

DEL C:\Documents and Settings\%1\Cookies *.*

Then running CLEAN USER1

expands to

DEL C:\Documents and Settings\USER1\Local Settings\History *.*

DEL C:\Documents and Settings\USER1\Cookies *.*

You will have to confirm the delete commands.

(That can be automated as well).

Link to post
Share on other sites

He'd have to manually collect the list of accounts, invoke the batch file for each one, and hope that the profiles are all in the standard locations. At the very least I'd want the batch file to iterate over a list of usernames provided as arguments and lookup the profiles for each.

This would really be much easier in VB or something.

Link to post
Share on other sites
.This would really be much easier in VB or something.

<{POST_SNAPBACK}>

batch(what ever it is called) just sucks. I don't really have anything facts to back that up with, but it fealt good to say :-)

Edited by shanenin
Link to post
Share on other sites
is the new shell officially going to be in vista?

<{POST_SNAPBACK}>

Last I heard. It's actually being released before Vista, as part of Exchange Server on Win2003. There's no reason why it couldn't ship for XP as well.

Link to post
Share on other sites

from my understanding of monad, eventually a person will be able to manage there whole windows system from a command line.

I am still having trouble grasping what it does. Is monad similar to bash, in the sence it is a powerful scripting language, but with oop built in.

Link to post
Share on other sites
Is monad similar to bash, in the sence it is a powerful scripting language, but with oop built in.

<{POST_SNAPBACK}>

Precisely. It's very much like a typical Unix shell, but implemented on, and hooked into, the .NET framework.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...