Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Create the file name with the date  (Read 18100 times)

0 Members and 1 Guest are viewing this topic.

sun_os

    Topic Starter


    Beginner

    Create the file name with the date
    « on: October 22, 2013, 07:30:44 PM »
    Dear All

    I want to create the file to call a number of the batches file to run the schedule job with the file name, the output result with the name date format (e.g. abc_20131023.xml).

    Besides, I want to the script clear the file name before generate the report.

    Can you please let me know the script can handle this task ?

    Thanks 

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Create the file name with the date
    « Reply #1 on: October 22, 2013, 08:54:06 PM »
    I do not know the format of a .xml, but you can access the date by using the variable %date%.  To help further we would need to know what 'echo %date%' outputs on your computer as it differs. Below is an example that would work on my machine that should help you puzzle your way through it.

    output:
    Code: [Select]
    C:\windows\system32>echo %date%
    10/22/2013 Tue

    code:
    Code: [Select]
    for /f "tokens=1 delims= " %%A in ("%date%") do (
    for /f "tokens=1-3 delims=/" %%B in ("%%A") do (
    set m=%%B
    set d=%%C
    set y=%%D
    ) )
    set prefix=abc
    set ext=xml


    set name=%prefix%_%y%%m%%d%.%ext%

    OR
    [/code]
    for /f "tokens=1,2 delims==" %%A in ('wmic path win32_localtime get month^, day^, year /value') do set %%A=%%B
    set prefix=abc
    set ext=xml


    set name=%prefix%_%Year%%Month%%Day%.%ext%

    ::May not work, requires wmic
    [/code]
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Create the file name with the date
    « Reply #2 on: October 22, 2013, 09:06:08 PM »
    The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

    Your description is very vague but this has lines below which delete a file if it exists or not, so you can create new data in the same filename.

    Code: [Select]
    @echo off
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

    set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
    set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
    echo datestamp: "%datestamp%"
    echo timestamp: "%timestamp%"
    echo fullstamp: "%fullstamp%"

    del "c:\path\to\folder\abc_%datestamp%.xml" 2>nul

    echo do something with "c:\path\to\folder\abc_%datestamp%.xml"

    pause

    sun_os

      Topic Starter


      Beginner

      Re: Create the file name with the date
      « Reply #3 on: October 18, 2014, 10:36:30 AM »
      Thank you all of your  guys ;D

      Blisk



        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: Create the file name with the date
        « Reply #4 on: October 26, 2014, 09:17:24 AM »
        I use this and it works
        Code: [Select]
        echo. 2>date.txt
        for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "date.txt" %%e-%%f-%%gdatet.txt

        sun_os

          Topic Starter


          Beginner

          Re: Create the file name with the date
          « Reply #5 on: April 21, 2021, 10:31:00 PM »
          Thank you brother

          Allan

          • Moderator

          • Mastermind
          • Thanked: 1260
          • Experience: Guru
          • OS: Windows 10
          Re: Create the file name with the date
          « Reply #6 on: April 22, 2021, 05:13:15 AM »
          deja vu

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1769
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: Create the file name with the date
          « Reply #7 on: April 22, 2021, 05:24:24 AM »
           :P     ;D
          " Anyone who goes to a psychiatrist should have his head examined. "