1.

Solve : Use batch file to insert text in a text file after a word i specify??

Answer»

To put it in plain terms guys n gals what i would like to know is if this is possible?

I have say 25 text files that have different information in them but at the bottom there is 2 lines the same in them all and i need to insert a further 2 lines because my database does not add them in while exporting them.

Would it be possible to create a batch file that would allow me to run it and upon running it edit these text files and insert two lines after a specific word which would save me a great deal of time doing it manually.

Any help will be much appreciated and thank you in advance.

Ryanpost example of this txt file and show us where you want to put them  well there is 25 different files all with information like the following:

20091105,20091105,1501-1502
20091112,20091112,1501-1502
20091119,20091119,1501-1502
20091126,20091126,1501-1502
20091203,20091203,1501-1502
20091210,20091210,1501-1502
20091217,20091217,1501-1502
20091224,20091224,1501-1502
]
SCHEDULES BAYS [
]
SCHEDULES CLEANUP
SYS CONFIG 20090427

I want to insert :

SYS DUEGATE 60 05:00
DISPLAY SCHED 00:30 01:30

after CLEANUP but on a new line so it would look like the following:

SCHEDULES CLEANUP
SYS DUEGATE 60 05:00
DISPLAY SCHED 00:30 01:30
SYS CONFIG 20090427

the text at the start is just a few lines from the document there are hundreds of lines. thanks if you can help you would be a LEGEND lol Code: [Select]echo off
setlocal enabledelayedexpansion
set num = 0

for /f "tokens=*" %%a in (file.txt) do (
if !num! lss 12 echo %%a >>output.txt
if !num! EQU 12 set lastline=%%a
set /a num+=1
)

echo.SYS DUEGATE 60 05:00 >>output.txt
echo.DISPLAY SCHED 00:30 01:30 >>output.txt
echo %lastline% >>output.txt

echo.DONE!
pause
this works for me after output.txt is created you can DEL file.txt and rename output.txt to file.txt

PS done fast, i need to sleep if you have Perl and able to use it.
Code: [Select]# perl -i.bak-ne 'print /CLEANUP$/?$_."SYS DUEGATE 60 05:00\nDISPLAY SCHED 00:30 01:30\n":$_' file*
20091105,20091105,1501-1502
20091112,20091112,1501-1502
20091119,20091119,1501-1502
20091126,20091126,1501-1502
20091203,20091203,1501-1502
20091210,20091210,1501-1502
20091217,20091217,1501-1502
20091224,20091224,1501-1502
]
SCHEDULES BAYS [
]
SCHEDULES CLEANUP
SYS DUEGATE 60 05:00
DISPLAY SCHED 00:30 01:30
SYS CONFIG 20090427
1. Create a folder C:\test1
2. Create a folder C:\test2
3. Move your 25 different files to C:\test1
4. Run below batch file
5. You'll get the result in C:\test2

Code: [Select]echo off
set SrcFolder=C:\test1
set DstFolder=C:\test2
for %%a in ("%SrcFolder%\*.txt") do (
  (for /f "tokens=1* delims=:" %%h in ('findstr /n .* "%%a"') do (
    echo.%%i
    if %%h equ 12 (
      echo SYS DUEGATE 60 05:00
      echo DISPLAY SCHED 00:30 01:30
    )
  ))>"%DstFolder%\%%~nxa"
)
guys wow replies were excellent and very fast thanks very much, this forum rocks.

i'll try them all and see which one works best for me but i CANT express how greatful i am to you all this is gonna save me a shed load of time and i can focus that elsewhere.

thank yous so much i'm really greatful Quote from: rytech on April 22, 2009, 06:16:19 AM

guys wow replies were excellent and very fast thanks very much, this forum rocks.

i'll try them all and see which one works best for me but i cant express how greatful i am to you all this is gonna save me a shed load of time and i can focus that elsewhere.

thank yous so much i'm really greatful

quick question to batcher, i noticed equ 12 does that insert the text i want on line 12 cause there is like hundreds of different lines and it is near the end and it is not on the same line anytime, i like the look of yours best as it seems the simplest so COULD you edit it to insert the text after the word i originally specified?

is that possible?if line 12 always have the word CLEANUP, then the batch solution should be ok. Otherwise, need to change it to something more flexible.does my solution work ?  Quote from: rytech on April 22, 2009, 06:22:01 AM
quick question to batcher, i noticed equ 12 does that insert the text i want on line 12 cause there is like hundreds of different lines and it is near the end and it is not on the same line anytime, i like the look of yours best as it seems the simplest so could you edit it to insert the text after the word i originally specified?

is that possible?

Everything is possible. Try this one.

Code: [Select]echo off
set SrcFolder=C:\test1
set DstFolder=C:\test2
for %%a in ("%SrcFolder%\*.txt") do (
  (for /f "usebackq delims=" %%h in ("%%a") do (
    echo.%%h
    if "%%h" equ "SCHEDULES CLEANUP" (
      echo SYS DUEGATE 60 05:00
      echo DISPLAY SCHED 00:30 01:30
    )
  ))>"%DstFolder%\%%~nxa"
)
Batcher your the man thanks very much buddy Devcom i never tried it mate cause the line number thing you had in yours but batchers done the job and i edited the batch file to delete the un needed folder once it had done it so it's all sorted but i need to know if it is possible to delete some text in the file and replace it, i need to replace the date with the date i am working with rather than the date set in the files as well it was just i forgot about that so i have:

ECHO OFF
set SrcFolder=C:\Test
set DstFolder=C:\Test1
for %%a in ("%SrcFolder%\*.txt") do (
  (for /f "usebackq delims=" %%h in ("%%a") do (
    echo.%%h
    if "%%h" equ "SYS CONFIG 20090327" (
      echo SYS CONFIG 20090427
    )
  ))>"%DstFolder%\%%~nxa"
)

Where SYS CONFIG 20090327 i want that replaced with SYS CONFIG 20090427, is that possible, this question goes to my man batcher lol or anyone with the knowledge cause i aint got it try to change this:
Code: [Select]    echo.%%h
    if "%%h" equ "SYS CONFIG 20090327" (
      echo SYS CONFIG 20090427
    )to
Code: [Select]    if "%%h" equ "SYS CONFIG 20090327" (
      echo SYS CONFIG 20090427
    ) else (
    echo.%%h
    )
should work devcom sorry to be a bloody pain but it says the system could not find the path specified, here is what i have, can you think of anything that looks wrong?

ECHO OFF
set SrcFolder=c:\test
set DstFolder=C:\test1
for %%a in ("%SrcFolder%\*.txt") do (
  (for /f "usebackq delims=" %%h in ("%%a") do (
    if "%%h" equ "SYS CONFIG 20090327" (
      echo SYS CONFIG 20090427
    ) else (
    echo.%%h
    )
  ))>"%DstFolder%\%%~nxa"
)

ECHO.
ECHO Done!
ECHO.
PAUSE
CLS


Discussion

No Comment Found