Insert Filename into file using find and repl

December 24, 2009 at 16:08:19
Specs: Windows Vista
I have about 80,000 html webpages that I have to do Search Engine Optimization for.
In order to do this, I need to paste the name of each file into the actual file (specifically into the metat ags)
Do you guys know of a script or
utility/program that can take the filename and paste it into the file contents?
like if a file is named 1x9.html,
the program would paste the filename into the file.
I need to do this for thousands of files, and I have no idea how to approach this.
I have vista, and dreamweaver.
Please Help!

See More: Insert Filename into file using find and repl

Report •


#1
December 24, 2009 at 19:40:46
insert the line? or modify an existing line to add filename?
based on line#? or context (f/e: insert after line:
<meta content=All name=robots>

(i'm using winXP pro)


Report •

#2
December 24, 2009 at 20:23:14
Based on context
I have to put the file name into the metatags.
How can I do this?

Report •

#3
December 25, 2009 at 01:33:19
Somebody here may hammer it out if you give specifics.

For most find & replace jobs try CHANGE by Bruce Guthrie. [No relation, I suppose to Woodie & Arlo.]


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

M2


Report •

Related Solutions

#4
December 25, 2009 at 04:20:23
If you want to use batch processing to alter an XML file, you would need to show us an example of how this XML file is built, since batch programmers usually have no clue how XML files are being build, apart from the fact they have a complex syntax.

Report •

#5
December 25, 2009 at 15:33:34
ok here are the specifics:
I have a folder, in which there are like 400 subfolders.
In each of these subfolders, I have hundreds of htm and html files.
All of these files have this in their code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> [TITLE OF EACH PAGE]</title>
<meta name="description" [INSERT FILENAME HERE]


( the title varies from page to page, and is not important in this. I need to insert the filename of each specific file into its html code, specifically into the metatags.)

I have about 80,000 files total I need to do this for,
and I need to do this for search engine optimization.
I have adobe dreamweaver find and replace, but unfortunately I do not think it can inject the filename of each file into the file contents.
This is pretty much it.
I have windows vista by the way



Report •

#6
December 25, 2009 at 23:42:49
man, i learned a buttload from working on this one! I learned from prior post that !xx! in the inputfilestream will bomb your script if enabledelayed... If no enab, then i couldn't pass the filename into the loop. then I got to learn about levels of sets.
So then I learned about layers of sets and how they work.
THANKS!
here is a sketch, to start out with, that i worked with. it has all the crap left in where i was working. then there's a snip and a "cut-down" version i hope will start you off.
junky debugging/prototype (for testing)

@echo off>tempf & setlocal
for /f "tokens=* delims=" %%b in ('dir /s /b *.htm) do (
set fil=%%b
set modf=%%b.new
echo off>%%b.new
rem SET LEVEL TWO --------------------

setlocal enabledelayedexpansion

::---------------- MAIN FILE-READ FOR-LOOP ----------------
for /f "tokens=* delims=" %%a in (!fil!) do (
REM ------------ LINEREAD LOOPTOP: ------------------------
set /a nn+=1
REM now disable to allow ! to come in without altering inputstream data

REM MINUS ONE LEVEL ------------------
endlocal
REM PLUS ONE LEVEL: ENABLED ------------------
setlocal enabledelayedexpansion
echo A IS "["%%a"]"

set xx=%%a
set xx=!xx:~0,7!
echo XX XX XX [!xx!]
if /i "!xx!" equ "<title>" call :10
REM MINUS ONE LEVEL -------------------------
endlocal

echo outputting...[%%a]
echo %%a>>tempf
REM --------------------- END-FOR -------------
ECHO LOOPBACK...
PAUSE
)
set /p xx=renam
ren tempf %%b.new
)
goto :eof

:10
echo T E N: fil=[!fil!]
pause
ECHO ----------------------- >>tempf
echo ^<meta content=!fil!^>^</meta^> >>tempf
goto :eof

<snip---------- here's the "working" prototype: >

@echo off>tempf & setlocal
for /f "tokens=* delims=" %%b in ('dir /s /b *.XML') do (
set fil=%%b
set modf=%%b.new
echo off>%%b.new
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (!fil!) do (
setlocal enabledelayedexpansion
set xx=%%a
set xx=!xx:~0,7!
if /i "!xx!" equ "<title>" call :10
endlocal
echo %%a>>tempf
)
ren tempf %%b.new
)
goto :eof

:10
echo ^<meta content=!fil!^>^</meta^> >>tempf
goto :eof


Report •

#7
December 25, 2009 at 23:53:29
Hey, how would I run the script you posted above? can you refer me to a program or something? I am pretty new to this.

Some more details:
the mainfolder is located:
C:\Users\PC\Desktop\video
(i could rename this to C:\Users\PC\Desktop\1)
and I have a bunch of subfolders in this folder
(I can rename them to numbers 2-500) (i dont really know exactly how many folders I have)
in each of the subfolders I have hundreds of html and htm files
all of the filenames are like 1x1.html, or 1x10.html.
all of the filenames are longer that 2 characters, but shorter than 10 characters.
its like this
Mainfolder>450 or so Subfolders>Files (no folders) in each of the subfolders
(there are no folders inside the subfolders, only html and htm files)


Report •

#8
December 26, 2009 at 01:44:19
OP said:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> [TITLE OF EACH PAGE]</title>
<meta name="description" [INSERT FILENAME HERE]
===================================

Not enough detail for me. Is there JUST ONE line to be altered? Is it always the same position in the file? Is it always the last line?

Does this:

<meta name="description" [INSERT FILENAME HERE]

mean change this:

<meta name="description"

to this:

<meta name="description" 1x1.html

or what?


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

M2


Report •

#9
December 26, 2009 at 09:49:35
Yea, all I need to do is inject the filename into the file
like this:
<meta name="description" 1x1.html

the position of the line where <meta name="description" is located may vary from file to file, but I can reformat all my files using dreamweaver search and replace, to make the <meta name="description"
on a specific line if it is neccessary

i ALWAYS need the filename after the <meta name="description", so if the filename was 1x1.html, it is like this:
<meta name="description" 1x1.html


Report •

#10
December 26, 2009 at 11:17:18
here's a better one than i put last night. to work it, put it into the primary folder
(C:\Users\PC\Desktop\video) and run it.
I'm not sure: 1) if you wanted the fullpath to the filename in the "meta" tag, (prob'ly not)
2) if you wanted the "closing tag" (</meta>) but i think you can easily see where to make the changes you will need to adapt.

@echo off>tempf & setlocal
for /f "tokens=* delims=" %%b in ('dir /s /b *.HTM*') do (
set filhtm=%%~dpnxb
set mm=%%~xb
set newname=%%~dpnb
:: disable/remove the foll. if you DO want the fullpath of filename in the tags
set fil=%%~nxb
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in (!filhtm!) do (
endlocal
echo [%%a]
echo %%a>>tempf
set xx=%%a
setlocal enabledelayedexpansion
set xx=!xx:~0,6!
if /i "!xx!" equ "<title" call :10
)
:: output xxxx.HTM to xxxx.NTM, xxxx.HTML to xxxx.NTML
set newname=!newname!.n!mm:~2!
:: enable foll if you want to directly overwrite the htm files
:: not recommended until a full trial has been run!!!
:: edit: i also fixed this should you decide to use it
:: set newname=!filhtm!
echo sending output to: !newname!
pause
move /y tempf !newname!
endlocal
)
goto :eof

:10
:: swap foll. lines if you DON'T want the endtag
::echo ^<meta name="description" !fil! >>tempf
echo ^<meta name="description" !fil!^>^</meta^> >>tempf
goto :eof

:://* end script

i left all the safeties enabled so it can be tested safely (we hope) so all the files are fname.NTM(L) and a pause before creating each file which i'm sure you will want to take out before running all 80 thousand!


Report •

#11
December 26, 2009 at 15:12:18
Wow thanks! that actually worked.
There is just 1 modification I need to make.
If I put this script into the main directory
(C:\Users\PC\Desktop\video)
it runs and it says it cannot find the specified file.
however, if I put into the actual subfolder, it works perfectly fine.
This is how I have my folders set up:
Main folder (C:\Users\PC\Desktop\video)
Subfolders (C:\Users\PC\Desktop\video\24 is one of the subfolders)
Lots of html and htm files.

Anyways, I was just wondering if you can make it so the script works from the main folder, so I could just leave it on overnight on my computer.
Here are the specific errors it gives me when I try to run the script from C:\Users\PC\Desktop\video

The system cannot find the file 1x1.html
the system cannot find the specified drive
sending output to: C:\Users\PC\Desktop\video\24\1x1.html

Can you please fix this?


Report •

#12
December 26, 2009 at 17:54:06
just add a line, at the very top, right after this "for"
for /f "tokens=* delims=" %%b in ('dir /s /b *.HTM*') do (

::add this line, right beneath the "for" statement:
set filhtm=%%~dpnxb

::and change the second for loop to this:
for /f "tokens=* delims=" %%a in (!filhtm!) do (

i forgot to test it from the root, so i didn't uncover this omission. (this is assuming you don't want the fullfiledrivepathname inserted into the html, that's why there needs to be two different filenames, otherwise you could get by with just the fullname.
I will also edit my last post to show these changes in case you prefer to cut/paste the code instead of doctoring what you have.

ps: i think you or someone has already fixed the javascript error... i went to the page and worked it, also viewed the script within the html document and it was different than yours.
the page seemed to work...
i'm not very handy with vbscript or jscript...


Report •


Ask Question