Create a file with a batch

Posted on 2004-10-26
Medium Priority
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

Accepted Solution

georg74 earned 800 total points
ID: 12416887
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
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 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%
goto end

echo all file names are used!

----8<---- end of usage example ----8<----

Expert Comment

ID: 12417353
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Six Sigma Control Plans
Loops Section Overview

627 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