VBS if any file exists

August 9, 2009 at 15:20:14
Specs: Windows XP
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!

See More: VBS if any file exists

Report •

August 9, 2009 at 16:41:41
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 location

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "\d"
objRegEx.IgnoreCase = True
'objRegEx.Global = False

Sub FindFiles(ByRef list, fso, path, basePathLen)
If basePathLen = 0 Then
basePathLen = Len("C:\EFG") + 1
If Right(path, 1) <> "\" Then _
basePathLen = basePathLen + 1
End If

For 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"

'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
End Sub

Thank you!

Report •

August 9, 2009 at 18:45:20
for checking whether directory has files, you can use count()

Set objFS=CreateObject("Scripting.FileSystemObject")
strFolder ="c:\test"
WScript.Echo objFS.GetFolder(strFolder).Files.Count

to check whether directory contains other subfolders

WScript.Echo objFS.GetFolder(strFolder).SubFolders.Count

GNU win32 packages | Gawk

Report •

August 10, 2009 at 12:22:43
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

Report •

Related Solutions

Ask Question