script to create a file with unique name

Discussion in 'Windows Server' started by cosimo, Jun 10, 2005.

  1. cosimo

    cosimo Guest

    I'd like to know if there is a way to create a new file within a script that
    have an unique name that contain the current date; i.e.: 10062005.txt.
    Thanks in advance.
    Cosimo MERCURO
     
    cosimo, Jun 10, 2005
    #1
    1. Advertisements

  2. Hi,

    With a VBScript:

    '--------------------8<----------------------
    dDate = Now

    ' using the ISO 8601 date format yyyymmdd so it is unambiguous and
    ' easily sortable in Explorer
    sDate = Year(dDate) & Right(100 + Month(dDate), 2) _
    & Right(100 + Day(dDate), 2)

    sFileName = sDate & ".txt"

    WScript.Echo "File name: " & sFileName

    '--------------------8<----------------------



    Or if you are using a batch file, you could use the code below.

    It is using the ISO 8601 date format yyyymmdd so it is unambiguous
    and easily sortable in Explorer.

    --------------------8<----------------------
    @echo off
    setlocal
    echo D = Now : WScript.Echo Year(D) ^& _ >%tmp%\today.vbs
    echo Right(100+Month(D),2) ^& Right(100+Day(D),2) >>%tmp%\today.vbs

    for /f "tokens=1" %%a in (
    'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a

    del %tmp%\today.vbs
    echo File name: %today%.txt
    pause
    endlocal
    --------------------8<----------------------


    You can do it with "pure" batch file as well, using e.g the batch date
    time functions from Ritchie Lawrence batch library available at
    http://www.commandline.co.uk/lib
     
    Torgeir Bakken \(MVP\), Jun 10, 2005
    #2
    1. Advertisements

  3. Here is a batch file solution:

    @echo off
    set d=%date:~4,10%
    :again
    set FileName=%d:/=%-%random%
    if exist %FileName% goto again

    %random% lies in the range of 1 .. 32,767.
     
    Pegasus \(MVP\), Jun 10, 2005
    #3
  4. cosimo

    cosimo Guest

    Great!!
    It works fine!
    Thanks you.
     
    cosimo, Jun 11, 2005
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.