1. You are viewing our forum as a guest. For full access please Register. WindowsBBS.com is completely free, paid for by advertisers and donations.

Need help with batch file

Discussion in 'Windows XP' started by DugE, 2004/02/13.

Thread Status:
Not open for further replies.
  1. 2004/02/13
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    Using XP Home on a HP Pavilion

    @echo off
    del /y "C:\Documents and Settings\Default User\Local Settings\History "
    del /y "C:\Documents and Settings\Default User\Local Settings\Temp "
    del /y "C:\Documents and Settings\Default User\Local Settings\Temporary Internet Files "
    del /y "C:\Documents and Settings\Default User\Cookies "
    del /y "C:\Documents and Settings\Default User\My Recent Documents "
    Exit

    I'm using the Default User folder to test. The problem I'm having is that when I run this batch nothing is deleted. I deleted all the /y to confirm that the program is running but when I check the folders they still have contents. Am I doing this incorrectly or what? Thanks.
     
    DugE,
    #1
  2. 2004/02/13
    Steve R Jones

    Steve R Jones SuperGeek Staff

    Joined:
    2001/12/30
    Messages:
    12,315
    Likes Received:
    252
    del /y "C:\Documents and Settings\Default User\Local Settings\History\*.* "

    Have to tell it what to delete->All files
     

  3. to hide this advert.

  4. 2004/02/13
    reboot

    reboot Inactive

    Joined:
    2002/01/07
    Messages:
    831
    Likes Received:
    0
    Are you running this in autoexec.bat, or within Windows, or???
    If you're running it before the GUI loads, then you'll need to change all the paths and names to the 8.3 naming convention (change "Documents and Settings" to docume~1 etc.)

    I see another problem, you're using the DEL command to try and delete folders...try this instead:

    del /y C:\Documents and Settings\Default User\Local Settings\History\*.*
     
  5. 2004/02/13
    Hugh Jarss

    Hugh Jarss Inactive

    Joined:
    2002/07/22
    Messages:
    908
    Likes Received:
    6
    Hi

    unless they've changed it (for XP), del won't take a /y switch and you will hit the "All files will be deleted" message / confirmation prompt as both the name and the extension are wildcarded

    the usual dodge to get round this is to use deltree (which does take a /y), which knocks out the folder at the same time, you then recreated the folder...

    ...but this is no good for the particular folders you are using - will be a pain to delete 'coz of the attributes.

    if you use 8.3 naming you could do
    Try:
    @echo off
    for %%f in (C:\DOCUME~1\DEFAUL~1\LOCALS~1\HISTORY\*.*) do del %%f
    for %%f in (C:\DOCUME~1\DEFAUL~1\LOCALS~1\TEMP\*.*) do del %%f
    for %%f in (C:\DOCUME~1\DEFAUL~1\LOCALS~1\TEMPOR~1\*.*) do del %%f
    for %%f in (C:\DOCUME~1\DEFAUL~1\LOCALS~1\COOKIES\*.*) do del %%f
    for %%f in (C:\DOCUME~1\DEFAUL~1\LOCALS~1\MYRECE~1\*.*) do del %%f


    check the 8.3 equivalents first by doing a DIR in case you have a DEFAUL~2 or something instead; the DIR will show you the correspondence between the long and short names.

    also watch out! the short name equivalents can alter if you put other folders/files onto the machine - what used to be a ~1 at the end can change to a ~2 sneakily...

    best wishes, HJ.

    ...if you want to try these lines in the "real world" to check them out first (rather than inside a batch file), replace the %% with % everywhere...

    edited out a mistake in the blue section above - sorry - trying to cook and type at the same time!
     
    Last edited: 2004/02/13
  6. 2004/02/13
    markp62

    markp62 Geek Member Alumni

    Joined:
    2002/05/01
    Messages:
    4,012
    Likes Received:
    16
    Or, you could use Hugh's code in a simplified way
    @echo off
    for %%f in (HISTORY TEMP TEMPOR~1 MYRECE~1 ) do del C:\DOCUME~1\DEFAUL~1\LOCALS~1\%%f\*.*

    What this does is step through the names between the parenthesis, inserting the name right where the %%f is. The command at the end is executed, then the next name in line is called up.
     
  7. 2004/02/13
    Newt

    Newt Inactive

    Joined:
    2002/01/07
    Messages:
    10,974
    Likes Received:
    2
    Hugh - changed. No deltree command. Del will blitz folders as well as files. And using the /q switch should take care of the problem but since the /y switch doesn't exist, it won't do anything good.
    Code:
    Deletes one or more files.
    
    DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
    ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
    
      names         Specifies a list of one or more files or directories.
                    Wildcards may be used to delete multiple files. If a
                    directory is specified, all files within the directory
                    will be deleted.
    
      /P            Prompts for confirmation before deleting each file.
      /F            Force deleting of read-only files.
      /S            Delete specified files from all subdirectories.
      /Q            Quiet mode, do not ask if ok to delete on global wildcard
      /A            Selects files to delete based on attributes
      attributes    R  Read-only files            S  System files
                    H  Hidden files               A  Files ready for archiving
                    -  Prefix meaning not
    
    If Command Extensions are enabled DEL and ERASE change as follows:
    
    The display semantics of the /S switch are reversed in that it shows
    you only the files that are deleted, not the ones it could not find.
     
    Newt,
    #6
  8. 2004/02/13
    Hugh Jarss

    Hugh Jarss Inactive

    Joined:
    2002/07/22
    Messages:
    908
    Likes Received:
    6
    Hi Newt - thanks for the info...

    ...umm, there never was a /Y switch for delete! only for (the now defuct) deltree... or for copy...

    so, what was wrong with John's original then (when he tried it without the /Y)?

    "If a directory is specified, all files within the directory will be deleted." is very ambiguous, it doesn't say whether the directory itself will get deleted.

    (And, "deletes one or more files" doesn't really nail it either - could be taken either way... sometimes a directory is just another sort of file...)

    If it means the directory is going to get deleted as well, the operation might fail due to the attributes permissions or whatever for these particular folders, perhaps? Like - not allowed to remove the folder, so I can't delete the files?

    If not - do you have to specify the path name with a final backslash thus:
    del "C:\Documents and Settings\Default User\Cookies\"
    to make it think that it's a directory? - in order to delete the files therin? Strange...

    looking like:
    del /q "C:\Documents and Settings\Default User\Cookies\*.* "
    is maybe the best bet...

    ...or a neater version like Mark's (maybe with a /q)(as long as the "for (set) do" structure still works!)

    best wishes, HJ.
     
    Last edited: 2004/02/13
  9. 2004/02/14
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    Hi all, thanks for the responses.

    Steve - Thanks, I forgot about the *.*. My batch worked so well in 98 I never had to edit and thus forgot how it was written. But it still won't delete and I can't understand why not! I placed some junk files in the folders before running the program so there would be something to delete but they are still there.

    Reboot - I'm running this under Windows for now to get it right. Once I have it the way I want it I'd planned on placing it in the start up folder. But your suggestion of running it from autoexec.bat may be a better way to do it. How would I need to edit the autoexec.bat to run myfile.bat? Thanks.

    Hugh - Without the /y switch I had to enter a 'y' at each command line for it to carry out the function but when I added it I didn't have to. I didn't think xp recognized deltree and Newt's response verified it. I'm eventually going to have to use the 8.3 naming format so I can run this program at startup. I like your example but you've confused me with the ~1 changing to ~2. I'll keep my eyes open.

    Mark -- In your example I noticed you left out the cookie folder. Was this intentional for a reason unknown to me or can I add it to your example and have the entire \Defaul~1\ folder tree in one line? Thanks.

    Newt - After reading your post I'm wondering now if my original batch file failed due to the lack of the /f switch. My test files were read only. May be the reason it didn't work? I'll try and see. But I have to use the 8.3 naming format but gonna check the /f switch for the info.

    Hugh - remarking on your last post. Without the /y switch the program ran as if I had added the /p switch. It prompted me to enter a 'y' before it would proceed. When I added the /y switch I didn't have to input anything. I'm noticing a very big difference in batch files between xp and 98. Gotta learn all over again. Also, I remember dos in 98 never had a /y switch for the del command but since xp doesn't recognize the deltree command maybe the command prompt in xp now sees the /y switch for del. Not sure exactly but it seems like it to me. The /y switch worked for me with the delete command.


    Thanks again to you all. I'll rewrite my batch and post back.
     
    DugE,
    #8
  10. 2004/02/14
    markp62

    markp62 Geek Member Alumni

    Joined:
    2002/05/01
    Messages:
    4,012
    Likes Received:
    16
    That was accidental, you can add whatever you want, just leave a space between each name. I believe this command calls another batch from another, runs it then returns.
    call myfile.bat
    BTW, I believe the only switch the DEL command accepts is the /P, which prompts for confirmation before deleting.
    You could use the ATTRIB command to take off all hidden, system, read only, and archive bits from the files, then DEL should work on those files.
    for %%f in (COOKIES HISTORY TEMP TEMPOR~1 MYRECE~1 ) do attrib -r -s -h -a C:\DOCUME~1\DEFAUL~1\LOCALS~1\%%f\*.*
    The you would add the DEL command line after it.
     
  11. 2004/02/14
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    @echo off
    del /s /y "C:\Documents and Settings\Default User\Local Settings\History\*.* "
    del /s /y "C:\Documents and Settings\Default User\Local Settings\Temp\*.* "
    del /s /y "C:\Documents and Settings\Default User\Local Settings\Temporary Internet Files\*.* "
    del /s /y "C:\Documents and Settings\Default User\Cookies\*.* "
    del /s /y "C:\Documents and Settings\Default User\My Recent Documents\*.* "
    Exit


    None of the files are being deleted. I replaced the /f switch with the /s switch with no difference. I placed a txt file in each of the folders above so there would be something to delete. However, after running the above all files are still there. Any ideas?

    Also, when I rewrite to the 8.3 format will I be able to execute from the desktop for testing purposes or will I have to execute from autoexec.bat? Thanks.
     
  12. 2004/02/14
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    Hi Mark

    So the line should then read as:

    for %%f in (COOKIES HISTORY TEMP TEMPOR~1 MYRECE~1 ) do attrib -r -s -h -a C:\DOCUME~1\DEFAUL~1\LOCALS~1\%%f\*.* do del C:\DOCUME~1\DEFAUL~1\LOCALS~1\%%f\*.*


    Correct?
     
    Last edited: 2004/02/14
  13. 2004/02/14
    markp62

    markp62 Geek Member Alumni

    Joined:
    2002/05/01
    Messages:
    4,012
    Likes Received:
    16
    Needs to be like this. Your line would cause a syntax error as you have 2 DO commands in the same line.

    for %%f in (COOKIES HISTORY TEMP TEMPOR~1 MYRECE~1) do attrib -r -s -h -a C:\DOCUME~1\DEFAUL~1\LOCALS~1\%%f\*.*
    for %%f in (COOKIES HISTORY TEMP TEMPOR~1 MYRECE~1) do del C:\DOCUME~1\DEFAUL~1\LOCALS~1\%%f\*.*
     
  14. 2004/02/14
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    Ok but will changiing the attribs cause any folders to be deleted, such as cookies, that wouldn't be rebuilt automatically? Thanks.
     
  15. 2004/02/14
    markp62

    markp62 Geek Member Alumni

    Joined:
    2002/05/01
    Messages:
    4,012
    Likes Received:
    16
    The way the command as it is now, deletion takes place in the last named folder, leaving (cookies history etc) folders in place. The contents are being deleted., the \*.* does it this way.
     
  16. 2004/02/14
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    Thanks Mark. :)
     
  17. 2004/02/14
    markp62

    markp62 Geek Member Alumni

    Joined:
    2002/05/01
    Messages:
    4,012
    Likes Received:
    16
    You're welcome.
     
  18. 2004/02/14
    DugE

    DugE Well-Known Member Thread Starter

    Joined:
    2002/09/10
    Messages:
    726
    Likes Received:
    3
    @ECHO OFF
    for %%f in (C:\DOCUME~1\OWNER\LOCALS~1\HISTORY\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\DOCUME~1\OWNER\LOCALS~1\HISTORY\*.*) do del %%f
    for %%f in (C:\DOCUME~1\OWNER\LOCALS~1\TEMP\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\DOCUME~1\OWNER\LOCALS~1\TEMP\*.*) do del %%f
    for %%f in (C:\DOCUME~1\OWNER\LOCALS~1\TEMPOR~1\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\DOCUME~1\OWNER\LOCALS~1\TEMPOR~1\*.*) do del %%f
    for %%f in (C:\DOCUME~1\OWNER\MYRECE~1\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\DOCUME~1\OWNER\MYRECE~1\*.*) do del %%f
    for %%f in (C:\DOCUME~1\OWNER\COOKIES\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\DOCUME~1\OWNER\COOKIES\*.*) do del %%f
    for %%f in (C:\WINDOW~1\DOWNLO~1\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\WINDOW~1\DOWNLO~1\*.*) do del %%f
    for %%f in (C:\WINDOW~1\PREFET~1\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\WINDOW~1\PREFET~1\*.*) do del %%f
    for %%f in (C:\WINDOW~1\TEMP\*.*) do attrib -r -s -h -a %%f
    for %%f in (C:\WINDOW~1\TEMP\*.*) do del %%f
    EXIT

    All right, where did I go wrong? I'm using Hugh's version instead of Mark's right now to get the feel of the programming but when I run the above batch nothing gets deleted. Any ideas?

    Also Hugh mentioned doing a DIR. Did he mean to do it withing the command prompt? I right clicked on a file and chose properties looking for the 8.3 dos name like was given in 98. XP don't give it in properties. Where will I find it? Thanks again.
     
  19. 2004/02/14
    Hugh Jarss

    Hugh Jarss Inactive

    Joined:
    2002/07/22
    Messages:
    908
    Likes Received:
    6
    Hi John

    sorry for absence - struggling with a dysfunctional maxtor...

    two things on top of each other. You don't need to do the %%f substitution thing if you can get away with the *.* at the end of each line and do them individually.

    Suggest you try getting one line working first, then when one is working build the others out of it...

    doing a dir first is a good idea like this to see the files are in the right place...

    here I've saved the BATch file with the name TESTRUN.BAT, I run it at the DOS prompt by typing testrun

    I'm going to edit it next to change it so it deletes things.

    what you can dir you should be able to del later on...

    best wishes, HJ
     
  20. 2004/02/14
    Hugh Jarss

    Hugh Jarss Inactive

    Joined:
    2002/07/22
    Messages:
    908
    Likes Received:
    6
    OK - so I edit testrun.bat so that it looks like this and then go back to my DOS window.

    I type testrun and see there's no error message, then a do
    (anywhere)>dir C:\WINDOWS\cookies

    and the screen looks like this - you can see that the cookie I was "aiming at" has indeed vanished
     
    Last edited: 2004/02/14
  21. 2004/02/14
    Hugh Jarss

    Hugh Jarss Inactive

    Joined:
    2002/07/22
    Messages:
    908
    Likes Received:
    6
    ...as the cookie had indeed deleted, I'm not going to run into problems with attributes.

    The attribute thing is more to do with the folder the cookies are in - but I'm not going to try to delete it, just the files therein.

    Having got one cookie deleted, extend the program to *.*

    but I want to avoid using /y as I don't trust it (not documented according to Newt's posting), so I'll use substitution with the %%f thing.

    edit: wouldn't the /q switch be the one to do this?

    I edit the batch file so that it looks like this and run it at the dos prompt, to get an error message - it couldn't delete the index file as confirmed by doing a dir of the folder

    Which makes me wonder, were you seeing any error messages? If so what were they?

    phew, Hugh.

    (BTW, THICKET is what you get the Log Off....)
     
    Last edited: 2004/02/15
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.