Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect a certain file extension

Status
Not open for further replies.

bowwow

IS-IT--Management
Jun 11, 2002
60
GB
Guys Im new to scripting a looking for a simple 'If' query to check if a file is of a particular file extension
e.g. Txt ... and then I'll continue with other actions.

Any help much appreciated!!
Cheers
 
Something like this ?
If UCase(Right(strPathName, 4)) = ".TXT" Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Try this one

Code:
Dim oFs: Set oFs = WScript.CreateObject("Scripting.FileSystemObject")
If oFs.GetExtensionName("C:\_work\test.xls") = "xls" Then
    ' do actions
End If
 
When attempting to plug in the

If UCase(Right(strPathName, 4)) = ".TXT" Then

Im getting an error saying that Ucase is not a Collection?
Any ideas?
 
What is your scripting language ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
This is the current VB script, you can see the number of IF clauses in the middle, Im looking to plug a further one in. The script goes on to create events within product called MOM. Any help much appreciated!:

'
' Set these values as required
'
Const FOLDER_NAME = "c:\testfolder"
Const DELETE_ZERO_BYTE_FILES = False ' *** True = delete the zero-byte files, False = report only
Const FILE_CREATE_DELAY_SECONDS = 30 ' *** Only check files older than this time


'
' Script constants
'
Const ARCHIVE_BIT = 32
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_INFO = 2

Dim fso


'
' Start of script
'
Set fso = CreateObject("Scripting.FileSystemObject")
Call CheckFiles
Set fso = Nothing

' End -----------------------------------------------------------------------------------


Sub CheckFiles()

Dim fsoFile
Dim NumberOfFiles : NumberOfFiles = 0
Dim FoundFiles : FoundFiles = ""

'
' Check that the folder we are going to examine actually exists, otherwise raise an
' alert and exit.
'
If fso.FolderExists(FOLDER_NAME) = False Then
Call CreateMomEvent(997, EVENT_TYPE_ERROR, "The specified folder for zero-byte file detection was not found.")
Exit Sub
End If


'
' Iterate through the collection of files in our folder
'
For Each fsoFile In fso.GetFolder(FOLDER_NAME).Files

'
' Files are created with the Archive bit set by default. Check if the Archive bit
' is set - if not then this we are not interested. As we process each zero-byte file
' we will clear the Archive bit so that it will not be processed again.
'
If ((fsoFile.Attributes And ARCHIVE_BIT) = ARCHIVE_BIT) Or (DELETE_ZERO_BYTE_FILES = True) Then

'
' We have a file with the archive bit set meaning that either it is a new file or
' was checked previously but was not zero-bytes in length.
'
' If the file was created less than FILE_CREATE_DELAY_SECONDS ago then we will ignore
' it as it may be zero length simply because the creating process hasn't written to it
' yet - e.g. file writes may be buffered.
'
If DateDiff("s", fsoFile.DateCreated, Now) > FILE_CREATE_DELAY_SECONDS Then

'
' The file was created greater than FILE_CREATE_DELAY_SECONDS ago so check
' if its size is zero bytes in length.
'
If fsoFile.Size = 0 Then

FoundFiles = FoundFiles & fsoFile.Name & ", "

'
' The file is zero bytes in length - increment our counter and reset the
' file's Archive bit so that we'll know in future its been processed.
'
NumberOfFiles = NumberOfFiles + 1
fsoFile.Attributes = fsoFile.Attributes Xor ARCHIVE_BIT

If DELETE_ZERO_BYTE_FILES = True Then

On Error Resume Next
Call fsoFile.Delete(False)

If Err.number <> 0 Then
Call CreateMomEvent(996, EVENT_TYPE_ERROR, "Failed to delete zero-byte file '" & fsoFile.Name & "'. " & Err.Description)
End If

On Error GoTo 0

End If

End If

End If

End If

Next
 
And the following is not working ?
If UCase(Right(fsoFile.Name, 4)) = ".TXT" Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top