list all files in a folder and sub folder without extention - vbscript

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

Related

Prepend text to Text file using VBScript

I have this script to allow me to insert text into a text file but I need it to be at the start of the text file. This script currently adds this to the end of the .txt file.
And I am new to trying these things out myself
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\scripts"
strFile = "\csv.txt"
strText = "sep=|"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
set objFile = nothing
set objFolder = nothing
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
objTextFile.WriteLine(strText)
objTextFile.Close
WScript.Quit
You can read the file's content into a string, add your string in front of that and write everything back to the same file.
Dim sFileText
Dim sPrependText
Const ForReading = 1, ForWriting = 2
' Open file For Reading and Read All content to a variable
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForReading, True)
sFileText = objTextFile.ReadAll
objTextFile.Close
' Prepend text in front of file's content
sPrependText = "sep=|"
sFileText = sPrependText & sFileText
' Open file For Writing and write text variable
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForWriting, True)
objTextFile.Write sFileText
objTextFile.Close

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

VBS to list file names NOT matching given extension

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

Write names of files in a folder that have been created/modified today to text file

I have written a VBScript to write all the file names in a folder to a text file, but what I actually want is a listing of only the names of .txt files in a specific folder that have been modified or created today.
The code:
Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder("D:\test")
Set ObjOutFile = fso.CreateTextFile("D:\test\File_names.txt")
Set ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine(ObjFile.Name)
Next
ObjOutFile.Close
Its a long time since I have written any VBScript but this is along the right lines. You may need to adjust it a bit
Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder("c:\temp\")
Set ObjOutFile = fso.CreateTextFile("c:\temp\File_names.txt")
Set ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then
If FormatDateTime(ObjFile.DateLastModified, 2) = FormatDateTime(Now(), 2) OR FormatDateTime(ObjFile.DateCreated , 2) = FormatDateTime(Now(), 2) Then
ObjOutFile.WriteLine(ObjFile.Name)
Else
ObjOutFile.WriteLine(ObjFile.Name & " - Modified:" & FormatDateTime(ObjFile.DateLastModified, 2) & " " & FormatDateTime(Now(),2) & " Created: " & FormatDateTime(ObjFile.DateCreated , 2) & " " & FormatDateTime(Now(), 2) )
End if
Else
ObjOutFile.WriteLine(ObjFile.Name & " Not Text")
End if
Next
ObjOutFile.Close
A code snippet with changes only:
For Each ObjFile In ObjFiles
If StrComp( Right( ObjFile.name, 4), ".txt", vbTextCompare) = 0 and _
( Fix( ObjFile.DateCreated) = Fix( Now) or _
Fix( ObjFile.DateLastModified) = Fix( Now) ) Then
ObjOutFile.WriteLine(ObjFile.Name)
End If
Next
FIX() explanation:
date format is a floating point value, counting days since midnight 30
December 1899. Hours and minutes are represented as fractional days

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