Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Please help me get started with scripts


  • Please log in to reply
80 replies to this topic

#1 Stephen W

Stephen W

  • Members
  • 210 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Norwich, UK
  • Local time:11:11 AM

Posted 20 December 2010 - 09:52 AM

Hi guys. I am using a Web Album Generator program to create many web albums. Whilst the program is very quick and easy to use it does not output the webpages exactly as I would like and I cannot find anything better. As a result I have been spending hours manually modifying all the html files to make them as I want them. I would like to automate these modifications to save me a lot of time.
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

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:11 AM

Posted 20 December 2010 - 10:53 AM

An HTML file is simply a text file with a different extension, so there is nothing magic about altering them. This is something that can be accomplished pretty easily with any programming or scripting language. Moving files is trivial also.

What programming experience do you have?

#3 Stephen W

Stephen W
  • Topic Starter

  • Members
  • 210 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Norwich, UK
  • Local time:11:11 AM

Posted 20 December 2010 - 11:02 AM

I have not much programming experience to speak of. I did a little Pascal at Uni and have done some Visual Basic for macros in excel. But I really don't know any programming languages - however, I am pretty good with the general concepts. I manage to do a little html by trial and error of modifying bits and pieces in notepad. So I know what modifications need to be made. I figured that somebody with experience would be able to tell me how to create some sort of command file that I could execute within the desired folder to carry out the instructions required. I would think this would be a fairly simple task for somebody experienced? And then I figured I would be able to use the code to play around with and perform other tasks in a similar way to how I edit and write html. Or am I being a bit naive in how simple the tasks I've outlined would be to program?

Edited by Stephen W, 20 December 2010 - 11:41 AM.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:11 AM

Posted 20 December 2010 - 12:57 PM

You are not being naive, but some of the tasks may take a few tries. For starters, I would recommend VBScript as the scripting language. Using Batch files would be limiting. The hard part will be parsing the HTML.

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 Stephen W

Stephen W
  • Topic Starter

  • Members
  • 210 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Norwich, UK
  • Local time:11:11 AM

Posted 20 December 2010 - 01:16 PM

Thanks for that info. But would it be too much to ask for you to take a first try at writing the code for me and telling me how it works and how to execute it? It would be nice to see something close to what I am working towards. If I had something that put me half way there, I think I could probably work out how to adapt the code to make it work as wanted. I am pretty much a newbie to this so a foot up on the ladder would be excellent if you have the time.


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?

Edited by Stephen W, 20 December 2010 - 01:25 PM.


#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:11 AM

Posted 20 December 2010 - 02:59 PM

I write code for a living, so most of my time is taken up with that, and writing code for my own projects. I gave you links to examples that show you exactly what to do; that's really the most help I can offer.

#7 Stephen W

Stephen W
  • Topic Starter

  • Members
  • 210 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Norwich, UK
  • Local time:11:11 AM

Posted 21 December 2010 - 08:57 AM

OK well I'm willing to have a go at working it out myself but I'm struggling to get off the ground. Looking at the first link you gave me, I found:
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.

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:11 AM

Posted 21 December 2010 - 01:05 PM

I'll try to free up a little time this evening.

#9 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:11 AM

Posted 22 December 2010 - 01:08 PM

I understand that this is a programming forum, but was wondering whether alternative techniques are allowed.

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.

Edited by JosiahK, 22 December 2010 - 01:09 PM.

Quod non mortiferum, fortiorem me facit.
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 Stephen W

Stephen W
  • Topic Starter

  • Members
  • 210 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Norwich, UK
  • Local time:11:11 AM

Posted 22 December 2010 - 01:39 PM

Hi Josiah. Thanks for your input. Any help would be great. I guess it would be most polite though for us to wait for Groovicus to post again as he may have done some work on it all ready. It sounds like he is a busy guy though so hopefully you can help me out with this.
Thanks

#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:11 AM

Posted 22 December 2010 - 03:39 PM

I actually have not had time to work on it. I have a personal project that uses up most of my time. I finally got past a roadblock last night, so I don't see having too much free time in the near future. This would be a great project for a student though.

#12 FayB

FayB

  • Members
  • 46 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:05:11 AM

Posted 22 December 2010 - 05:02 PM

I was just looking at this thread and became interested. I am a progammer but an "old time" programmer - don't know VB or much object oriented programming.
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

Edited by FayB, 22 December 2010 - 05:50 PM.


#13 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:11 AM

Posted 22 December 2010 - 05:27 PM

This is the script as I have it so far.
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 + FN


Edit: Can you please confirm that all of the HTML files that you need to process are stored in the same folder.

Edited by JosiahK, 22 December 2010 - 06:18 PM.

Quod non mortiferum, fortiorem me facit.
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 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:11 AM

Posted 22 December 2010 - 07:26 PM

OK, here is the best I can do in VBS right now. For this version of the code you specify the name of the FOLDER that you want to work with as the variable CFN on line 14.
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.

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.


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

Edited by JosiahK, 22 December 2010 - 07:28 PM.

Quod non mortiferum, fortiorem me facit.
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 CrimsonSpider

CrimsonSpider

  • Members
  • 91 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:The Matrix
  • Local time:10:11 AM

Posted 22 December 2010 - 08:23 PM

I write code for a living, so most of my time is taken up with that, and writing code for my own projects. I gave you links to examples that show you exactly what to do; that's really the most help I can offer.


Hey, i couldn't help but notice that you're such a pro and that you do this for a living! :inlove:

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
"Don’t worry if it doesn’t work right. If everything did, you’d be out of a job."
(Mosher’s Law of Software Engineering)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users