Create a file with a batch

Posted on 2004-10-26
Last Modified: 2011-09-20
Hi, I need to create a batch that creates a txt file, here is the fun part the batch will run every 1 to 5 minutes (the time is not impotant)  and a new file will be created with a new name, this name has to be in sequence to the last one created or we can have the time it was created (HH:MM:SS)

Question by:oalvarado
    LVL 3

    Accepted Solution

    you did not mention the language, so i suppose you need win* batch :-)
    the problem is how to create next file name in the sequence, where
    win* batch language lacks basic arithmetics and string functions.

    so, here is a batch file to create a new file name in the sequence.
    the file name will be <directory>\<prefix><number>.<extension>
    the output is put in the NEWNAME shell variable, so you can use it
    in another batch. It will generate sequences of up to 10000 file names,
    but it can be easily extended.

    this batch will run on (almost) every version of win* (and DOS).
    It can be simplified a bit if you're using only XP...
    With WinXP, it can be extended to add current date to the file name
    or create a directory with current date as the name,
    and so to create up to 10000 file name daily. Ask if you need that.

    ----8<---- newname.bat ----8<----
    @echo off

    rem ---- configuration ----
    set DDIR=c:\destination\directory
    set PREFIX=f_
    set EXT=txt
    rem --- end of config ----

    set A=0
    set B=0
    set C=0
    set D=0
    set NEWNAME=%DDIR%\%PREFIX%%D%%C%%B%%A%.%EXT%
    if not exist %NEWNAME% goto end
    if %A%==9 goto 20
    set V=A
    set X=%A%
    goto inc
    if %B%==9 goto 30
    set V=B
    set X=%B%
    set A=0
    goto inc
    if %C%==9 goto 40
    set V=C
    set X=%C%
    set B=0
    set A=0
    goto inc
    if %D%==9 goto fail
    set V=D
    set X=%D%
    set C=0
    set B=0
    set A=0
    goto inc

    if %X%==0 goto 1
    if %X%==1 goto 2
    if %X%==2 goto 3
    if %X%==3 goto 4
    if %X%==4 goto 5
    if %X%==5 goto 6
    if %X%==6 goto 7
    if %X%==7 goto 8
    set %V%=9
    goto try
    set %V%=1
    goto try
    set %V%=2
    goto try
    set %V%=3
    goto try
    set %V%=4
    goto try
    set %V%=5
    goto try
    set %V%=6
    goto try
    set %V%=7
    goto try
    set %V%=8
    goto try

    set NEWNAME=

    set A=
    set B=
    set C=
    set D=
    set X=
    set V=
    ----8<---- end of newname.bat ----8<----

    ----8<---- usage example ----8<----
    @echo off

    rem this example runs on XP, where date /T is available

    call newname.bat
    if %NEWNAME%a==a goto full_house
    echo first line in the new file >%NEWNAME%
    date /T>>%NEWNAME%
    time /T>>%NEWNAME%
    set NEWNAME=
    goto end

    echo all file names are used!

    ----8<---- end of usage example ----8<----
    LVL 3

    Expert Comment

    Hi guys,

    Not shooting for points here but I came up against a similar issue with trying to date stamp a file and found this script which is quite cool. It puts all the different date/time parts into environment variables which can then be referenced using %DD%/%MM%/%YY% %HH%:%MIN%:%SS% or any other combo of these variables.

    REM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    For /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
          For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
                set dow=%%i
                set %%a=%%j
                set %%b=%%k
                set %%c=%%l
                set hh=%%m
                set min=%%n
                set ss=%%o
    if %HH%==10 goto end
    if %HH%==11 goto end
    if %HH%==12 goto end
    set HH=0%HH%

    REM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article will show, step by step, how to integrate R code into a R Sweave document
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now