Solved

Automation for Converting Filename to Lowercase and Creation of htaccess redirects

Posted on 2012-04-11
16
290 Views
Last Modified: 2012-08-14
Hi,

We are currently converting our FAQ pages (database driven) to static pages. What we did is used a software that gathers the pages from the live site and produce the HTML version of the pages. Since the filename of the pages were created using the title like what wordpress does (in title case), the html filename are now in title case.

e.g. /faq/question/1/This-Is-A-Sample-Page.html

So there are 3 stages that we will be doing in the pages.

STAGE 1 [AUTOMATED]: Convert the filenames from title case to lower case letters e.g. from /faq/question/1/This-Is-A-Sample-Page.html to /faq/question/1/this-is-a-sample-page.html

STAGE 2 [MANUAL]: Refile the pages manually to category folders
e.g.
/faq/category1/this-is-a-sample-page.html
/faq/category2/this-is-a-sample-page-too.html
/faq/category3/this-is-a-sample-page-for-category-3.html

STAGE 3 [AUTOMATED]: Create htaccess redirects in batch recursively for all pages ready to add in the htaccess file

e.g. RedirectMatch permanent ^/faq/question/1/This-Is-A-Sample-Page.html$ http://www.my-site.com/faq/category1/this-is-a-sample-page.html

We want to automate stage 1 and 3. Here is our idea of what the software tools or scripts should do.

STAGE 1 AUTOMATION: We can just run the software or script and it will go through all pages recursively through all folders and subfolders under the /faq/ folder and convert all filenames to lower case

STAGE 3 AUTOMATION: We will be keeping the original copy of the /faq/ folder with the title case filenames. What the script or software will do is look at the original copy of the faq/ recursively through all folder and subfolders and then find the equivalent page that is in the refiled faq/ folder and the software or script will go through all pages and then when done it will create the list of redirects that can just be copied to an htaccess file.

e.g.

RedirectMatch permanent ^/faq/question/1/This-Is-A-Sample-Page.html$ http://www.my-site.com/faq/category1/this-is-a-sample-page.html
RedirectMatch permanent ^/faq/question/3/This-Is-A-Sample-Page-too.html$ http://www.my-site.com/faq/category2/this-is-a-sample-page-too.html
RedirectMatch permanent ^/faq/question/8/This-Is-A-Sample-Page-For-Category-3.html$ http://www.my-site.com/faq/category3/his-is-a-sample-page-for-category-3.html

Anyone who has any idea on software tools or scripts that can do the automations above or who knows how to create the scripts that can automate what we want to do with the renaming and creation of redirects for our pages?

Thanks,
Openaccount
0
Comment
Question by:openaccount1
  • 8
  • 8
16 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37835174
Hi, I decided to give it a go. I don't expect this to be exactly what you want right from the start but it should be possible to get it to do what you want.

First, the script:

Option Explicit

Const C_WEBSITE = "http://www.my-site.com/"

Const C_FILE_TMP = "log_questions.txt"
Const C_FILE_ADD = "add2htaccess.txt"

Const C_FAQ = "faq"
Const C_CATEGORY = "category"

Dim oFSO, iStage, sStage, sSuccessMessage, oTmpFile, oFolderFAQ, sRootPath, oFolderQuestion, oSubfolderQuestion, oQuestionFile, oFolderCategory, oColFiles, sOrigFile, oAddFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolderFAQ = oFSO.GetFolder(C_FAQ)
sRootPath = oFolderFAQ.ParentFolder.Path
'WScript.Echo "sRootPath = " & sRootPath

iStage = 1 : sStage = "rename files to lowercase and remember original file paths" : sSuccessMessage = "now move all question html files to correct category folder"
If oFSO.FileExists(C_FILE_TMP) Then iStage = 2 : sStage = "move files" : sSuccessMessage = "now run again for last step" : If CheckQuestionsGone() Then iStage = 3 : sStage = "create htaccess list" : sSuccessMessage = "done!"

WScript.Echo "STAGE " & iStage & ": " & sStage

Select Case iStage

	Case 1:

		Set oTmpFile = oFSO.OpenTextFile(C_FILE_TMP, 2, True) ' create temp output with all the original files
		Set oFolderQuestion = oFolderFAQ.Subfolders("question")
		For Each oSubfolderQuestion In oFolderQuestion.Subfolders
			'WScript.Echo oSubfolderQuestion.Name
			For Each oQuestionFile In oSubfolderQuestion.Files
				'WScript.Echo "   " & oQuestionFile.Name
				oTmpFile.WriteLine Replace(Mid(oFSO.BuildPath(oSubfolderQuestion.Path, oQuestionFile.Name), Len(sRootPath) + 1), "\", "/")
				If LCase(oQuestionFile.Name) <> oQuestionFile.Name Then oQuestionFile.Move oFSO.BuildPath(oSubfolderQuestion.Path, LCase(oQuestionFile.Name))
			Next
		Next
		Set oFolderQuestion = Nothing
		oTmpFile.Close
		Set oTmpFile = Nothing

	Case 2:

		Dim iAutoMoveCounter ' just for testing, move the 2 test files to category1, 2, 3
		iAutoMoveCounter = 1
		Set oFolderQuestion = oFolderFAQ.Subfolders("question")
		For Each oSubfolderQuestion In oFolderQuestion.Subfolders
			For Each oQuestionFile In oSubfolderQuestion.Files
				If Not oFSO.FolderExists(oFSO.BuildPath(oFolderFAQ.Path, C_CATEGORY & iAutoMoveCounter)) Then oFolderFAQ.Subfolders.Add C_CATEGORY & iAutoMoveCounter
				Set oFolderCategory = oFolderFAQ.Subfolders(C_CATEGORY & iAutoMoveCounter)
				oQuestionFile.Move oFSO.BuildPath(oFolderCategory.Path, oQuestionFile.Name)
				Set oFolderCategory = Nothing
				iAutoMoveCounter = iAutoMoveCounter + 1
			Next
		Next
		Set oFolderQuestion = Nothing

	Case 3:

		Set oTmpFile = oFSO.OpenTextFile(C_FILE_TMP, 1, False)
		Set oColFiles = CreateObject("Scripting.Dictionary")
		While Not oTmpFile.AtEndOfStream
			sOrigFile = oTmpFile.ReadLine()
			oColFiles(LCase(Mid(sOrigFile, InstrRev(sOrigFile, "/") + 1))) = sOrigFile ' when we find the filename under a category folder, we will know what the original file was
		Wend
		oTmpFile.Close
		Set oTmpFile = Nothing
		Set oAddFile = oFSO.OpenTextFile(C_FILE_ADD, 2, True)
		For Each oFolderCategory In oFolderFAQ.Subfolders
			If Left(oFolderCategory.Name, Len(C_CATEGORY)) = C_CATEGORY Then
				'WScript.Echo oFolderCategory.Name
				For Each oQuestionFile In oFolderCategory.Files
					'WScript.Echo "   " & oQuestionFile.Name
					oAddFile.WriteLine "RedirectMatch permanent ^" & oColFiles(oQuestionFile.Name) & "$ " & C_WEBSITE & C_FAQ & "/" & oFolderCategory.Name & "/" & oQuestionFile.Name
				Next
			End If
		Next
		oAddFile.Close
		Set oAddFile = Nothing

End Select

WScript.Echo "STAGE " & iStage & " successful; " & sSuccessMessage

Set oFolderFAQ = Nothing
Set oFSO = Nothing

Function CheckQuestionsGone()

	Dim iFilesFound
	iFilesFound = 0

	Set oFolderQuestion = oFolderFAQ.Subfolders("question")
	For Each oSubfolderQuestion In oFolderQuestion.Subfolders
		iFilesFound = iFilesFound + oSubfolderQuestion.Files.Count
	Next
	Set oFolderQuestion = Nothing

	CheckQuestionsGone = (iFilesFound = 0)

End Function

Open in new window


I implemented stage 2 only for my convenience, in your case it should just check that there's no files left under the 'question' folder and then go to stage 3.

Here's a 'session log' to show the usage:

D:\Temp\ee\Q_27670513>dir /b/s
D:\Temp\ee\Q_27670513\faq
D:\Temp\ee\Q_27670513\t.vbs
D:\Temp\ee\Q_27670513\faq\question
D:\Temp\ee\Q_27670513\faq\question\1
D:\Temp\ee\Q_27670513\faq\question\3
D:\Temp\ee\Q_27670513\faq\question\8
D:\Temp\ee\Q_27670513\faq\question\1\This-Is-A-Sample-Page.html
D:\Temp\ee\Q_27670513\faq\question\3\This-Is-A-Sample-Page-Too.html
D:\Temp\ee\Q_27670513\faq\question\8\This-Is-A-Sample-Page-For-Category-3.html

D:\Temp\ee\Q_27670513>cscript //nologo t.vbs
STAGE 1: rename files to lowercase and remember original file paths
STAGE 1 successful; now move all question html files to correct category folder

D:\Temp\ee\Q_27670513>type log_questions.txt
/faq/question/1/This-Is-A-Sample-Page.html
/faq/question/3/This-Is-A-Sample-Page-Too.html
/faq/question/8/This-Is-A-Sample-Page-For-Category-3.html

D:\Temp\ee\Q_27670513>cscript //nologo t.vbs
STAGE 2: move files
STAGE 2 successful; now run again for last step

D:\Temp\ee\Q_27670513>dir /b/s
D:\Temp\ee\Q_27670513\faq
D:\Temp\ee\Q_27670513\t.vbs
D:\Temp\ee\Q_27670513\log_questions.txt
D:\Temp\ee\Q_27670513\faq\question
D:\Temp\ee\Q_27670513\faq\category1
D:\Temp\ee\Q_27670513\faq\category2
D:\Temp\ee\Q_27670513\faq\category3
D:\Temp\ee\Q_27670513\faq\question\1
D:\Temp\ee\Q_27670513\faq\question\3
D:\Temp\ee\Q_27670513\faq\question\8
D:\Temp\ee\Q_27670513\faq\category1\this-is-a-sample-page.html
D:\Temp\ee\Q_27670513\faq\category2\this-is-a-sample-page-too.html
D:\Temp\ee\Q_27670513\faq\category3\this-is-a-sample-page-for-category-3.html

D:\Temp\ee\Q_27670513>cscript //nologo t.vbs
STAGE 3: create htaccess list
STAGE 3 successful; done!

D:\Temp\ee\Q_27670513>type add2htaccess.txt
RedirectMatch permanent ^/faq/question/1/This-Is-A-Sample-Page.html$ http://www.my-site.com/faq/category1/this-is-a-sample-page.html
RedirectMatch permanent ^/faq/question/3/This-Is-A-Sample-Page-Too.html$ http://www.my-site.com/faq/category2/this-is-a-sample-page-too.html
RedirectMatch permanent ^/faq/question/8/This-Is-A-Sample-Page-For-Category-3.html$ http://www.my-site.com/faq/category3/this-is-a-sample-page-for-category-3.html

Open in new window


Can you have a look to see if this works for you?
0
 

Author Comment

by:openaccount1
ID: 37835679
Hi Robert,

For stage 2 - moving of pages to correct category, that will be done manually by us. It will not be done by the script. Because in the script that you gave, it was moving the faq questions automatically. What the script should do is just look at the old path and the new path and create the redirect. It should not move the pages. As we will be doing the refiling of pages manually. what I gave is just sample categories but there will be actual categories e.g. /faq/basketball/, /faq/volleyball, /faq/football/ etc... and not just /faq/category1/, /faq/category2/ etc...

Were almost there :)

Thanks,
openaccount
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37836941
Ok, makes sense, what I do now is show an error when there's files left under the 'question' directory, assuming there won't be a new 'question' category. Otherwise, stage 3 is performed. If you want to test partially before the moves are all done, you can take that check out, one way would be to have the function CheckQuestionsGone return True.

Option Explicit

Const C_WEBSITE = "http://www.my-site.com/"

Const C_FILE_TMP = "log_questions.txt"
Const C_FILE_ADD = "add2htaccess.txt"

Const C_FAQ = "faq"
Const C_QUESTION = "question"

Dim oFSO, iStage, sStage, sSuccessMessage, sErrorMessage, oTmpFile, oFolderFAQ, sRootPath, oFolderQuestion, oSubfolderQuestion, oQuestionFile, oFolderCategory, oColFiles, sOrigFile, oAddFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolderFAQ = oFSO.GetFolder(C_FAQ)
sRootPath = oFolderFAQ.ParentFolder.Path
'WScript.Echo "sRootPath = " & sRootPath

iStage = 1 : sStage = "rename files to lowercase and remember original file paths" : sSuccessMessage = "now move all question html files to correct category folder"
If oFSO.FileExists(C_FILE_TMP) Then iStage = 2 : sStage = "move files" : sSuccessMessage = "now run again for last step" : If CheckQuestionsGone() Then iStage = 3 : sStage = "create htaccess list" : sSuccessMessage = "done!"
sErrorMessage = ""

WScript.Echo "STAGE " & iStage & ": " & sStage

Select Case iStage

	Case 1:

		Set oTmpFile = oFSO.OpenTextFile(C_FILE_TMP, 2, True) ' create temp output with all the original files
		Set oFolderQuestion = oFolderFAQ.Subfolders(C_QUESTION)
		For Each oSubfolderQuestion In oFolderQuestion.Subfolders
			'WScript.Echo oSubfolderQuestion.Name
			For Each oQuestionFile In oSubfolderQuestion.Files
				'WScript.Echo "   " & oQuestionFile.Name
				oTmpFile.WriteLine Replace(Mid(oFSO.BuildPath(oSubfolderQuestion.Path, oQuestionFile.Name), Len(sRootPath) + 1), "\", "/")
				If LCase(oQuestionFile.Name) <> oQuestionFile.Name Then oQuestionFile.Move oFSO.BuildPath(oSubfolderQuestion.Path, LCase(oQuestionFile.Name))
			Next
		Next
		Set oFolderQuestion = Nothing
		oTmpFile.Close
		Set oTmpFile = Nothing

	Case 2:

		sErrorMessage = "Not all files have been moved yet!"

	Case 3:

		Set oTmpFile = oFSO.OpenTextFile(C_FILE_TMP, 1, False)
		Set oColFiles = CreateObject("Scripting.Dictionary")
		While Not oTmpFile.AtEndOfStream
			sOrigFile = oTmpFile.ReadLine()
			oColFiles(LCase(Mid(sOrigFile, InstrRev(sOrigFile, "/") + 1))) = sOrigFile ' when we find the filename under a category folder, we will know what the original file was
		Wend
		oTmpFile.Close
		Set oTmpFile = Nothing
		Set oAddFile = oFSO.OpenTextFile(C_FILE_ADD, 2, True)
		For Each oFolderCategory In oFolderFAQ.Subfolders
			If oFolderCategory.Name <> C_QUESTION Then
				'WScript.Echo oFolderCategory.Name
				For Each oQuestionFile In oFolderCategory.Files
					'WScript.Echo "   " & oQuestionFile.Name
					oAddFile.WriteLine "RedirectMatch permanent ^" & oColFiles(oQuestionFile.Name) & "$ " & C_WEBSITE & C_FAQ & "/" & oFolderCategory.Name & "/" & oQuestionFile.Name
				Next
			End If
		Next
		oAddFile.Close
		Set oAddFile = Nothing

End Select

If sErrorMessage = "" Then
	WScript.Echo "STAGE " & iStage & " successful; " & sSuccessMessage
Else
	WScript.Echo "STAGE " & iStage & " unsuccessful; " & sErrorMessage
End If

Set oFolderFAQ = Nothing
Set oFSO = Nothing

Function CheckQuestionsGone()

	Dim iFilesFound
	iFilesFound = 0

	Set oFolderQuestion = oFolderFAQ.Subfolders(C_QUESTION)
	For Each oSubfolderQuestion In oFolderQuestion.Subfolders
		iFilesFound = iFilesFound + oSubfolderQuestion.Files.Count
	Next
	Set oFolderQuestion = Nothing

	CheckQuestionsGone = (iFilesFound = 0)
	'CheckQuestionsGone = True

End Function

Open in new window

0
 

Author Comment

by:openaccount1
ID: 37849638
Hi Robert,

Been away for 3 days so I was not able to respond to you immediately. I tested the script and in stage 3, there is nothing added in the add2htaccess.txt file. Can you please check what the problem is.

Thanks,
Openaccount
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37850006
Well, there's a number of possibilities. I guess you're getting an empty file so it's not write permissions. It could be the files are not matching for some reason. Can you post the first line from 'log_questions.txt' or check the structure of it against mine? For that take a look at line 17 of the 'session log' I posted earlier.

Then, there is some logging built into the script as posted before (but commented out):

'WScript.Echo

Open in new window


You can activate that logging by removing the single quote from the beginning of those lines. There's 2 of them under the "Case 3:" block. Can you post the output? Also, if you happen to have saved earlier output that'd be great. (But it would probably only be 2 lines...)

You can also add more logging to the script, for example, before line 54 (Wend):

WScript.Echo "file: '" & LCase(Mid(sOrigFile, InstrRev(sOrigFile, "/") + 1)) & "', original url: '" & sOrigFile & "'"

Open in new window


If all that doesn't output anything I could try writing a little test script to see what's different in your environment.

BTW: Have you been working in a (partly) copied directory or is this the real deal?
0
 

Author Comment

by:openaccount1
ID: 37854559
Hi Robert,

I am working on partly copied directory. Here is the log for the latest test. The htaccess text file is still blank.

E:\Temp>dir /b/s
E:\Temp\a.vbs
E:\Temp\faq
E:\Temp\faq\question
E:\Temp\faq\question\11
E:\Temp\faq\question\12
E:\Temp\faq\question\13
E:\Temp\faq\question\14
E:\Temp\faq\question\15
E:\Temp\faq\question\16
E:\Temp\faq\question\17
E:\Temp\faq\question\18
E:\Temp\faq\question\21
E:\Temp\faq\question\3
E:\Temp\faq\question\4
E:\Temp\faq\question\5
E:\Temp\faq\question\6
E:\Temp\faq\question\7
E:\Temp\faq\question\8
E:\Temp\faq\question\9
E:\Temp\faq\question\11\Sample-Question-11.html
E:\Temp\faq\question\12\Sample-Question-12.html
E:\Temp\faq\question\13\Sample-Question-13.html
E:\Temp\faq\question\14\Sample-Question-14.html
E:\Temp\faq\question\15\Sample-Question-15.html
E:\Temp\faq\question\16\Sample-Question-16.html
E:\Temp\faq\question\17\Sample-Question-17.html
E:\Temp\faq\question\18\Sample-Question-18.html
E:\Temp\faq\question\21\Sample-Question-21.html
E:\Temp\faq\question\3\Sample-Question-3.html
E:\Temp\faq\question\4\Sample-Question-4.html
E:\Temp\faq\question\5\Sample-Question-5.html
E:\Temp\faq\question\6\Sample-Question-6.html
E:\Temp\faq\question\7\Sample-Question-7.html
E:\Temp\faq\question\8\Sample-Question-8.html
E:\Temp\faq\question\9\Sample-Question-9.html

E:\Temp>cscript //nologo a.vbs
STAGE 1: rename files to lowercase and remember original file paths
STAGE 1 successful; now move all question html files to correct category folder

E:\Temp>type log_questions.txt
/faq/question/11/Sample-Question-11.html
/faq/question/12/Sample-Question-12.html
/faq/question/13/Sample-Question-13.html
/faq/question/14/Sample-Question-14.html
/faq/question/15/Sample-Question-15.html
/faq/question/16/Sample-Question-16.html
/faq/question/17/Sample-Question-17.html
/faq/question/18/Sample-Question-18.html
/faq/question/21/Sample-Question-21.html
/faq/question/3/Sample-Question-3.html
/faq/question/4/Sample-Question-4.html
/faq/question/5/Sample-Question-5.html
/faq/question/6/Sample-Question-6.html
/faq/question/7/Sample-Question-7.html
/faq/question/8/Sample-Question-8.html
/faq/question/9/Sample-Question-9.html

E:\Temp>cscript //nologo a.vbs
STAGE 3: create htaccess list
file: 'sample-question-11.html', original url: '/faq/question/11/Sample-Question
-11.html'
file: 'sample-question-12.html', original url: '/faq/question/12/Sample-Question
-12.html'
file: 'sample-question-13.html', original url: '/faq/question/13/Sample-Question
-13.html'
file: 'sample-question-14.html', original url: '/faq/question/14/Sample-Question
-14.html'
file: 'sample-question-15.html', original url: '/faq/question/15/Sample-Question
-15.html'
file: 'sample-question-16.html', original url: '/faq/question/16/Sample-Question
-16.html'
file: 'sample-question-17.html', original url: '/faq/question/17/Sample-Question
-17.html'
file: 'sample-question-18.html', original url: '/faq/question/18/Sample-Question
-18.html'
file: 'sample-question-21.html', original url: '/faq/question/21/Sample-Question
-21.html'
file: 'sample-question-3.html', original url: '/faq/question/3/Sample-Question-3
.html'
file: 'sample-question-4.html', original url: '/faq/question/4/Sample-Question-4
.html'
file: 'sample-question-5.html', original url: '/faq/question/5/Sample-Question-5
.html'
file: 'sample-question-6.html', original url: '/faq/question/6/Sample-Question-6
.html'
file: 'sample-question-7.html', original url: '/faq/question/7/Sample-Question-7
.html'
file: 'sample-question-8.html', original url: '/faq/question/8/Sample-Question-8
.html'
file: 'sample-question-9.html', original url: '/faq/question/9/Sample-Question-9
.html'
STAGE 3 successful; done!
E:\Temp>

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37854722
Nice! The only thing that's missing is a "dir /b/s" at the end. Now it's not obvious for me where you've moved the files, maybe the structure is different than I thought (subfolders or not under the faq folder?). The other parts seem to work as they should but it's just not finding any folders/files other than the question folder (they're not under there are they?)
0
 

Author Comment

by:openaccount1
ID: 37854737
Here is the structure under the FAQ folder.

E:\Temp\faq>dir /b/s
E:\Temp\faq\category
E:\Temp\faq\question
E:\Temp\faq\category\cat1
E:\Temp\faq\category\cat2
E:\Temp\faq\category\cat3
E:\Temp\faq\category\cat4
E:\Temp\faq\category\cat1\sample-question-13.html
E:\Temp\faq\category\cat1\sample-question-14.html
E:\Temp\faq\category\cat2\sample-question-15.html
E:\Temp\faq\category\cat2\sample-question-4.html
E:\Temp\faq\category\cat2\sample-question-9.html
E:\Temp\faq\category\cat3\sample-question-16.html
E:\Temp\faq\category\cat3\sample-question-17.html
E:\Temp\faq\category\cat3\sample-question-18.html
E:\Temp\faq\category\cat3\sample-question-21.html
E:\Temp\faq\category\cat4\sample-question-11.html
E:\Temp\faq\category\cat4\sample-question-12.html
E:\Temp\faq\category\cat4\sample-question-3.html
E:\Temp\faq\category\cat4\sample-question-5.html
E:\Temp\faq\category\cat4\sample-question-6.html
E:\Temp\faq\category\cat4\sample-question-7.html
E:\Temp\faq\category\cat4\sample-question-8.html
E:\Temp\faq\question\11
E:\Temp\faq\question\12
E:\Temp\faq\question\13
E:\Temp\faq\question\14
E:\Temp\faq\question\15
E:\Temp\faq\question\16
E:\Temp\faq\question\17
E:\Temp\faq\question\18
E:\Temp\faq\question\21
E:\Temp\faq\question\3
E:\Temp\faq\question\4
E:\Temp\faq\question\5
E:\Temp\faq\question\6
E:\Temp\faq\question\7
E:\Temp\faq\question\8
E:\Temp\faq\question\9

E:\Temp\faq>

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37854781
Ah, that's the problem: The script only processed one level of subfolders. That may be enough, when it starts inside the 'category' folder.

Do you need that extra level in there, in the actual URL as well?
0
 

Author Comment

by:openaccount1
ID: 37854788
Yes, we need the extra level. Actually its better if the script does not have a sublevel limitation. e.g. it can find the pages even if they are filed like this.

faq\category\cat4\cat4sub1\cat4sub1-1\cat4sub1-1-1\sample-question-8.html

Maybe around 5 sublevels under the category folder if we can't make it at unlimited sublevels.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37854823
'Unlimited' is no problem (only memory limits which shouldn't be a problem here), that's what recursion is for. The new code:

Option Explicit

Const C_WEBSITE = "http://www.my-site.com/"

Const C_FILE_TMP = "log_questions.txt"
Const C_FILE_ADD = "add2htaccess.txt"

Const C_FAQ = "faq"
Const C_QUESTION = "question"
Const C_CATEGORY = "category"

Dim oFSO, iStage, sStage, sSuccessMessage, sErrorMessage, oTmpFile, oFolderFAQ, sRootPath, oFolderQuestion, oSubfolderQuestion, oQuestionFile, oColFiles, sOrigFile, oAddFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolderFAQ = oFSO.GetFolder(C_FAQ)
sRootPath = oFolderFAQ.ParentFolder.Path
'WScript.Echo "sRootPath = " & sRootPath

iStage = 1 : sStage = "rename files to lowercase and remember original file paths" : sSuccessMessage = "now move all question html files to correct category folder"
If oFSO.FileExists(C_FILE_TMP) Then iStage = 2 : sStage = "move files" : sSuccessMessage = "now run again for last step" : If CheckQuestionsGone() Then iStage = 3 : sStage = "create htaccess list" : sSuccessMessage = "done!"
sErrorMessage = ""

WScript.Echo "STAGE " & iStage & ": " & sStage

Select Case iStage

	Case 1:

		Set oTmpFile = oFSO.OpenTextFile(C_FILE_TMP, 2, True) ' create temp output with all the original files
		Set oFolderQuestion = oFolderFAQ.Subfolders(C_QUESTION)
		For Each oSubfolderQuestion In oFolderQuestion.Subfolders
			'WScript.Echo oSubfolderQuestion.Name
			For Each oQuestionFile In oSubfolderQuestion.Files
				'WScript.Echo "   " & oQuestionFile.Name
				oTmpFile.WriteLine Replace(Mid(oFSO.BuildPath(oSubfolderQuestion.Path, oQuestionFile.Name), Len(sRootPath) + 1), "\", "/")
				If LCase(oQuestionFile.Name) <> oQuestionFile.Name Then oQuestionFile.Move oFSO.BuildPath(oSubfolderQuestion.Path, LCase(oQuestionFile.Name))
			Next
		Next
		Set oFolderQuestion = Nothing
		oTmpFile.Close
		Set oTmpFile = Nothing

	Case 2:

		sErrorMessage = "Not all files have been moved yet!"

	Case 3:

		Set oTmpFile = oFSO.OpenTextFile(C_FILE_TMP, 1, False)
		Set oColFiles = CreateObject("Scripting.Dictionary")
		While Not oTmpFile.AtEndOfStream
			sOrigFile = oTmpFile.ReadLine()
			oColFiles(LCase(Mid(sOrigFile, InstrRev(sOrigFile, "/") + 1))) = sOrigFile ' when we find the filename under a category folder, we will know what the original file was
		Wend
		oTmpFile.Close
		Set oTmpFile = Nothing
		Set oAddFile = oFSO.OpenTextFile(C_FILE_ADD, 2, True)
		ProcessSubfolders oFolderFAQ.Subfolders(C_CATEGORY), C_WEBSITE & C_FAQ
		oAddFile.Close
		Set oAddFile = Nothing

End Select

If sErrorMessage = "" Then
	WScript.Echo "STAGE " & iStage & " successful; " & sSuccessMessage
Else
	WScript.Echo "STAGE " & iStage & " unsuccessful; " & sErrorMessage
End If

Set oFolderFAQ = Nothing
Set oFSO = Nothing

Function CheckQuestionsGone()

	Dim iFilesFound
	iFilesFound = 0

	Set oFolderQuestion = oFolderFAQ.Subfolders(C_QUESTION)
	For Each oSubfolderQuestion In oFolderQuestion.Subfolders
		iFilesFound = iFilesFound + oSubfolderQuestion.Files.Count
	Next
	Set oFolderQuestion = Nothing

	CheckQuestionsGone = (iFilesFound = 0)

End Function

Sub ProcessSubfolders(ByVal oSubfoldercategory, ByVal sCurrentPath)
	sCurrentPath = sCurrentPath & "/" & oSubfoldercategory.Name
	WScript.Echo "* CurrentPath = '" & sCurrentPath & "'"
	Dim oSubSubfolderCategory
	For Each oSubSubfolderCategory In oSubfoldercategory.Subfolders
		WScript.Echo oSubSubfolderCategory.Name
		ProcessSubfolders oSubSubfolderCategory, sCurrentPath
	Next
	For Each oQuestionFile In oSubfoldercategory.Files
		WScript.Echo "   " & oQuestionFile.Name
		oAddFile.WriteLine "RedirectMatch permanent ^" & oColFiles(oQuestionFile.Name) & "$ " & sCurrentPath & "/" & oQuestionFile.Name
	Next
End Sub

Open in new window

0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 37854843
Actually it might make more sense to change the order in that last Sub. Also added a check on the file so you would know if a file has accidentally been renamed or something.

Sub ProcessSubfolders(ByVal oSubfoldercategory, ByVal sCurrentPath)
	sCurrentPath = sCurrentPath & "/" & oSubfoldercategory.Name
	WScript.Echo "* CurrentPath = '" & sCurrentPath & "'"
	For Each oQuestionFile In oSubfoldercategory.Files
		WScript.Echo "   " & oQuestionFile.Name
		If oColFiles.Exists(oQuestionFile.Name) Then
			oAddFile.WriteLine "RedirectMatch permanent ^" & oColFiles(oQuestionFile.Name) & "$ " & sCurrentPath & "/" & oQuestionFile.Name
		Else
			WScript.Echo "- file not in original faq: " & oQuestionFile.Name
		End If
	Next
	Dim oSubSubfolderCategory
	For Each oSubSubfolderCategory In oSubfoldercategory.Subfolders
		WScript.Echo oSubSubfolderCategory.Name
		ProcessSubfolders oSubSubfolderCategory, sCurrentPath
	Next
End Sub

Open in new window

0
 

Author Comment

by:openaccount1
ID: 37855014
Hi Robert,
The script is working correctly now. One last question. How can I remove the pop-ups if the third stage is run by just clicking the vbs file instead of using MS Dos?
0
 

Author Comment

by:openaccount1
ID: 37855018
ok found it.
0
 

Author Closing Comment

by:openaccount1
ID: 37855028
Thanks for the prompt response. Code is easy to understand and you delivered what we want.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37855079
Great! Glad to help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now