text file modification from a batch

Microsoft Windows xp professional w/serv...
April 21, 2010 at 14:10:09
Specs: Windows 7
Hello,

I need help to add some text at the begining of an existing line in a text file.

Example:

I have a file that contain:
line 1
line 2
line3

I need to find the good line (using findstr for example)
when the line is found I would like to add in front of it a small text such as "my comment"

I need to do this in a batch and I can't use sed or VB a C etc... only standard command


See More: text file modification from a batch

Report •

#1
April 21, 2010 at 15:34:44
@echo off > newfile & setLocal enableDELAYedeXpansion

set L=
for /f "tokens=1 delims=[]" %%a in ('find /n "string" ^< myfile') do (
set L=%%a
)
if not defined L echo no go && goto :eof

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
set S=%%a
if !L! equ !N! set S=comment !S!
>> newfile echo.!S!
)


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

M2


Report •

#2
April 21, 2010 at 23:44:12
Hello M2,
I clearly see now developpment is not my job, I spent close to a week to be far from what you made.
Many many thanks.

It works fantastic if I have one occurence of the searched string.

In my file I have some time 2 or 3 time a result for the string searched. Your script modify the last occurance.

Is there a way to catch each time the string is found in myfile.

Thanks,
Patrick


Report •

#3
April 22, 2010 at 03:32:38
@echo off > newfile & setLocal enableDELAYedeXpansion

set L=
for /f "tokens=1 delims=[]" %%a in ('find /n "string" ^< myfile') do (
  set L=!L! %%a
)

if not defined L echo no go && goto :eof

set N=
for /f "tokens=* delims= " %%a in (myfile) do (
  set /a N+=1
  set S=%%a
    for %%i in (!L!) do (
      if %%i equ !N! set S=comment !S!
    )
>> newfile echo.!S!
)


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

M2


Report •

Related Solutions

#4
April 22, 2010 at 05:24:28
Hello M2,

you are verry quick....

I use your script and it appears that the change is made if I have one or more occurance of the string but the comment is

applyed 4 lines bellow the target, I tried to change things but it come worth and worth with my fingers...

Can you help me again?
Thanks,
Patrick


Report •

#5
April 22, 2010 at 05:39:47
@echo off > newfile & setLocal enableDELAYedeXpansion

set L=
for /f "tokens=1 delims=[]" %%a in ('find /n "string" ^< myfile') do (
set L=!L! %%a
)
::echo L is !L! & goto :eof

if not defined L echo no go && goto :eof

set N=
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< myfile') do (
set /a N+=1
set S=%%b
  for %%i in (!L!) do (
    if %%i equ !N! set S=comment !S!
  )
>> newfile echo.!S!
)


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

M2


Report •

#6
April 22, 2010 at 05:50:38
Fantastic it work like a charm...
You are the king.
Many thanks

Report •

#7
April 22, 2010 at 08:21:14
Hello M2,

I was continuing to work (much netter) with your help and I had to do a second search.
This ppend quite often but I noticed that the file is re written each time.
This log is quite big so setting a new file for each search is not ideal.
Is there a way to have alle serch that will stay on the same file?
Many thanks,
Patrick


Report •

#8
April 22, 2010 at 08:43:32
Add this line at the end:

move /y newfile myfile


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

M2


Report •

#9
April 22, 2010 at 09:12:11
.......P E R F E C T .....

Many thanks,
Patrick


Report •

#10
April 23, 2010 at 13:01:51
Hello M2, it's me again, I have used your script into another batch like this.

for /f "usebackq delims=" %%a in ("%WinVer%") do (
call c:\deltaCopy\find_false_positiv.cmd %%a
)

Where WinVer is a file name and false positiv.cmd is the script you wrote with my parameters, here is it:

@echo off > rs_log_modifie.txt & setLocal enableDELAYedeXpansion
set identifie=%1
echo %identifie%
pause
set L=
for /f "tokens=1 delims=[]" %%a in ('find /n /i "%identifie%" ^< rs_log.txt') do (
set L=!L! %%a
)
::echo L is !L! & goto :eof

if not defined L echo no go && goto :eof

set N=
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< rs_log.txt') do (
set /a N+=1
set S=%%b
for %%i in (!L!) do (
if %%i equ !N! set S=FAUX_POSITIF !S!
)
>> rs_log_modifie.txt echo.!S!
)
move /y rs_log_modifie.txt rs_log.txt

My problem is the variable %identifie% should contain the value of my file but it appears that it works strangely.
I suspect the problem is on passing the variable from one batch to another but I'm not sure.

Can you help again?
Many thanks,
Patrick


Report •

#11
April 23, 2010 at 13:21:47
[1] I would name the bat BAT, not CMD.

[2] If WinVer has spaces you probably need "tokens=*"

[3] I dunno what usebackq is doing.


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

M2


Report •

#12
April 23, 2010 at 13:43:54
I) changed from cmd to bat and no differences
2) You are right WinVer contain line with space in it

I don't understand how to modify tokens because we have it twice in the batch and there is already quotes

"tokens=1 delims=[]"

For usebackq, unfortunately I don't know too, this is a piece of code I'm using since long time and I didnt understood this but it was working for me.

Thanks
Patrick


Report •

#13
April 23, 2010 at 14:01:16
Change this:

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

to this:

for /f "tokens=* delims=" %%a in ("%WinVer%") do (


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

M2


Report •

#14
April 23, 2010 at 14:29:13
Hello M2,

Sorry the execution of the script take quite a while and I woulk like to be as closed as possible to the reality.
So I changed with your recommandation and it seams that the variable is not defined as the no go comment is displayed and no one false positive is catched.


Report •

#15
April 23, 2010 at 14:38:13
What does this give?

echo %identifie%


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

M2


Report •

#16
April 23, 2010 at 14:42:51
I added the echo and pause command to see the results and this how I saw the no go answer like if the variable is not defined.

Report •

#17
April 23, 2010 at 15:58:10
Post the script you're using.


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

M2


Report •

#18
April 23, 2010 at 22:07:58
Hello M2,

the script does a lot of things and has all the information in French, so I'll post the part we are looking at.

I set this variable for all the OS (XP,Vista and 7)
set WinVer=
set WinVer=c:\DeltaCopy\Faux_Positif_WinVista.txt

Then I have an rsync command that generate the log rs_log.txt and I use this log like this in a bat file

for /f "tokens=* delims=" %%a in ("%WinVer%") do (
call c:\deltaCopy\find_false_positiv.bat %%a
)


and the call command execute your script with my parameters like this:

@echo off > rs_log_modifie.txt & setLocal enableDELAYedeXpansion
set identifie=%1
echo %identifie%
pause
set L=
for /f "tokens=1 delims=[]" %%a in ('find /n /i "%identifie%" ^< rs_log.txt') do (
set L=!L! %%a
)
::echo L is !L! & goto :eof

if not defined L echo no go && goto :eof

set N=
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< rs_log.txt') do (
set /a N+=1
set S=%%b
for %%i in (!L!) do (
if %%i equ !N! set S=FAUX_POSITIF !S!
)
>> rs_log_modifie.txt echo.!S!
)
move /y rs_log_modifie.txt rs_log.txt


Thanks for your help,
Patrick


Report •

#19
April 23, 2010 at 23:07:09
Oups I forgot to add the content of the files, here they are:

faux_positif_win7.txt (this file show de relative folders I have to check.

AppData/Local/Application Data
AppData/Local/Historique
AppData/Local/Temporary Internet Files
AppData/Roaming/Microsoft/Windows/Start Menu/Programmes
Application Data

And this is an extract of the log from the rsync command:

2010/04/22 09:36:05 [5876] building file list
2010/04/22 09:36:05 [5876] rsync: opendir "/cygdrive/c/Users/Patrick/AppData/Local/Application Data" failed: Permission denied (13)
2010/04/22 09:36:06 [5876] rsync: opendir "/cygdrive/c/Users/Patrick/AppData/Local/Historique" failed: Permission denied (13)
2010/04/22 09:36:07 [5876] rsync: send_files failed to open "/cygdrive/c/Users/Patrick/NTUSER.DAT": Device or resource busy (16)
2010/04/22 09:36:07 [5876] rsync: send_files failed to open "/cygdrive/c/Users/Patrick/ntuser.dat.LOG1": Device or resource busy (16)
2010/04/22 09:36:07 [5876] rsync: send_files failed to open "/cygdrive/c/Users/Patrick/ntuser.dat.LOG2": Device or resource busy (16)
2010/04/22 09:38:14 [5876] rsync: opendir "/cygdrive/c/Users/Patrick/AppData/Local/Temporary Internet Files" failed: Permission denied (13)
2010/04/22 09:39:00 [5876] .d..t...... AppData/Local/Mozilla/Firefox/Profiles/o5xoac25.default/
2010/04/22 09:42:49 [5876] rsync: opendir "/cygdrive/c/Users/Patrick/AppData/Roaming/Microsoft/Windows/Start Menu/Programmes" failed: Permission denied (13)
2010/04/22 09:43:26 [5876] rsync: opendir "/cygdrive/c/Users/Patrick/Application Data" failed: Permission denied (13)
2010/04/22 09:43:26 [5876] rsync: opendir "/cygdrive/c/Users/Patrick/Cookies" failed: Permission denied (13)
2010/04/22 09:44:14 [5876] .d..t...... AppData/Roaming/Microsoft/Windows/Recent/
2010/04/22 09:44:34 [5876] >f..t...... AppData/Roaming/Microsoft/Windows/Recent/DeltaCopy.lnk

I hope it will be lisible on the web site as the output lines are quite long in the log file.
Thanks again for your help,
Patrick


Report •

#20
April 27, 2010 at 09:26:03
I tried to play with delim and so on without success.
Do you have an idea?

Many thanks,
Patrick


Report •

#21
April 27, 2010 at 15:00:52
i would suggest quotes here:
call c:\deltaCopy\find_false_positiv.bat "%%a"
then if you do, you will need to remove them here:
for /f "tokens=1 delims=[]" %%a in ('find /n /i %identifie% ^< rs_log.txt') do (
::----
because sending this (%%a):
AppData/Local/Application Data
comes accross as this:
%1: AppData/Local/Application
%2: Data
and although you could do:
shift
set identifie = %*
it's easier to just use quotes, unless some of your data contains quotes which it doesn't look like.
but i don't think thats the whole problem because not all you filenames have spaces.

a whole nuther approach (well, a couple different ones)
this one will find all the filenames listed in %winver%:
findstr /g:%winver% rsync.log > modrsync.log

and this one will find only the failures, based on the (error#) pattern at ends of failure lines:
findstr /e /r "(.*)" rsync.log > modrsync.log



Report •

#22
April 28, 2010 at 02:29:32
Thanks you so much,

I'm tweaking the things according your remarks and it goes much better now.
I still have some glitches but I hope I will be able to sort that out.

Thanks again for your help.
Patrick


Report •

Ask Question