Computing.Net > Forums > Programming > batch - read from file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch - read from file

Reply to Message Icon

Name: flukas
Date: March 13, 2009 at 14:01:41 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi,
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.



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: March 13, 2009 at 18:52:18 Pacific
Reply:

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


0

Response Number 2
Name: flukas
Date: March 14, 2009 at 02:32:38 Pacific
Reply:

thanks, it works


0

Response Number 3
Name: flukas
Date: March 14, 2009 at 04:27:11 Pacific
Reply:

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.


0

Response Number 4
Name: Judago
Date: March 14, 2009 at 05:52:28 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: March 14, 2009 at 05:59:26 Pacific
Reply:

edlin

holy cow!


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

M2


0

Related Posts

See More



Response Number 6
Name: flukas
Date: March 14, 2009 at 09:38:00 Pacific
Reply:

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
SETLOCAL ENABLEDELAYEDEXPANSION
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%
:konec


0

Response Number 7
Name: Judago
Date: March 14, 2009 at 16:58:08 Pacific
Reply:

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:/=-%.


0

Response Number 8
Name: flukas
Date: March 14, 2009 at 18:01:25 Pacific
Reply:

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 :)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch - read from file

Batch [READ FROM FILE THEN ECHO ] www.computing.net/answers/programming/batch-read-from-file-then-echo-/15561.html

java arrays and reading from file www.computing.net/answers/programming/java-arrays-and-reading-from-file/14305.html

Reading from a file www.computing.net/answers/programming/reading-from-a-file/19662.html