Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

610 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