Date/timestamping filenames/foldernames in a Windows Batch file

Discussion in 'Software' started by goldfish, Jul 27, 2004.

  1. goldfish

    goldfish Lt. Sushi.DC

    Okay. How the blinking hell do you use the %date% and %time% variables, or the time and date commands to put a datestamp on a filename/foldername. You can't use either in either, cause they have charachters like / and . and :, which you cant have in filenames or foldernames.

    What has to be done?
     
  2. snakefoot

    snakefoot Sergeant Major

  3. goldfish

    goldfish Lt. Sushi.DC

    Yes, sorry, I should have mentioned.
    but....
    echo blah %date:~10,4%
    outputs "blah "
    and nothing else. Am I doing somthing wrong? Do I need somthing installed?
     
  4. goldfish

    goldfish Lt. Sushi.DC

    Hm.
    This is interesting. The above batch works, in as far as it makes a datestamp folder. However, I want to time-stamp the filenames. I dont know how the for /f statement works, and I've tried substituting the date /t section to time /t section, and i just get the minutes. All I need really is the hour and minutes to put on the end of the file (so somelog-1350.log). I dont really understand how I can do that right now using that statement.
     
  5. goldfish

    goldfish Lt. Sushi.DC

    Hmm, I've worked it out. Sure is ugly though!
    Code:
      @ECHO off
    
      :: Extract DATE tokens to D
      FOR /F "delims=:-. " %%C IN ('time /t') DO SET H=%%C
      FOR /F "tokens=2-4 delims=:-. " %%G IN ('time /t') DO SET M=%%G%%H%%I
      FOR /F "tokens=2-4 delims=/-. " %%P IN ('date /t') DO SET D=%%R%%Q%%P
    
      :: Expand D in your line and remove ECHO.{demo!} to activate it
      ECHO "C:\logs\%D%\log-%H%-%M%"
    
    There must be an easier way to get the time out (in the same way as date..)
     
  6. Kodo

    Kodo SNATCHSQUATCH

  7. goldfish

    goldfish Lt. Sushi.DC

    Yeeahh... but I don't know about WMI (yet) ;)
     
  8. Kodo

    Kodo SNATCHSQUATCH

    put your batch down and walk away... now pick up WMI.. if you can do VBScript, you can pick up wmi rather quickly.
     
  9. goldfish

    goldfish Lt. Sushi.DC

    C:\WMI\physical_memory.vbs(3, 1) Microsoft VBScript runtime error: Permission denied: 'GetObject'

    :( when trying to connect to my Win2003 server

    And also why do MSDN code samples have to by SO DARN SMALL! I have to turn up the text size loads just even to be able to read them... and the rest of the text looks massive!
     
  10. goldfish

    goldfish Lt. Sushi.DC

    Works when I want to do it to myself, however... Hmm
     
  11. goldfish

    goldfish Lt. Sushi.DC

    Waaw! WMI is cool! :D

    Not so great for copying files, well, not yet anyway. I'd imagine that the FileSystemObject in VBScript would do that for me, but then thats not really WMI, its just a VBScript.

    I'm gunna have to do some learning :D
     
  12. goldfish

    goldfish Lt. Sushi.DC

    Dammit, i think my brain is about to explode!!
     
  13. Kodo

    Kodo SNATCHSQUATCH

    yeah, there's some really cool stuff in there..

    here's my script for enabling and disabling System Restore on winxp


    VBS file:
    ____________
    If MsgBox("Click ok to disable System Restore for all volumes. Click Cancel to enable", vbOKCancel, "System Restore Manipulator") = vbCancel Then

    ena
    Else
    dis
    End If





    Sub dis()
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\default")
    Set objItem = objWMIService.Get("SystemRestore")
    errResults = objItem.Disable("")
    msgbox "System Restore Disabled",vbOKOnly,"Disabled!"

    End Sub

    Sub ena()
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\default")
    Set objItem = objWMIService.Get("SystemRestore")
    errResults = objItem.enable("")
    msgbox "System Restore Enabled",vbOKOnly,"Enabled!"




    End Sub
     
  14. goldfish

    goldfish Lt. Sushi.DC

    Oo, groovy little script :D I was going to modify it to check whether it was enabled... but for some reason there isn't a "status" property in either SystemRestore OR SystemRestoreConfig. Bizarre.

    I has gotta think of somthing to do with it so I can work out what I can do with it :D
     
  15. NeoNemesis

    NeoNemesis Moutharrhea

    wow that looks extremely hard. is wmi harder than c++?
     
  16. Kodo

    Kodo SNATCHSQUATCH

    it's just different is all. If you're using to VB then it's pretty easy to pick up. It just involves a whole other class of objects really..
     
  17. goldfish

    goldfish Lt. Sushi.DC

    Monkeys can code VB
    http://www.newtechusa.com/PPI/main.asp

    :D
     
  18. Kodo

    Kodo SNATCHSQUATCH

    har har... read that joke a bout a year ago..lol
     
  19. goldfish

    goldfish Lt. Sushi.DC

    OMG, i rock... i made a WMI script to do somthing useful... using a class ive never used before... !! :D YAY!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds