Computing.Net > Forums > Programming > batch to text help

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 to text help

Reply to Message Icon

Name: mustang509
Date: September 30, 2008 at 14:09:48 Pacific
OS: vista
CPU/Ram: 1250 mb
Product: pavilion
Comment:

Trying to write my first batch file to extract some text. The text format is as follows.
xxxxxx:yyyyyy-zzzzzz

The first file I wrote was.
@echo on
FOR /F "tokens=1,2 delims=:" %%G IN (test.txt) DO @echo %%H>>test2.txt

That got me down to yyyyyy-zzzzzz

Then I wrote
@echo on
FOR /F "tokens=1,2 delims=-" %%G IN (test2.txt) Do echo %%G;%%H;>>test3.txt
Then I got yyyyyy;zzzzzz;
Which is close to what I need but it writes to seperate lines cant figure out how to get
yyyyyy;zzzzzz;yyyyyy;zzzzzz
continous across one line.
Any help would be appreciated as I mentioned tthis is my first try any help or direction would be appreciated. I have searched the net and read many of posts just to get where i am. Also it would be appreciated if you could point me in the direction of a how to if there is such a thing. I am really interested in leaning a programing language just dont know where to start. Thanks in advance.
mustang



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: September 30, 2008 at 14:59:07 Pacific
Reply:

Using cmd.exe, there isn't a "proper" way to write some output without ending with a new line. However, there is a "trick" you can use:

Instead of echo %%G;%%H;>>test3.txt you can do:

set /p =%%G;%%H;<nul >>test3.txt

As you can see, the Win32 Command Processor batch language is quirky and unintuitive. It's pragmatic, we use it to get a job done, but we never claim it's a good language. If you want to learn a programming language, try something decent, like Python, Java or C#.


0

Response Number 2
Name: mustang509
Date: September 30, 2008 at 16:00:36 Pacific
Reply:

I really appreciate that pointer. Would you recomend python I know they probably all have good and bad points. But I just wont to pick one for now and try and get an understanding of whats going on. Probably a stupid question but would I lean that command that you gave me in python. Forgive me for being so nieve. Again thanks


0

Response Number 3
Name: ghostdog
Date: September 30, 2008 at 18:29:32 Pacific
Reply:

yes, you should learn Python.


0

Response Number 4
Name: mustang509
Date: September 30, 2008 at 18:52:51 Pacific
Reply:

Thanks for the comments and the help with the script here is my final. Probably not the best way to do it but it works thanks to klint. I downloaded python hopefully with some time I will be able to make something better but for now it works. :)

@echo on
FOR /F "tokens=1,2 delims=:" %%G IN (test.txt) DO @echo %%H>> test1.txt
FOR /F "tokens=1,2 delims=-" %%G IN (test1.txt) Do set /p =%%G;%%H;<nul >>test2.txt
del test1.txt
pause

Just out of curiosity is python similair to what I did above. I mean will it be able to do stuff like that. Sorry for all the dumb questions just trying to learn.


0

Response Number 5
Name: mustang509
Date: September 30, 2008 at 18:58:25 Pacific
Reply:

One thing I forgot to ask if python can do that kind of thing does someone have to have it installed on there computer to use it. Unlike if I learned dos thats already on most computers.


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: September 30, 2008 at 21:44:00 Pacific
Reply:

yes of course. Python is a programming language suitable for many tasks. in Python,


for lines in open("file"):
lines=lines.strip() #strip newline
x,y = lines.split(":")
# now x contains xxxxxx, and y contains yyyyyy-zzzzz
y = y.replace("-",";") # replace all - to ;
print ':'.join(x,y)



0

Response Number 7
Name: klint
Date: October 1, 2008 at 02:39:33 Pacific
Reply:

Python is a general-purpose programming language, and it can do lots of things that are nearly impossible to do with batch files. Batch files are special-purpose. They are good for running external commands and checking their return codes. Other things, such as text processing and arithmetic, are more difficult in batch but are easy in general-purpose languages. One disadvantage with Python is that the machine your program will run on will also need to have Python installed.

I chose to mention Python because it's relatively easy to learn and teaches good programming habits. However, you should get a good book to make it easier to learn. The one I learnt from was "Programming Python" published by O'Reilly. That was a few years ago. I don't know if there are better books now. Alternatively, you can try the tutorials that come with the Python download.


0

Response Number 8
Name: ghostdog
Date: October 1, 2008 at 04:16:13 Pacific
Reply:

if you want to run python script on different windows machine, you can compile them to executable then install them without installing the whole distribution.


0

Response Number 9
Name: mustang509
Date: October 1, 2008 at 06:48:50 Pacific
Reply:

Ghostdog want to thank you for your help. I used your program in python and it worked really good. For some reason I got an error on the print line. But that could be just because I wasnt sure how to run it.

print ':'.join(x,y)
TypeError: join() takes exactly one argument (2 given)

So I tried

for lines in open("pytest.txt"):
lines=lines.strip() #strip newline
x,y = lines.split(":")
# now x contains xxxxxx, and y contains yyyyyy-zzzzz
y = y.replace("-",";") # replace all - to ;
print y,

Works perfect had to cut and copy the results to wordpad. Wasnt sure how to get it to create a new text file yet. But I am sure I will figure it out. Again thanks this will give me hours of free entertainment lol.


0

Response Number 10
Name: mustang509
Date: October 1, 2008 at 08:15:42 Pacific
Reply:

After looking at it further the

print y,

didnt work either. It seemed to everything was on one line but there was a space between each one. And I have been looking for hours trying to figure out how to get it to save to a new text file after it prints. Any further help would be much appriciated. Thanks in advance.


0

Response Number 11
Name: Mechanix2Go
Date: October 1, 2008 at 08:44:07 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=1-3 delims=:- " %%a in (myfile) do (
set str=!str!%%a;%%b;%%c;
)
echo !str! > newfile


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

M2


0

Response Number 12
Name: mustang509
Date: October 1, 2008 at 09:37:38 Pacific
Reply:

Mechanix2go awsome you did in 1 line what I was doing in like 3 different batch files. I was looking into python havent given up on it. But right now I would be much more interested in learning batch which I guess is dos. Please correct me if I am wrong. Could you point me to any material that might speed up my learning curve. Also I have wondered what the 2 line setlocal does seems to work without it. Any further direction would be more than appriciated.


0

Response Number 13
Name: Mechanix2Go
Date: October 1, 2008 at 11:14:01 Pacific
Reply:

First things first. XP runs on NT and not DOS. NT has an NTVDM which provides 'DOS-like' services.

Most scripts written for DOS will run in NTVDM. But not vice-versa.
==================================
setLocal
means that vars are known only in this process and expire when the bat quits.


EnableDelayedExpansion
means that the var can change as neded.

example:

==================================
@echo off
setLocal EnableDelayedExpansion

for /L %%a in (1 1 3) do (
echo %random%
)

for /L %%a in (1 1 3) do (
echo !random!
)
==========================


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

M2


0

Response Number 14
Name: mustang509
Date: October 1, 2008 at 11:37:19 Pacific
Reply:

Thanks for that clarification. So would you say if I want to learn the basics of programing learn dos. I like the fact that it seems compatible with most windows based pc. I also like the batch function which I belive could do wonders for many projects. I have been doing alot of reading but cant seem to find the answer in as a real begginer what language I should try and learn. I have read many of your posts and like the tight formulas you come up with is that dos, c, c++. If its not to much trouble I was hopeing you could point me in the right direction.


0

Response Number 15
Name: Mechanix2Go
Date: October 1, 2008 at 15:56:09 Pacific
Reply:

I'd learn asm fordt, then c++


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

M2


0

Response Number 16
Name: ghostdog
Date: October 1, 2008 at 18:50:29 Pacific
Reply:

batch is not the tool to learn programming. It lacks data structures such as arrays and many more. As for Python, make sure you indent your code just like what i did in response 6. If not, the script will not run. If you want to learn Python, go to the official document website and take the tutorial


0

Response Number 17
Name: mustang509
Date: October 3, 2008 at 09:32:34 Pacific
Reply:

Thanks for all the great replies I used mechanix2go program and it worked great. Although I tried it on a larger file today a 6.5mb and it outputed a 8kb file. Is there a limit on the size a batch file can handle. the input file was .ext file so was the output file. Dont know if it helps any but the last couple of output characters started get to get jumbled up. Any help would be appreciated. Thanks in advance.


0

Response Number 18
Name: mustang509
Date: October 3, 2008 at 10:16:30 Pacific
Reply:

ghostdog could you give me some assistance I did put your program in with the correct spacing and I get this error.

File "C:\Users\****\Desktop\remove.py", line 5, in <module>
print ':'.join(x,y)
TypeError: join() takes exactly one argument (2 given)

Also can you tell me is that going to write to my existing file or create a new one. Or will it just print the results to the screen. If the later could you tell me how to make it create a new file. Thanks in advance.


0

Response Number 19
Name: ghostdog
Date: October 4, 2008 at 00:00:57 Pacific
Reply:


for lines in open("file"):
lines=lines.strip() #strip newline
x,y = lines.split(":")
# now x contains xxxxxx, and y contains yyyyyy-zzzzz
y = y.replace("-",";") # replace all - to ;
print "%s:%s"%(y,y)

the error message says very clearly that join() does not take in 2 arguments. Therefore it should be ''.join(arg)
I would suggest you read the Python docs as suggested if you want to take this further.
for writing to a file in Python, you open a file handle and write it out eg


open("outputfile","a").write(line)

All of these are documented, so pls do read the manuals and take the Python tutorial.

0

Response Number 20
Name: Mechanix2Go
Date: October 4, 2008 at 18:02:03 Pacific
Reply:

A bat probably won't get anywhere near a MB size file.


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

M2


0

Response Number 21
Name: mustang509
Date: October 4, 2008 at 19:38:25 Pacific
Reply:

Thanks for the info on that mechanix2go. Ghostdog thanks I have your file working. And I have been doing alot of reading on python. Would there be a reason why my 6.5mb file turns out with 1.2mb. Checked the end dont seem to run all the way through for some reason. It does go alot farther than the batch file :). Thank you both for all your help. I will keep reading.


0

Response Number 22
Name: Mechanix2Go
Date: October 4, 2008 at 22:46:52 Pacific
Reply:

You can use ths utility:

http://www.golden-triangle.com/chan...


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Open batch in new window ... phpMyAdmin 403 Error



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 to text help

Batch File Formmatting Help, Please www.computing.net/answers/programming/batch-file-formmatting-help-please/14040.html

Batch to parse .txt for words www.computing.net/answers/programming/batch-to-parse-txt-for-words/16839.html

batch to rename based on fileconten www.computing.net/answers/programming/batch-to-rename-based-on-fileconten/17419.html