Go4Expert Member
5Oct2009,10:40   #11
topsy99's Avatar
Dim objFSO
Dim MyFile
Dim MyFolder
Dim objShell

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = objFSO.GetFolder("C:\Test")
Set objShell = WScript.CreateObject("WScript.Shell")

For Each MyFile In MyFolder.Files
If Right(MyFile.Path,4) = ".txt" Then
objShell.Run("notepad " + MyFile.Path)
End If
Next
Go4Expert Member
5Oct2009,13:08   #12
topsy99's Avatar
got it working. i changed myfolder rather than c:\test and it failed now changed c:\test and it works.

question. i cant program vb or most things. can i make this program date selective.
when running it to open all text files in folder it locks the computer as there are over a 100 files. can i be date specific.
Go4Expert Member
5Oct2009,13:09   #13
topsy99's Avatar
e.g. files named 0310fle.txt can i make the program run all programs 0310*.txt
~ Б0ЯИ Τ0 С0δЭ ~
5Oct2009,19:32   #14
SaswatPadhi's Avatar
Of course you can.

Code: VB
Dim objFSO
Dim MyFile
Dim MyFolder
Dim objShell

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = objFSO.GetFolder("C:\Test")
Set objShell = WScript.CreateObject("WScript.Shell")

For Each MyFile In MyFolder.Files
    If Right(MyFile.Path,4) = ".txt" And Left(MyFile.Name,4) = "0310" Then
        objShell.Run("notepad " + MyFile.Path)
    End If
Next

Explanation :
In VB (and VBS), you use the Right, Left and Mid functions to extract sub-strings of specific lengths from specific positions in a string.
So, what I basically do is; I check the last 4 characters of the file-path with Right(MyFile.Path,4). If it matches with ".txt", I open the file.

Your question was to open 0310*.txt.
How I modified the code is : I kept the previous check for txt extension + I added one more condition, where I check if the first 4 characters of the file-name (NOT file-path) is 0310.

I hope this made the point clear. Here are some further examples :

CRITERIA .......... CONDITION
*.txt%%|%.......... Right(MyFile.Path,4) = ".txt"
0310*.txt |.......... Right(MyFile.Path,4) = ".txt" And Left(MyFile.Name,4)="0310"
*A?.txt |$$.......... Right(MyFile.Path,4) = ".txt" And Left(Right(MyFile.Path,6),2) = "A"

Go4Expert Member
6Oct2009,03:44   #15
topsy99's Avatar
works great. now one more question. can i input 0310 or any date from the desktop to the vbs file when i run it. will it stop and aske me for "0310" and how do i code it.?
~ Б0ЯИ Τ0 С0δЭ ~
6Oct2009,13:21   #16
SaswatPadhi's Avatar
Well, you can do it this way :

Code: VB
Dim objFSO
Dim MyFile
Dim MyFolder
Dim objShell
Dim Prefix

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = objFSO.GetFolder("D:\Test")
Set objShell = WScript.CreateObject("WScript.Shell")

Prefix = InputBox("(*) Enter prefix for file-names (e.g. for opening 0310*.txt files, enter 0310) :"+vbcrlf+vbcrlf+"(*) Leaving this blank or, clicking cancel will open *.txt files ...", "File-Name Prefix")
For Each MyFile In MyFolder.Files
    If Right(MyFile.Path,4) = ".txt" And Left(MyFile.Name,Len(Prefix)) = Prefix Then
        objShell.Run("notepad " + MyFile.Path)
    End If
Next

This works for prefix only. So, you can open files like 0310*.txt, 0612*.txt, ABC*.txt etc.. but not anything like *ABC.txt or *0310.txt.
Feel free to modify the code to suit your needs.
topsy99 like this
Go4Expert Member
6Oct2009,14:58   #17
topsy99's Avatar
you are so clever. that works perfectly. thank you. i wish i could program vbs.
I am very greatful for your assistance. it has simplified a problem that was working on through dos batch files but couldnt get them working.
~ Б0ЯИ Τ0 С0δЭ ~
7Oct2009,13:22   #18
SaswatPadhi's Avatar
My pleasure
Go4Expert Member
25Apr2010,14:48   #19
topsy99's Avatar
Ref the above vb program working perfectly. could it be adapted to search specific htm files. in same folder e.g. ran24041.htm

would be interested in your thoughts.
Go4Expert Member
25Apr2010,14:49   #20
topsy99's Avatar
also how can i reward you?