Copy line in txt file to filename

March 8, 2010 at 06:58:44
Specs: Windows XP
Hi,

I have 2 files named abc.txt and abc.doc

abc.txt contains the following lines

test1
test2
test3
data below
copydata

The amount of test lines random from .txt file
to .txt file

I need the line BELOW the "data below"
copied and then used to rename the abc.doc
file to "copydata - abc.doc"

Looking forward for at solution ;)


See More: Copy line in txt file to filename

Report •

#1
March 8, 2010 at 07:41:38
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (abc.txt) do (
if !D!==Y (
set S=%%a
goto :done here
)
if "%%a" equ "data below" (set D=Y)
)
)
:done here

ren abc.doc "!S! - abc.doc"


=====================================
Helping others achieve escape felicity

M2


Report •

#2
March 8, 2010 at 08:21:28
I will try this. But forgot to mention one thing.

abc.txt and abc.doc was example files. I have a bunch of files where the filename is the same but the extension is different. So I have abc1.txt, abc1.doc, abc1.pdf and abc2.txt, abc2.doc, abc2.pdf and so on. The text files have the same structure (the same linie below the "data below" line i need for renaming the other files)

Any way to batch this for all files in one folder?

Thanks


Report •

#3
March 8, 2010 at 09:44:40
OK, get back to us when you've tested the first script and noted any other requirements.


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
March 9, 2010 at 02:13:29
Works great. I now just need, to be able to do this, to all files
in a folder.

I have changed a bit. the copied text is needed after the
original file name.

@echo off & setLocal EnableDELAYedeXpansion
for /f "tokens=* delims= " %%a in (abc.txt) do (
if !D!==Y (
set S=%%a
goto :done here
)
if "%%a" equ "data below" (set D=Y)
)
)
:done here
ren abc.doc "abc - !S!.doc"


Report •

#5
March 9, 2010 at 02:40:53
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%f in ('dir/b/a-d') do (
set D=N
for /f "tokens=* delims= " %%a in (%%f) do (
if !D!==Y (
set S=%%a
goto :done here
)
if "%%a" equ "data below" (set D=Y)
)
)
:done here
ren abc.doc "abc - !S!.doc"
)


=====================================
Helping others achieve escape felicity

M2


Report •

#6
March 9, 2010 at 02:49:55
Thank you for the quick answer. But what about the renaming
part?

If I have abc1.txt, it should take the line below "data below" and
use it to rename abc1.doc. But in the same folder I have abc2.txt
with another value in the line below "data below". This should
then be used to rename abc2.doc

Hope it makes sense


Report •

#7
March 9, 2010 at 03:09:00
Hold on. I goofed.


=====================================
Helping others achieve escape felicity

M2


Report •

#8
March 9, 2010 at 18:15:33
To activate, remove the ECHO in front of REN.
----------------------------------

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%f in ('dir/b/o-n/a-d *.txt') do (
set F=%%f
set FN=%%~Nf
set D=N
set S=
for /f "tokens=* delims= " %%a in (%%f) do (
  if '!D!'=='Y' (
    set S=%%a
    set D=N
  )
  if "%%a" equ "data below" (
    set D=Y
  )
)
:done here
if defined S echo ren "!F!" "!FN! - !S!.doc"
)


=====================================
Helping others achieve escape felicity

M2


Report •

#9
March 9, 2010 at 23:19:19
Almost there

Here is echo when I run the batch
ren "abc.txt" "abc - Text below data below line.doc"

2 problems:
- It's the .doc file I need renamed and not the .txt file
** Just managed to solve this **
by changing the ren line to
if defined S echo ren "!FN!.doc" "!FN! - !S!.doc"

- there is a problem when the .txt and .doc files contain
space (multiple words). It only uses the first word before
space.

Thank you for the good work so far ;)


Report •

#10
March 10, 2010 at 11:39:25
Solved the problem myself with space in files names. This is
now working.

changed this line
for /f "tokens=* delims= " %%a in (%%f) do (

to

for /f "usebackq delims==" %%a in ("%%f") do (

script now looks like this:
setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%f in ('dir/b/o-n/a-d *.txt') do (
set F=%%f
set FN=%%~Nf
set D=N
set S=
for /f "usebackq delims==" %%a in ("%%f") do (
if '!D!'=='Y' (
set S=%%a
set D=N
)
if "%%a" equ "data below" (
set D=Y
)
)
:done here
if defined S echo ren "!FN!.doc" "!FN! - !S!.doc"
)


Now the last question. Can I do anything to get this script
running in subfolders as well. I have a lot of subfolders, and I
could just run the script in every folder. But it would be nice to
just run it once, at the top folder level ;)

Thanks again


Report •

#11
March 10, 2010 at 15:09:34
this might be the easiest way, just make a bat to call M2s batch (f/e, called "fixit"):

@echo off
for /r %%a in (.) do call \basedir\fixit
:: ----- end
or make the main script a subroutine called by a main routine if you want it all in one script
for /r %%a in (.) do call :fixit
goto :eof
:fixit
...


Report •

#12
March 10, 2010 at 16:26:17
azazel,

I'm glad you made it work. I couldn't.


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question