batch file - reading from a file

May 19, 2009 at 12:53:51
Specs: Windows XP
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!!!

See More: batch file - reading from a file

Report •


#1
May 19, 2009 at 16:21:36
Change the destination path to suit.

@echo off
cls

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



Report •

#2
May 20, 2009 at 00:43:52
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


Report •

#3
May 20, 2009 at 02:25:22
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?


Report •

Related Solutions

#4
May 20, 2009 at 02:53:02
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


Report •

#5
May 20, 2009 at 05:30:14
@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


Report •

#6
May 20, 2009 at 19:56:04
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.


Report •

#7
May 21, 2009 at 01:08:49
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



Report •

#8
May 21, 2009 at 01:35:58
Why are you using tokens=1,3 and what is copy /d ?


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

M2


Report •

#9
May 21, 2009 at 02:28:28
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 . . . :)


Report •


Ask Question