Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I'm trying to write a VBScript that if any file exists in a particular folder it exits and does nothing. If no files exist then it creates a file and writes some text inside of it. I have the creating a file and writing text part working. But having trouble creating the part that looks for any file type. I think I have to use a Regular Expression for a pattern, but not sure if this is the right way to go. Any help would be greatly appreciated.
Thank you!

To clarify my original post all files are text files and I want it to do nothing if any file exists in said folder. If no file exists in said folder I want the code to create a file and write a line of text in it and place it in another folder...
This is what I have thus far:
Dim objfso, rootfolder, objRegEx, filetxt' Create FileSystemObject
Set objfso = WScript.Createobject("Scripting.FileSystemObject")'Get the Starting Folder
Set rootfolder= objfso.GetFolder("C:\EFG") 'Change the file locationSet objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "\d"
objRegEx.IgnoreCase = True
'objRegEx.Global = FalseSub FindFiles(ByRef list, fso, path, basePathLen)
If basePathLen = 0 Then
basePathLen = Len("C:\EFG") + 1
If Right(path, 1) <> "\" Then _
basePathLen = basePathLen + 1
End IfFor Each f In fso.GetFolder("C:\EFG").Files
list.Add Mid(f.Path, basePathLen), f.DateCreated
Next 'f'Loop through all the folders
For Each f In objfso.GetFolder("C:\EFG").Files
if objRegEx.Test(f) then
WScript.Echo "Code would find file"
'else'path = filesys.GetAbsolutePathName("c:\EFG")
'getname = filesys.GetFileName(path)
'Set filetxt = filesys.CreateTextFile("c:\EFG\done\ZZZZZZZZ.txt", True)
'filetxt.WriteLine("This is an end of job identifier.")
end if
Next
End SubThank you!

for checking whether directory has files, you can use count()
Set objFS=CreateObject("Scripting.FileSystemObject") strFolder ="c:\test" WScript.Echo objFS.GetFolder(strFolder).Files.Countto check whether directory contains other subfolders
WScript.Echo objFS.GetFolder(strFolder).SubFolders.Count

Thank you Ghostdog!
This is what I wound up using...I didn't want a popup for the actual filecount, that's the only change I made....Thanks again!
Dim objfso, filetxt, Getfilecount
Set objFSo=CreateObject("Scripting.FileSystemObject")
strFolder ="c:\efg"
Getfilecount = objfso.GetFolder(strFolder).Files.Count
if objfso.getfolder(strfolder).files.Count = 0 Then
Set filetxt = objfso.CreateTextFile("c:\EFG\done\ZZZZZZZZ.txt", True)
filetxt.WriteLine("This is an end of job identifier.")
end if

![]() |
copy Files based on date
|
dec to binary conversion ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |