Computing.Net > Forums > Programming > VBS if any file exists

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VBS if any file exists

Reply to Message Icon

Name: Shootace
Date: August 9, 2009 at 15:20:14 Pacific
OS: Windows XP
Subcategory: General
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Shootace
Date: August 9, 2009 at 16:41:41 Pacific
Reply:

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

Thank you!


0

Response Number 2
Name: ghostdog
Date: August 9, 2009 at 18:45:20 Pacific
Reply:

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


0

Response Number 3
Name: Shootace
Date: August 10, 2009 at 12:22:43 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


copy Files based on date dec to binary conversion ...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VBS if any file exists

C++ can you check if a file exists? www.computing.net/answers/programming/c-can-you-check-if-a-file-exists/11411.html

Check for file existance on server www.computing.net/answers/programming/check-for-file-existance-on-server/16291.html

VBScript help with ? wildcard www.computing.net/answers/programming/vbscript-help-with-wildcard/14446.html