-2

I have a text file with data. And I would like to copy the text after #NAME:, and add some more text after that.

Original file:

#NAME:,Disco Ball
http://my.site.com/discoball.mp3
#NAME:,Lasers
http://my.site.com/lasers.mp3

New text file:

#NAME:,mp3-id="Disco Ball",Disco Ball
http://my.site.com/discoball.mp3
#NAME:,mp3-id="Lasers",Lasers
http://my.site.com/lasers.mp3

As you can see, I want to be able to copy the text after #NAME:, and then add it within the new text mp3-id=" ",

How can I go about doing this either in a batch file or VB script?

Mofi
  • 46,139
  • 17
  • 80
  • 143
bayard0
  • 27
  • 1
  • 4
  • You must show us some efforts from you , so edit your question and post the code that you have been tried until now ! – Hackoo Mar 27 '16 at 09:51

5 Answers5

1

If you decide on VBScript: Open the file and copy the contents to a string, replace the correct sections, and then write to the file.

Read and write into a file using VBScript

Community
  • 1
  • 1
1
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q36242413.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqtokens=1*delims=:," %%a IN ("%filename1%") DO (
 IF /i "%%a"=="#NAME" ECHO(%%a:,mp3-id="%%b",%%b
 IF /i "%%a"=="http" ECHO(%%a:%%b
)
)>"%outfile%"

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances.

I used a file named q36242413.txt containing your data for my testing.

Produces the file defined as %outfile%

Simply read each line of the file and use :, as delims which means "both : and , are delimiters.

The lines therefore are assigned as (the part before the first sequence-of-delimiters) to %%a and (the remainder of the line) to %%b (this is the action of tokens=1*)

The usebackq tells cmd that the filename is quoted.

Therefore, %%a will acquire either #NAME or http and %%b the remainder of the line after the first delimiter-sequence, so test which is being used and echo out the appropriate replacement string.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

If you can accept VBS, then you should be able to work with JScript as well.

The JREPL.BAT regular expression find/replace utility is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.

The JREPL.BAT solution makes a solution nearly trivial:

jrepl "(^#NAME:,)(.*)" "$1mp3-id=\q$2\q,$2" /x /f test2.txt /o -

Use CALL JREPL if you put the command within a batch script.

Full documentation is available from the command line via jrepl /?, or use jrepl /?? for paged output.

dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Just a little tweak from the answer of Magoo

You can drag and drop your file text at this batch to convert your data :

@Echo off
Set "filename=%1"
For %%i in (%filename%) Do (set outfile="%%~ni_converted%%~xi")
(
    For /f "usebackq tokens=1* delims=:," %%a in (%filename%) Do (
    If /i "%%a"=="#NAME" ECHO(%%a:,mp3-id="%%b",%%b
    If /i "%%a"=="http" ECHO(%%a:%%b
    )
)>%outfile%
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

Hope this will help you..

VBScript Code:

filename = "C:\path\file.txt"
textToSearch= "#NAME:,"
attachText= "mp3-id="
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Set outputFile = fso.CreateTextFile("C:\path\output.txt", True) 
Do Until f.AtEndOfStream
  fileLine=f.ReadLine 
  if Instr(1,fileLine, textToSearch ) then
     reminingText=Replace(fileLine ,textToSearch , "")
     '#NAME:,mp3-id="Disco Ball",Disco Ball
     newLineText= textToSearch & attachText & chr(34) & reminingText & chr(34) & "," & reminingText
  else
     newLineText=fileLine
  End if
  outputFile.WriteLine(newLineText)   
Loop
outputFile.Close 
f.Close
Kiran Maroju
  • 174
  • 1
  • 2
  • 15