VBS to list file names NOT matching given extension - vbscript

I have been working on the following VBS. This will search a parent folder and all child / subfolders and print any file with the provided extension (doc, docx, msg, ppt, txt) and came up with the following. I am new to VBS but I would like to define the file extensions to IGNORE, listing all others. I have the directory listing of each file type working but I don't want to have to set a NEXT for each file type. I was able to use the <> code in the top section to show all files that don't match an extension using:
If objFSO.GetExtensionName(strFileName) <> "jpg" then
But this doesn't work in the lower part:
if LCase(InStr(1,Files, "jpg")) > 1 then Wscript.Echo Files
I would also like to be able to define multiple file types; like
If objFSO.GetExtensionName(strFileName) <> "jpg" OR "jpeg" OR "tiff"
Lastly I need to output to a text file, not a windows script msg box.
Can anyone help? Sorry for typos or confusion, English is not my first language.
Dim fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set FSO = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso.CreateTextFile("C:\temp\output.txt", 2)
objStartFolder = "C:\Test"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "x937" then
Wscript.Echo objFile.Name
End If
Next
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "docx" then
Wscript.Echo objFile.Name
End If
Next
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "doc" then
Wscript.Echo objFile.Name
End If
Next
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "txt" then
Wscript.Echo objFile.Name
End If
Next
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "msg" then
Wscript.Echo objFile.Name
End If
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
for each Files in colFiles
if LCase(InStr(1,Files, "msg")) > 1 then Wscript.Echo Files
next
ShowSubFolders Subfolder
Next
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
for each Files in colFiles
if LCase(InStr(1,Files, "txt")) > 1 then Wscript.Echo Files
next
ShowSubFolders Subfolder
Next
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
for each Files in colFiles
if LCase(InStr(1,Files, "ppt")) > 1 then Wscript.Echo Files
next
ShowSubFolders Subfolder
Next
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
for each Files in colFiles
if LCase(InStr(1,Files, "xls")) > 1 then Wscript.Echo Files
next
ShowSubFolders Subfolder
Next
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
for each Files in colFiles
if LCase(InStr(1,Files, "doc")) > 1 then Wscript.Echo Files
next
ShowSubFolders Subfolder
Next
End Sub

You can check all of the extensions in one loop:
For Each objFile in colFiles
Dim strFileName : strFileName = objFile.Name
Dim strExtension : strExtension = LCase(objFSO.GetExtensionName(strFileName))
If strExtension <> "txt" And _
strExtension <> "jpg" And _
strExtension <> "msg" And _
strExtension <> "docx" Then
Wscript.Echo objFile.Name
End If
Next
I just put a bunch of random extensions in because it wasn't clear which ones you want to ignore. Make sure you put them all in lowercase so the comparison works because the code converts the actual extension to lowercase.

For exporting the output to a txt file, the below code might work
Function TextFile_Generate()
strFile = "C:\...\ReportName.txt"
strValue = "output text..."
Set objFSO=CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
Set objFile = objFSO.OpenTextFile(strFile,8, True)
Else
Set objFile = objFSO.CreateTextFile(strFile,True)
End If
objFile.Write strValue & vbCrLf
objFile.Close
Set objFile = Nothing
Set objFSO =Nothing
End Function

Related

Memory leak with a recursive sub using VBScript

I'm creating a script that will allow a user to search within a specified directory with a search term. The script will create a CSV file and then write the base file name, the file size, the last modified date, and the absolute path of files that contain the search term within the file name. However, I'm running into an issue searching subfolders within the folder. The issue is that I'm running out of memory within the subroutine.
Here is the script I've written thus far.
Dim fileCount, searchPath, searchTerm, CheckFile, wholePath
Dim objFSO, objFolder, objFile, objWriteFile
Dim savePath
objCheckFile = "C:\Users\USERFILE\Desktop\Audit.csv"
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Asks for directory to search in
searchPath = InputBox("Please enter the path of the folder you would like to search", "Where am I searching?")
Set objFolder = objFSO.GetFolder(searchPath)
'Asks for the search term to use
searchTerm = InputBox("Please enter the term you would like to search for", "What am I searching?")
If objFSO.FileExists(objCheckFile) Then
WScript.Echo "Please delete the file named Audit.csv before trying again"
Else
Set objWriteFile = objFSO.CreateTextFile("Audit.csv", ForWriting, True)
End If
Set colFiles = objFolder.Files
Set colFolders = objFolder.SubFolders
'Searches for files within the folder and writes to a CSV file
For Each objFile In colFiles
If InStr(LCase(objFSO.GetFileName(objFile)), LCase(searchTerm)) > 0 Then
objWriteFile.Write objFSO.getFileName(objFile) & ", "
objWriteFile.Write objFile.size & ", "
objWriteFile.Write objFile.DateLastModified & ", "
objWriteFile.Write objFSO.getAbsolutePathName(objFolder) & objFSO.getFileName(objFile)
objWriteFile.Writeline
End If
Next
ShowSubFolder objFolder
Sub ShowSubFolder(Folder)
If InStr(LCase(objFSO.GetFileName(objFile)), LCase(searchTerm)) > 0 Then
objWriteFile.Write objFSO.getFileName(objFile) & ", "
objWriteFile.Write objFile.size & ", "
objWriteFile.Write objFile.DateLastModified & ", "
objWriteFile.Write objFSO.getAbsolutePathName(objFolder) & objFSO.getFileName(objFile)
objWriteFile.Writeline
End If
For Each objSubFolder In colFolders
ShowSubFolder objSubFolder
Next
End Sub
Your recursion never terminates because of this:
Set colFiles = objFolder.Files
Set colFolders = objFolder.SubFolders
'...
ShowSubFolder objFolder
Sub ShowSubFolder(Folder)
'...
For Each objSubFolder In colFolders
ShowSubFolder objSubFolder
Next
End Sub
This is similar to a fork bomb. For each subfolder of objFolder you recurse again into each subfolder of objFolder. And again. And again. And ...
Change your code to this and it should do what you want:
Set colFiles = objFolder.Files
'...
ShowSubFolder objFolder
Sub ShowSubFolder(Folder)
'...
For Each objSubFolder In Folder.SubFolders
ShowSubFolder objSubFolder
Next
End Sub
You may also want to adjust the conditional inside the recursive procedure, because it uses objFile and objFolder that are defined outside the procedure.

Vbscript copy files based on beginning letters

I am trying to get this script to copy all files starting with "XX". Currently it only copies one file.
Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\source")
strDestFolder = "C:\destination\"
For Each objFile In colFiles.Files
If Left(objFile.Name, 2) = "XX" Then
If objNewestFile = "" Then
Set objNewestFile = objFile
Else
If objNewestFile.DateLastModified < objFile.DateLastModified Then
Set objNewestFile = objFile
End If
End If
End If
Next
If Not objNewestFile Is Nothing Then
objFSO.CopyFile objNewestFile.Path,strDestFolder,True
End If
WScript.Echo "Copied."
You can use wildcards * and ? in [source] argument of FSO .CopyFile method.
So the code may look like:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\source\XX*.*", "C:\destination\", True
WScript.Echo "Copied."

list all files in a folder and sub folder without extention

I've come across the following script that I'd really like to use but I would like it not to have the .extention at the end
Dim fso
Dim ObjOutFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjOutFile = fso.CreateTextFile("C:\Users\User\Movies.csv")
ObjOutFile.WriteLine("Type,File Name,Size")
GetFiles("E:\")
ObjOutFile.Close
Function GetFiles(FolderName)
On Error Resume Next
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile
Set ObjFolder = fso.GetFolder(FolderName)
Set ObjFiles = objfolder.Files
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine("File," & ObjFile.Name & "," & objFile.Size & "," & objFile.Type)
Next
Set ObjSubFolders = ObjFolder.SubFolders
For Each ObjFolder In ObjSubFolders
ObjOutFile.WriteLine("Folder," & ObjFolder.Name)
GetFiles(ObjFolder.Path)
Next
End Function
I'm rubbish at this but I would really apperciate the help
Use the .GetBaseName() method of the FileSystemObject. As in:
>> WScript.Echo goFS.GetBaseName("c:\dir\name.ext")
>>
name

copying files to another folder and renaming them based on the creation date using vbscript

I'm fairly new to vbscript and programming! As I've already mentioned in the title, I've written (or at least I've tried) a vbscript which should copy and rename all files in C:\test\ and the subfolders of C:\test\ to a another Folder, named C:\test1.
Here's what I've got so far:
Dim objStartFolder, objtargetFolder, objDateCreatedName
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\test"
objtargetFolder = "C:\test1"
Set objFolder = objFSO.GetFolder(objStartFolder)
WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
WScript.Echo objFile.Name
Next
WScript.Echo
ShowSubFolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
WScript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile In colFiles
Set objDateCreatedName = objFSO.GetFile(objStartFolder)
WScript.Echo objDateCreatedName.DateCreated
WScript.Echo "I'm going to copy " & objFolder.Path & objFile.Name & " to " & objtargetFolder & objtargetFolder & objFile.Name & "."
Next
WScript.Echo
ShowSubFolders Subfolder
Next
End Sub
It would be really nice if you could help me and if you need more Information I'll make sure to deliver them.
If every file should go into the same destination folder you could simply do something like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Function Pad(s)
Pad = Right("00" & s, 2)
End Function
Sub CopyFiles(fldr, dst)
'Copy all files from fldr to destination folder and append the date (in ISO
'format) to the name. Overwrite existing files.
For Each f In fldr.Files
created = Year(f.DateCreated) & "-" & Pad(Month(f.DateCreated)) & "-" & _
Pad(Day(f.DateCreated))
newname = fso.GetBaseName(f) & "_" & created & "." & fso.GetExtensionName(f)
f.Copy fso.BuildPath(dst, newname), True
Next
'Recurse into subfolders.
For Each sf In fldr.SubFolders
CopyFiles sf, dst
Next
End Sub
CopyFiles fso.GetFolder("C:\test"), "C:\test1"

Vbscript list all PDF files in folder and subfolders

Well here is my code but I just can not filter the listing using the objFile.Extension i am sure it is some thing silly
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\dev"
Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
If objFile.Extension = "PDF" Then
Wscript.Echo objFile.Name
End If
Next
Wscript.Echo
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub
On run it comes back with the error
(11, 1) Microsoft VBScript runtime error: Object doesn't support this
property or method: 'objFile.Extension'
You'll want to use the GetExtensionName method on the FileSystemObject object.
Set x = CreateObject("scripting.filesystemobject")
WScript.Echo x.GetExtensionName("foo.pdf")
In your example, try using this
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = "PDF" Then
Wscript.Echo objFile.Name
End If
Next
(For those who stumble upon this from your search engine of choice)
This just recursively traces down the folder, so you don't need to duplicate your code twice. Also the OPs logic is needlessly complex.
Wscript.Echo "begin."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSuperFolder = objFSO.GetFolder(WScript.Arguments(0))
Call ShowSubfolders (objSuperFolder)
Wscript.Echo "end."
WScript.Quit 0
Sub ShowSubFolders(fFolder)
Set objFolder = objFSO.GetFolder(fFolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = "PDF" Then
Wscript.Echo objFile.Name
End If
Next
For Each Subfolder in fFolder.SubFolders
ShowSubFolders(Subfolder)
Next
End Sub
The file extension may be case sentive...but the code works.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Dev\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "pdf" Then
Wscript.Echo objFile.Name
End If
Next
Wscript.Echo
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\NOLA BOOTHE\My Documents\operating system"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
May not help OP, but hopefully others may find this helpful:
run
%ComSpec% /c cd/d StartPath & dir/s/b *.pdf
using shell object
StdOut will contain all PDF files
There's a well documented answer to your question at this url:
http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/18/how-can-i-list-the-files-in-a-folder-and-all-its-subfolders.aspx
The answer shown at that URL is kind of complicated and uses WMI (Windows Management Instrumentation) to iterate through files and folders. But if you do a lot of Windows administration, it's worth the effort to learn WMI.
I'm posting this now in case you need something right now; but I think I used to use a filesystemobject based approach, and I'll look for some example, and I'll post it later if I find it.
I hope this is helpful.
Check this code :
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Folder1\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "pdf" Then
Wscript.Echo objFile.Name
End If
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
for each Files in colFiles
if LCase(InStr(1,Files, ".pdf")) > 1 then Wscript.Echo Files
next
ShowSubFolders Subfolder
Next
End Sub

Resources