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.

Create a Date Batch File

Discussion in 'Other PC Software' started by w.young, 2011/01/12.

  1. 2011/01/12
    w.young

    w.young Inactive Thread Starter

    Joined:
    2002/01/07
    Messages:
    291
    Likes Received:
    0
    Hi,
    I'm going back in time here,.....

    I want to create a 'batch file' that when i run it, it will remove some files from a folder.
    These files are in a date format YYYYMMDD (IE: 20110112.bak)

    I want to delete all the files except the current date when the batch file is ran.

    @Echo Off
    SET M=%DATE:~4,2%
    SET D=%DATE:~7,2%
    SET Y=%DATE:~10,4%
    SET MyD=%Y%%M%%D%
    Echo %MyD%

    *** the command above displays the current date
    I'm not that good with IF statements....

    How can I make this happen?
    Thanks,
    Bill
     
  2. 2011/01/29
    Tony King

    Tony King Well-Known Member

    Joined:
    2002/05/12
    Messages:
    140
    Likes Received:
    1
    If you want a good reference, try 'Windows in a Nutshell' (O'Reilly, 2002) by D. Karp T. O'Reilly and T. Mott, ISBN 0-596-00249-1. It has a good section, pages 395 to 435, on DOS commands and batch files.

    Actually, as you have generated the current date, it looks fairly straightforward:

    IF %MyD%.* COPY C:\TEMP
    DEL C:\TEMP *.*
    COPY C:\TEMP [to original directory]

    The above will check the current directory for your 'date' files and any it finds it will copy to a TEMP directory. It will then delete all the remaining files and return the copied ones. It's a bit messy but I couldn't see a way round the copy and re-copy.

    NB: This is offered in good faith but with no guarantees!!
     

  3. to hide this advert.

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.