Solved Insert a serial number before specific word

September 23, 2011 at 07:42:29
Specs: Windows XP
Hello Dears can any one step in and help
I have the word "FEVER" repeat itself in thousands of files is there any way to insert number before this word so in the first occurence become 1FEVER and in the 2nd occurence 2FEVER and in the 3rd occurence 3FEVER and so on
thanks as many as the fever popup

See More: Insert a serial number before specific word

Report •


✔ Best Answer
October 6, 2011 at 01:53:20
Ibrahim - at last good news - thank you. If you really want to do something for me please make a small donation to your local Breast Cancer Support Group.

Your comment that there is minor problem if it can be fixed that will be great i.e the number is assigned randomly not according to the alphabetical order of the files is possibly caused by the Directory listing. I overlooked that I have the Dircmd environment variable set to list files in alpha order by name so just add the /on switch to the For loop so that it reads:

for /f "tokens=*" %%1 in ('dir /b /on *.htm') do (

Thank you for allowing me to view the files, very interesting, now all I need is a medicalese interpreter!!

Kind regards.

Wahine.


Please come back & tell us if your problem is resolved.



#1
September 23, 2011 at 14:10:58
You'll have to change the paths, and test this, but it should work.

@echo on
SETLOCAL ENABLEDELAYEDEXPANSION
REM using dir to obtain a list of the .jpg file names in c:\folder
dir /b c:\folder\fever* > filelist.tmp
REM declaring a variable that will be incremented to create the numbered
REM file names you wanted
set numprefix=0
REM looping through the file list we created earlier
for /F "tokens=*" %%A in (filelist.tmp) do (
REM adding 1 to numprefix variable to create next number filename
set /a numprefix=!numprefix!+1
REM renaming original file to be current value of numprefix variable .jpg
ren "c:\folder\%%A" !numprefix!%%A.jpg
)
REM removing the temporary file list file
del /q /f filelist.tmp
ENDLOCAL


Report •

#2
September 23, 2011 at 20:27:51
Hi JOwah
you are great thanks a lot I will try it and give u the result

Report •

#3
September 23, 2011 at 21:28:10
Hello Jowah thanks Dear
the job has n't done the word fever is found in each htm file I sent you just 10 to carry the test on them
http://ifile.it/6h4l7ae
I want to insert a number before the word fever i.e 1 in the 1st file 2 in the 2nd 3 in the third etc
please do the job and leave for me the hard and difficult task i.e copy and past

thanks Jowah again


Report •

Related Solutions

#4
September 23, 2011 at 21:45:49
Ok I tested it this time, and it works. You will have to change the path in line 4 and in line 13. Change the c:\folder\ path to the actual path that contains the files on your machine.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM using dir to obtain a list of the .jpg file names in c:\folder
dir /b c:\folder\*fever* > filelist.tmp
REM declaring a variable that will be incremented to create the numbered
REM file names you wanted
set numprefix=0
REM looping through the file list we created earlier
for /F "tokens=*" %%A in (filelist.tmp) do (
REM adding 1 to numprefix variable to create next number filename
set /a numprefix=!numprefix!+1
REM renaming original file to be current value of numprefix variable .jpg
ren "c:\folder\%%A" "!numprefix!%%A.jpg"
)
REM removing the temporary file list file
del /q /f filelist.tmp
ENDLOCAL


Report •

#5
September 25, 2011 at 07:18:29
Hello Jowah
I am sorry to tell u the negative result
the batch work on the names of files
what I want is to search inside the files (contents of the files) and insert the serial number inside (I see jpg extension what that does mean??)
can u please modify it

if the batch file is afraid of getting-in because of fever please let it have some antipyretics !!!!!!!!!!

Thanks jowah


Report •

#6
September 25, 2011 at 18:38:38
I see what you mean now ibrahim10. This is possible, but now I need to figure out how to get an accurate list of file names to search through. Are all of your files in the same directory? If so, we could skip this step, and go straight into using findstr to find the string "fever" on each line in each file.

Report •

#7
September 25, 2011 at 23:51:38
Thank you very much Jowah
yes Dear all are in the same folder

Report •

#8
September 26, 2011 at 06:10:42
I found this batch at http://en.irfanview-forum.de/vb/sho... because I don't have time this morning to work on this issue. The finished product will be similar to this except that yours will increment the number in front of "fever".


@echo off
setlocal enabledelayedexpansion

if not exist "%1" (echo this file does not exist...)&goto :eof

set /p findthis=find this text string:
set /p replacewith=and replace it with this:

for /f "tokens=*" %%a in (%1) do (
set write=%%a
if %%a==%findthis% set write=%replacewith%
echo !write!
echo !write! >>%~n1.replaced%~x1
)


Report •

#9
September 27, 2011 at 00:32:13
Change input/output paths/filenames to suit and test this. It will work only if FEVER does not appear more than once on any line.

@echo off
cls
setlocal enabledelayedexpansion

if exist %temp%\trial1.txt del %temp%\trial*.txt

set outfile=0
set cnt=0

for /f "tokens=*" %%1 in ('dir /a-d /b trial*.txt') do (
    set infile=%%1
    set /a outfile+=1

        for /f "delims=*" %%A in (!infile!) do (
            set inline=%%A
            (
            echo !inline!|findstr /I "fever">nul &&set /a cnt+=1
            call set inline=%%inline:fever=!cnt!fever%%
            )
            echo !inline!>>%temp%\trial!outfile!.txt
  )
set cnt=0    
)


Please come back & tell us if your problem is resolved.


Report •

#10
September 27, 2011 at 06:06:08
That is pretty slick Wahine. Thanks for posting that.

Report •

#11
September 27, 2011 at 10:09:56
Warm welcome Wahine and Jowah thank you for your help
both are extraordinary good persons
thanks again

Report •

#12
September 27, 2011 at 23:32:32
Hello thank you wahine you are still in my recent memory
I failed to get result either it hang or say the file is not found

I put all files (they are.HTM) in D:\try1 and I left D:\try2 an empty folder for the output files
I guess the script work line by line in one file but what I want is the first occurence of the word (Fever) in each file
In fact the word Fever occur only once in each file
Can you please revise it and test it on the files in this link


http://ifile.it/6h4l7ae


Report •

#13
October 1, 2011 at 20:32:50
I haven't lost hope !!!!!!!!!!!!!!

Report •

#14
October 3, 2011 at 22:46:24
Hi
before the post is locked I would like to thank every member in this forum who offer help to others in return of no thing even if I am not the target of their help
but I am under obligation in person to thank Wahine and Jowah for their help
my best regards

Report •

#15
October 3, 2011 at 23:51:04
A still devoting some time to your request but as the files are .htm that brings in other problems.

You also state In fact the word Fever occur only once in each file while fever appears 7 times in 107a, 4 times in 109a, 3 times in 110a, 4 times in 111a and twice in 112a. If only your statement was true it would simplify things.

I will get back to you by the weekend to report either success or failure.

Wahine.


Please come back & tell us if your problem is resolved.


Report •

#16
October 4, 2011 at 10:17:52
Thanks a lot wahine I appreciate your invaluable help
I mean if I get only one word (Fever) from each file that is the only thing I need because when the word repeated in the same file it refers to same cause(disease)
so I need only one from each file otherwise the disease will be referred to, by several numbers which will not serve my purpose i.e differential diagnosis

I am indebted to you for your help
Thanks


Report •

#17
October 4, 2011 at 23:03:02
Hello Wahine

I replaced Fever with pyrexia and left only one word of fever in each file if that will help?
thanks

the new link
http://ifile.it/8ih5qjk


Report •

#18
October 5, 2011 at 17:43:18
Ibrahim10 - I replaced Fever with pyrexia and left only one word of fever in each file if that will help? That's a brilliant move and makes life a lot easier. Putting Fever in the first line of each file also means that all the Pyrexias can be changed back to fever if you wish to do so.

I think the following script will do what you require and have used the More command to copy the files in order to maintain the layout. The script must be fully tested. The script is heavily commented and all Rem: command lines may be removed at your discretion.

Fingers crossed awaiting your response.

@echo off
cls
setlocal enabledelayedexpansion

set nbr=1

Rem: Create output folder.
if not exist c:\output\ md c:\output\

Rem: Make Input folder the temporary default folder
pushd c:\input\||echo Pushd failed - job terminated&&exit /b

Rem: Select the file name from a directory listing
for /f "tokens=*" %%1 in ('dir /b *.htm') do (
    set infile=%%1

Rem: Extract first line from the file selected
    set /p inline=<!infile!

Rem: Update first line to include the number required
        for /f "tokens=1*" %%A in ("!inline!") do (
          set first=!nbr!%%A

Rem: Write updated first line to output file
          echo !first! %%B>c:\output\!infile!

Rem: Use the More command to copy balance of file to output
          more +1 !infile!>>c:\output\!infile!

Rem: Increment number for next file
          set /a nbr+=1
    )
)

Rem: Display directory listing of output folder
dir c:\output\


Please come back & tell us if your problem is resolved.


Report •

#19
October 5, 2011 at 23:50:54
Hi Wahine
worked like a charm it is fantastic
You have done a lot of work to help me You solved my problem with Fever I wish I could do you a favour !!!

there is minor problem if it can be fixed that will be great i.e the number is assigned randomly not according to the alphabetical order of the files
But please don't invest your valuable time for this point

Thanks a lot with my best regards


Report •

#20
October 6, 2011 at 01:53:20
✔ Best Answer
Ibrahim - at last good news - thank you. If you really want to do something for me please make a small donation to your local Breast Cancer Support Group.

Your comment that there is minor problem if it can be fixed that will be great i.e the number is assigned randomly not according to the alphabetical order of the files is possibly caused by the Directory listing. I overlooked that I have the Dircmd environment variable set to list files in alpha order by name so just add the /on switch to the For loop so that it reads:

for /f "tokens=*" %%1 in ('dir /b /on *.htm') do (

Thank you for allowing me to view the files, very interesting, now all I need is a medicalese interpreter!!

Kind regards.

Wahine.


Please come back & tell us if your problem is resolved.


Report •

#21
October 6, 2011 at 22:40:58
Thanks Wahine
you are great and I am grateful to you
every thing now is fine thanks again

Report •


Ask Question