topsy99's Avatar
Go4Expert Member
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
topsy99's Avatar
Go4Expert Member
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.
topsy99's Avatar
Go4Expert Member
e.g. files named 0310fle.txt can i make the program run all programs 0310*.txt
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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"

topsy99's Avatar
Go4Expert Member
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.?
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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
topsy99's Avatar
Go4Expert Member
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.
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
My pleasure
topsy99's Avatar
Go4Expert Member
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.
topsy99's Avatar
Go4Expert Member
also how can i reward you?