batch - read from file

March 13, 2009 at 14:01:41
Specs: Windows XP
how do I add a number into the name of files created by batch file? Each time the bat is run, the number increases by one. I do not mind a separate file storing this number.

See More: batch - read from file

Report •

March 13, 2009 at 18:52:18
Storing the number is quite easy:

> numfile echo %num%

Where "numfile" is whatever you want to call your number file and %num% is a variable containing you number. It doesn't have to be a variable but since it will change each time it is run it is probably a good idea. The file will be overwritten with the new number each time so it will only contain the current number.

Retrieving the number is just as easy:

set /p num=<numfile

The number will be contained in the variable %num%.

This can be built upon to start from a specified number (I'll use 0) if "numfile" doesn't exist.

if exist "numfile" (
set /p num=<numfile
) else (
set num=0

To increment the number you can use the set /a command.

set /a num+=1

Report •

March 14, 2009 at 02:32:38
thanks, it works

Report •

March 14, 2009 at 04:27:11
One more question. I would like to append a log behind this number. Reading works fine, writing the log records works fine, but I do not know how to rewrite the first line (the number) I can only rewrite the whole file.

Report •

Related Solutions

March 14, 2009 at 05:52:28
Editing files from a batch script has been somewhat neglected, there are 3 main choices, running the whole file through a for loop and into a new file, third party utilities or the antiqued dos edlin that is not included with 64bit systems.

In both of the examples below it is important that your number is always on the very first line.

In your case using the for loop method may look something like:

>newfile echo %num%
for /f "usebackq skip=1 delims=" %%a in ("your text file.txt") do >>newfile echo %%a
move "newfile" "your text file.txt"

This will skim out any empty lines, if the empty lines are important steps can be taken to preserve them.

Here is a way that involves the antiqued edlin dos executable that isn't and won't be present in 64 bit systems. I don't recommend this method but I will outline it any way.

start /b /wait cmd /c "(echo 1&echo %num%&echo exit)|edlin file.txt&exit"

The reason I used start is to try and kill off ntvdm(nt virtual dos machine) without actually killing off the process in case it is in use. The edlin method has quite a few drawbacks, to mention a couple it can't handle long file names and always leaves a ^z end of file character at the end of the file.

Report •

March 14, 2009 at 05:59:26

holy cow!

If at first you don't succeed, you're about average.


Report •

March 14, 2009 at 09:38:00
Thanks for help, it is working and ready for use :)
One thing is weird though: If there are no files of specified type, it takes all ragardles the type (I expected it to do nothing)

It is shortened but he important lines are there :

@echo off
set typy=(*.doc *.xls *.txt)
set slozka=%~n0

if not exist %slozka% md %slozka%

if not exist "%slozka%\log.txt" (
echo 0 > "%slozka%\log.txt"
echo LOG pro %~n0%~x0 >> "%slozka%\log.txt"

set /p cislo=<"%slozka%\log.txt"
set /a cislo=(cislo+1)

>newfile echo %cislo%
for /f "usebackq skip=1 delims=" %%a in ("%slozka%\log.txt") do >>newfile echo %%a
move "newfile" "%slozka%\log.txt"

if not exist rar.exe (
echo Nenalezen soubor rar.exe >> %slozka%\log.txt
goto konec

set soubory=

for %%f in %typy% do (
set soubory=!soubory! "%%f"

rar a "%slozka%\%cislo%. zaloha - %date%.rar" %soubory%

Report •

March 14, 2009 at 16:58:08
I'm not sure what exactly the problem is, perhaps if %soubory% is undefined rar.exe takes the initiative and does the lot. You could test this by adding the line below just before the rar command:

if not defined soubory goto konec

There is another thing that may cause you problems depending on your regional settings - using %date% as part of a file name. If you use backslashes for delimiters e.g 15/3/09 it would make an illegal file name as backslashes are not allowed in file names. To overcome this is normally simple, use %date:/=-%.

Report •

March 14, 2009 at 18:01:25
It was the rar, it takes everything when run without any parameters.
In my local settings the date delimiter is . (dot) Anyway, I have updated it to %date:/=-%, it is safer :)

Report •

Ask Question