Computing.Net > Forums > Programming > batch file - reading from a 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 file - reading from a file

Reply to Message Icon

Name: mySelf
Date: May 19, 2009 at 12:53:51 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

hi evryone-
i need to wirte a batch script that read the first line from a txt file (try.txt) and than take the first word from this line'and finelly cope the try.txt to another directory with the new name file (the first word from the first line )!
thanks!!!



Sponsored Link
Ads by Google

Response Number 1
Name: Valerie (by Garibaldi)
Date: May 19, 2009 at 16:21:36 Pacific
Reply:

Change the destination path to suit.

@echo off
cls

for /f %%1 in (try.txt) do (
    copy try.txt path\%%1.txt > nul
)



0

Response Number 2
Name: Mechanix2Go
Date: May 20, 2009 at 00:43:52 Pacific
Reply:

Hi Val,

uh...
==========================
@echo off > newfile & setLocal EnableDelayedExpansion

for /f %%1 in (try.txt) do (
echo copy try.txt path\%%1.txt
goto :eof
)


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

M2


0

Response Number 3
Name: Valerie (by Garibaldi)
Date: May 20, 2009 at 02:25:22 Pacific
Reply:

Hi M2G.

But why? > newfile & setLocal EnableDelayedExpansion

Only the first word in the first line of the text file is being extracted, delayed expansion is not required (?) and no file is being written until Copy is executed. No local or global var being Set.

Also why goto :eof?

I take the liberty of reposting the OPs requirement with correction to typos.

i need to write a batch script that reads the first line from a txt file (try.txt) and then take the first word from this line and finally copy the try.txt to another directory with the new name file (the first word from the first line )!

I take the italicised to mean that the entire first line of the text file need not be read, only the first word of the first line of the text file need be extracted in order to produce the destination file-name.

I also find that as you've added Echo to the Copy command line that's exactly what is done. After the path is added the displayed output from your script is:

←C:\>trial1
copy try.txt d:\temp\carpatia.txt

←C:\>

(carpatia being the first word of the first line of the junk Try.txt file I created).

so Try.txt doesn't get copied to it's new location with its new file-name.

Have I had a blonde-out?


0

Response Number 4
Name: Mechanix2Go
Date: May 20, 2009 at 02:53:02 Pacific
Reply:

Hi Val,

My first line is boiler plate and not needed here.

Without goto :eof I get:

copy try.txt path\line.txt
copy try.txt path\second.txt
copy try.txt path\3rd.txt
=====================
when try.txt contains:

line one
second line
3rd line


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

M2


0

Response Number 5
Name: ghostdog
Date: May 20, 2009 at 05:30:14 Pacific
Reply:

@OP, if you can afford to use other tools like Python

thefile="file.txt"
firstline=open(thefile).readline()
firstword = firstline.split()[0]
os.rename(thefile,firstword)

save as myscript.py and on command line
c:\test> python myscript.py


0

Related Posts

See More



Response Number 6
Name: Valerie (by Garibaldi)
Date: May 20, 2009 at 19:56:04 Pacific
Reply:

M2G - profuse apologies. I must admit to having been beaten by a one-liner. I hope that the OP MySelf picked up on the correction you posted before creating multitudes of unwanted files.

Val.


0

Response Number 7
Name: mySelf
Date: May 21, 2009 at 01:08:49 Pacific
Reply:

Thanks all for your answers

this is what i write and it work

FOR /F "tokens=1,3 delims=," %%G IN (c:\temp3\y.txt) DO copy /d /y c:\temp3\x.txt c:\temp2\%%G.txt

but ....! i still having a problem
I write this .bat on a notepad and when i write this syntax
on a few row -this syntax will not work.
when i write this syntax in one row -its good
if i add few command (add word to the one line) it is not work as well.
my Q is : how can i write this syntax in few lines -so that it will work well

Thanks



0

Response Number 8
Name: Mechanix2Go
Date: May 21, 2009 at 01:35:58 Pacific
Reply:

Why are you using tokens=1,3 and what is copy /d ?


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

M2


0

Response Number 9
Name: mySelf
Date: May 21, 2009 at 02:28:28 Pacific
Reply:

Hi

You r right -it can bee "tokens=1,3 and what is copy"

But this is not the issue .
Why i can not write this syntax in few lines .
How can i write this line on a few that this script will work well.
I mean that if i write it on one line it work (but if it more then X carecters the script wont work at all)!!!

Thanks again . . . :)


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 file - reading from a file

Reading from a text file in C++ www.computing.net/answers/programming/reading-from-a-text-file-in-c/8983.html

Reading from a file and executing www.computing.net/answers/programming/reading-from-a-file-and-executing/19675.html

Reading from a USB Barcode-Reader o www.computing.net/answers/programming/reading-from-a-usb-barcodereader-o/11231.html