1.

Solve : need help with bat file?

Answer»

Hello..
I am new to programming but I would like some help with creating a bat file that will do some basic changes..
The problem I am having is I would like to write a bat file that will open a certain TXT file in windows 2000 and convert the text to certain information needed by another program.  An example would be..
the first line of text would read...
goto/4.9087,6.9879,8.4567
next line would read..
goto/4.9113,7.8765,9.9876
and so on..
what I need to do is convert the goto to g01 and and an X to the first dimension and Y to the next dimension and z to the next dimension having the first line of text on one line ex.    G01 X4.9087Y6.9879Z8.4567  this would be on one line and then the bat file would look at the next line of the TXT file and do the same thing over and over again until it reaches the last line of the TXT file and also be able to save the new TXT file under a different name or the same name under a different directory.
There is more that I need to do to this to make it work automaticaly but if you COULD please help me it will give me a good start on the areas I am not sure about..
Thank You again for any help you could give me..For reading a text file line by line try uisng the FOR instruction with the /F switch and defining the delimiters as /, comma and dot. This will parse each line according to the parsing rules you set up:

Code: [SELECT]echo off
for /f "tokens=1-7 delims=/.," %%i in (file.txt) do (
   echo %%i %%j %%k %%l %%m %%n %%o
   )

The resulting output line would be

goto 4 9087 6 9879 8 4567

You would have to the use the set instruction to recombine them as you see fit.

The following code will make a one dimensional array. Continue nesting the FOR statements  with the /l switch to build more dimensions.

Code: [Select]echo off
set idx=0
for /l %%x in (1, 3, 52) do (
            for /l %%y in (1, 3, 52) do (
                        call set /a idx=%%idx%%+1
                        call set array.%%idx%%=%%y
            )
)      

Happy Computing.

 8-)Thank You very much Sidewinder..
I was able to change the coding to produce the correct output..Could you please tell me how to save this output to a different txt file. The problem is that the correct output shows in the dos window but now I need to be able to save that output to a different txt file.
Also can you tell me how to search the txt file for a certain word (EX. color) and delete it before saving the new txt file.
Thanks Again for all your help...To save a file under a different name, simply redirect the echo instruction to a specific file.

Code: [Select]echo off
for /f "tokens=1-7 delims=/.," %%i in (file.txt) do (
   echo %%i %%j %%k %%l %%m %%n %%o > newfile.txt
                   OR
   echo %%i %%j %%k %%l %%m %%n %%o >> newfile.txt
   )

As for SearchandReplace, this is more problematic:

This little snippet will change "green" to "white", but DELETING the "green" would necessitate changing, say "green" to nulls (as opposed to spaces) which I'm not sure you can do in DOS batch.

Code: [Select]echo off
setlocal enableextensions
setlocal enabledelayedexpansion
for /f %%x in ('dir /a:-d /s /b') do (
      for /f "tokens=* delims=" %%i in (%%x) do (
            set input=%%i
            set input=!input:green=white!
            echo !input! > %%x.chg
      )
)
endlocal      

Data manipulatiion and string operations are not really what batch language was designed for. There are many scripting languages (vbscript, jscript, perl, python, even Rexx) which are better tools for these operations. While batch language can actually be faster for some jobs, in the long run you're better off with a real scripting language.

Thanks for reading my rant. Every once in a while I like to get on my soapbox.

Good luck. 8-)



Discussion

No Comment Found