Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!!!

Change the destination path to suit.
@echo off cls for /f %%1 in (try.txt) do ( copy try.txt path\%%1.txt > nul )

Hi Val,
uh...
==========================
@echo off > newfile & setLocal EnableDelayedExpansionfor /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

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?

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

@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 linec:\test> python myscript.py

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.

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 wellThanks

Why are you using tokens=1,3 and what is copy /d ?
=====================================
If at first you don't succeed, you're about average.M2

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

![]() |
![]() |
![]() |

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