Please help me get started with scripts I would like to automate modifying some html files
#1
Posted 20 December 2010 - 09:52 AM
The first thing I would like to change is that the program generates thumbs but puts them in the same folder as the original images. I would like a 'thumbs' directory as a sub-directory to the original image folder. All of the thumbs are generated with names ending _small. So my first piece of script needs to take all the files with names ending '_small.jpg' and move them to a new folder within the same directory called 'thumbs'. It then needs to edit all html files within the directory so that where ever there is a link to an _small.jpg image we need thumbs\ put in front of that file name. For example:
This <td class="thumbcell"><a href="kar100.html"><img src="kar100_small.jpg" title="kar100" alt="kar100_small.jpg" /></a></td>
must become <td class="thumbcell"><a href="kar100.html"><img src="thumbs\kar100_small.jpg" title="kar100" alt="kar100_small.jpg" /></a></td>
This code should only be applied if thumbs\ is not all ready there as I don't want it to duplicate the modification.
The other thing I would like to be able to do is automate the removal of a specific line of code from all html files with a directory. The reason for this is that the program I am using places an ad for itself at the bottom of each webpage. I am not publishing the pages online as I am only using this for personal organisation of my photo albums so I just find the ad bugs me and should not upet anybody if I remove it. So I would like to automate the removal of the following line of text from all of my html files:
<p class="footnote">Photo album created with <a href="http://www.ornj.net/">Web Album Generator</a></p>
If anybody can help me to achieve these goals and get me started with writing and executing my own scripts that would be much appreciated.
Thanks
#2
Posted 20 December 2010 - 10:53 AM
What programming experience do you have?
#3
Posted 20 December 2010 - 11:02 AM
This post has been edited by Stephen W: 20 December 2010 - 11:41 AM
#4
Posted 20 December 2010 - 12:57 PM
Moving the files is pretty simple.
1. Create a directory if it doesn't already exists
2. Build a list of files in the old directory.
3. Copy the files into the new directory.
A bunch more examples.
PRO-TIP: Be very careful when testing your code, especially if it has to do with deleting or moving files. If you do not pay attention, you could delete everything on your computer.
Changing the HTML is harder. Basically you have to read the entire html file, line by line. loop through it looking for the proper HTML tags that you want, splitting those lines of text down further until you fins the attributes and values that you want, modifying them, and then rewriting the entire thing back to a file again.
Some string functions here.
#5
Posted 20 December 2010 - 01:16 PM
If that is asking too much maybe you could help me get up and running with the easiest bits first. Could you write me the code that will go in a file that once executed in a chosen directory will:
1. create folder 'thumbs'
2. move any file ending _small.jpg into thumbs
3. examine all html files in the directory and delete the line: <p class="footnote">Photo album created with <a href="http://www.ornj.net/">Web Album Generator</a></p>
I'm guessing that adding 'thumbs\' to the appropriate lines of the html files in the right place may be a fair bit more work?
This post has been edited by Stephen W: 20 December 2010 - 01:25 PM
#6
Posted 20 December 2010 - 02:59 PM
#7
Posted 21 December 2010 - 08:57 AM
CreateFolder Method
This method allows us to create a folder.
Syntax: object.CreateFolderfoldername
So I would think from that I would need 'object.CreateFolderthumbs' as the syntax to create my thumbs folder. However, I do not know how to execute this command in a specific directory. Can I do this by simply creating a text document containing that syntax and naming it 'thumbs.cmd' then placing it in the relevant directory before double clicking it? Would this code automatically work within Windows 7 or would I have to install some program? That's pretty much a total guess from me but perhaps demonstrates my current understanding of this. But if I can figure out how to execute the code that creates the thumbs directory I should be able to work on the rest with more confidence.
#9
Posted 22 December 2010 - 01:08 PM
Actually moving the files is really easy.
Open the folder you want to move them from.
Type *small.jpg into the Windows Search bar in the top right corner of Windows explorer.
Drag all the matching files into the folder you want to move them too.
I'm also staring at a notepad++ window and thinking how easily it can perform a mass search and replace on multiple files (html files in this case) to turn a simple string (such as the advert at the bottom of the page) into nothingness. That would get rid of half the problem at least.
As for the problem of writing the actual script to change , I would be willing to take it on if Groovicus is too busy. I'm a student, and if doing something is the best way to learn about it I'm all for doing it.
This post has been edited by JosiahK: 22 December 2010 - 01:09 PM
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.
#10
Posted 22 December 2010 - 01:39 PM
Thanks
#11
Posted 22 December 2010 - 03:39 PM
#12
Posted 22 December 2010 - 05:02 PM
But I decided th take an example of creating a folder in VB script and see if I could get it to work.
Here is the example - I just changed the drive and the folder name
strFolder = "K:\mydir\testfld2"
set objFSO = createobject("Scripting.FileSystemObject")
if objFSO.FolderExists(strFolder) = False then
objFSO.CreateFolder strFolder
wscript.echo "Folder Created"
else
wscript.echo "Folder already exists"
end if
I used notepad to put it in a file with a .vbs extension.
I then went to the folder where I put the vbs file and clicked on it . It created the folder and gave me the message.
I doubt I can help you much - don' t have the knowledge and not sure I'll have the time - but thought this might get you started. Maybe Josiah can help you more.
NOTE: I think the example will only creates the last level - in my example K:\mydir would have to exist in order for it to create tetsfld2
If you are really interested in learning to write your own scripts, I would suggest you start leaning by building the script you want a l ittle at a time. Write a part of the script - such as the create folder - and test it. Then add some other code. Starting with an already written script can be a way to learn also,. Play around with it some - try some different things to see what it does. This is just my opinion. If you do play with it, BE CAREFUL. Create some test data to play with and above all, make sure you have backups!!!!!!! I have deleted a lot of things I didn't meant to.
Fay
This post has been edited by FayB: 22 December 2010 - 05:50 PM
#13
Posted 22 December 2010 - 05:27 PM
I do not yet know, and am trying to find out, how to navigate a folder in VBScript to select all the HTML files within that folder. I am keeping as a backup the option of executing this script from a batch file (which are very good at working with files and folders), but would prefer to work purely in VBS if possible.
What it should do at present is work through the file specified in the variable named FN (on line 8), prepending "thumbs/" to any text ending "_small.jpg". It also removes the advert text you specified in your first post.
I would appreciate it if you could point it to a html file that needs working on (preferably a copy, so that you don't risk breaking the original), and check that it's working as you need it to.
Being unfamiliar with working the file system from VBS, I'd prefer it if you could use Windows Search to move the thumbnails into their folder.
Dim FN ' Name of the file. Will be passed from Batch.'
Dim ParseFile ' Entire contents of the HTML file'
Dim TS ' The string constant "_small.jpg"'
Dim FS ' Numeric location of the string TS in UnParsedFile'
Dim FQ ' Numeric Location of the quote mark " before _small.jpg.'
Dim LFS ' Last location of the end of the last TS found'
FN = "C:\Users\Josiah\Desktop\testfile.txt"
TS = "_small.jpg"
LFS = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(FN)
Set objReadFile = objFSO.OpenTextFile(FN, 1)
ParseFile = objReadFile.ReadAll
objReadFile.Close
'Scans for _small.jpg and prepends "thumbs\" to the file name'
Do
FS = Instr(LFS, ParseFile, TS)
If FS > 0 then
FQ = FS
Do
FQ = FQ - 1
Loop until mid(ParseFile, FQ, 1) = CHR(34)
If not mid(Parsefile, FQ+1,5) = "thumbs\" then
ParseFile = left(ParseFile, FQ) + "thumbs\" + right(ParseFile, len(ParseFile) - FQ)
Else
Wscript.Echo "Already Thumbs"
end if
LFS = FS + 17
end if
Loop until FS = 0
'Gets rid of the Ornj advert'
Dim DelStr
DelStr = "<p class="+chr(34)+"footnote"+chr(34)+">Photo album created with <a href="+chr(34)+"http://www.ornj.net/"+chr(34)+">Web Album Generator</a></p>"
Do
FQ = Instr(1, ParseFile, DelStr)
If FQ > 0 then
If len(ParseFile) > FQ + Len(DelStr) then
ParseFile = left(ParseFile, FQ - 1) + Right(Parsefile,len(ParseFile) - FQ - Len(DelStr) - 1)
else
ParseFile = left(ParseFile, FQ - 1)
end if
end if
Loop until FQ = 0
Set objFile = objFSO.OpenTextFile(FN, 2, True)
objFile.writeline(ParseFile)
Wscript.Echo "File Parse Complete" + vbCrLf + FNEdit: Can you please confirm that all of the HTML files that you need to process are stored in the same folder.
This post has been edited by JosiahK: 22 December 2010 - 06:18 PM
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.
#14
Posted 22 December 2010 - 07:26 PM
It will process all .htm and .html files in the root of that folder, not dealing with subdirectories.
It will handle the last two of your 3 criteria:
It will remove the Advert
It will add the text "thumbs\" where appropriate before _small images. It doesn't perform that change twice.
It will not move the thumbnail images as that is a task I believe can be better handled within Windows Explorer. It's also a task that didn't integrate nicely with the rest of the script and would be just another place for problems to creep up.
It will fail to identify and process images where the _small.jpg is not entirely in lowercase. The technique I believe I could use to correct for this involves creating a copy of the file entirely in upper or lower case, and this horrifically increases the overall processing time. I guessed that if the pages have been made by a computer system this critical string should be entirely lowercase (or at least consistently cased) on every page anyway, so this possible issue should not arise. If for example every instance is actually "_Small.JPG" then you just have to change line 13 to reflect that.
While I have tested this script as best I can on my own computer, I strongly advise that you make a backup copy of your files before executing the code. Perhaps that is merely a nerd's paranoia for backups talking, but I don't want to be responsible for worsening any situation I join to try to help.
Quote
In order to run the script you first need to Open Notepad.
Copy and Paste the contents into the Notepad Window.
Modify Line 14 to point to the folder in which your HTML files are found.
From Notepad Click File and Save as.
Save as any appropriate name ending with .vbs
Double-click the .vbs file to run it.
Although working on 3 mini pages on my machine it was near-instantaneous the program may take a while to complete if you have a lot of long HTML files. Once Finished it will beep and pop up a list of all pages processed.
Dim CFN ' Current Folder Name
Dim CF ' Current Folder, Object
Dim FileNames ' Array of File names in current folder.
Dim FN ' Name of the file. Will be passed from Batch.'
Dim ParseFile ' Entire contents of the HTML file'
Dim TS ' The string constant "_small.jpg"'
Dim FS ' Numeric location of the string TS in UnParsedFile'
Dim FQ ' Numeric Location of the quote mark " before _small.jpg.'
Dim LFS ' Last location of the end of the last TS found'
Dim AllFileNames ' Records all files that have been processed.'
Dim DelStr ' ornj advert to remove'
DelStr = "<p class="+chr(34)+"footnote"+chr(34)+">Photo album created with <a href="+chr(34)+"http://www.ornj.net/"+chr(34)+">Web Album Generator</a></p>"
TS = "_small.jpg"
CFN = "C:\Users\Josiah\Desktop"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set CF = objFSO.GetFolder(CFN)
Set files = CF.Files
' Outside loop, loops through files'
For each objFile In files
FN = objFile.name
' Checks for HTML extension'
If Ucase(right(FN,4)) = ".HTM" or UCASE(right(FN,5)) = ".HTML" then
LFS = 1
Set objFile = objFSO.GetFile(FN)
Set objReadFile = objFSO.OpenTextFile(FN, 1)
ParseFile = objReadFile.ReadAll
objReadFile.Close
'Scans for _small.jpg and prepends "thumbs\" to the file name'
Do
FS = Instr(LFS, ParseFile, TS)
If FS > 0 then
FQ = FS
Do
FQ = FQ - 1
Loop until mid(ParseFile, FQ, 1) = CHR(34)
If not mid(Parsefile, FQ+1,5) = "thumbs\" then
ParseFile = left(ParseFile, FQ) + "thumbs\" + right(ParseFile, len(ParseFile) - FQ)
end if
LFS = FS + 17
end if
Loop until FS = 0
'Gets rid of the Ornj advert'
Do
FQ = Instr(1, ParseFile, DelStr)
If FQ > 0 then
If len(ParseFile) > FQ + Len(DelStr) then
ParseFile = left(ParseFile, FQ - 1) + Right(Parsefile,len(ParseFile) - FQ - Len(DelStr) - 1)
else
ParseFile = left(ParseFile, FQ - 1)
end if
end if
Loop until FQ = 0
' Writes the modified file contents back into place'
Set objFile = objFSO.OpenTextFile(FN, 2, True)
objFile.writeline(ParseFile)
' Records this file in a variable as having been processed'
AllFileNames = AllFileNames + vbCrLf + FN
End if
Next
'Identifies which files have been modified'
Wscript.Echo "File Parse Complete" + vbCrLf + AllFileNames
This post has been edited by JosiahK: 22 December 2010 - 07:28 PM
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.
#15
Posted 22 December 2010 - 08:23 PM
groovicus, on 20 December 2010 - 02:59 PM, said:
Hey, i couldn't help but notice that you're such a pro and that you do this for a living!
As a computing student myself, I'd like to do the same kinda thing in the future :D
Was wondering what programming languages you excel in!
Thank yooooou,
CrimsonSpider
(Mosher’s Law of Software Engineering)

Help


Back to top











