I'm trying to write this code in VBs, (I'm quite new)
I want to have the code go through a folder/directory, and pick out all ".txt" files, in something like a for loop. These text files could then be listed using MsgBox ("txt filename"). This is what I've got so far:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\Desktop\folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = ".txt" Then
Wscript.Echo objFile.Name
Next
It doesn't seem to be picking out a txt file called "name.txt". Any help would be greatly appreciated.
ps:
please ignore and bad spelling and my terrible formatting (im new to stackoverflow) Thanks! (also the code above is mostly mashed together code i found off the internet)
You should modify this line ; if you use UCase :
If UCase(objFSO.GetExtensionName(objFile.name)) = ".txt"
To
If UCase(objFSO.GetExtensionName(objFile)) = "TXT"
Or if you use LCase
If LCase(objFSO.GetExtensionName(objFile)) = "txt"
And your code looks like this one :
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\Desktop\folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If LCase(objFSO.GetExtensionName(objFile)) = "txt" Then
Wscript.Echo objFile.Name
End If
Next
GetExtensionName Method
Related
I have a script to list the files in a directory including their length:
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txtOut = objFSO.CreateTextFile("d:\FileNameLength.csv")
FoldersRec "d:\MyData"
Sub FoldersRec(Startfolder)
Set objFolder = objFSO.GetFolder(Startfolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder In colSubfolders
FoldersRec(objSubfolder)
Next
For Each file In objFolder.Files
strZeile = file & ";" & Len(file)
txtOut.WriteLine strZeile
Next
''MsgBox "Done"
End Sub
It works fine. What I would like to change is to convert it to an explicit loop after which I can add something, like for instance a message box.
If I just enter MsgBox "Done" just before End Sub it would appear for each file.
Therefore, the idea is to add something like loop while file exists or loop while Len(file)>0. But I am not quite sure about the syntax.
Why not like this:
Set txtOut = objFSO.CreateTextFile("d:\FileNameLength.csv")
FoldersRec "d:\MyData"
MsgBox "Done"
I would rewrite your code like this:
Set fso = CreateObject("Scripting.FileSystemObject")
ListSizes fso.GetFolder("D:\MyData"), fso.CreateTextFile("d:\FileNameLength.csv")
MsgBox "Done"
Sub ListSizes(folder, outStream)
Dim subfolder, file
For Each subfolder In folder.Subfolders
ListSizes subfolder, outStream
Next
For Each file In folder.Files
outStream.WriteLine file & ";" & file.Size
Next
End Sub
This way you can pass any Stream object to ListSizes, for example the console (when called through cscript.exe):
ListSizes fso.GetFolder("D:\MyData"), WScript.StdOut
Also, I suspect want the file .Size (in bytes), and not the length of the file path?
So, here is the best solution for my problem:
Set fso = CreateObject("Scripting.FileSystemObject")
ListSizes fso.GetFolder("s:"), fso.CreateTextFile("r:\FileNameLength.csv")
MsgBox "Done"
Sub ListSizes(folder, outStream)
Dim subfolder, file
For Each subfolder In folder.Subfolders
On Error Resume Next
ListSizes subfolder, outStream
Next
For Each file In folder.Files
outStream.WriteLine file & ";" & file.Size & ";" & len(file)
Next
End Sub
With this solution I get both file name length and file size.
I want to load all files of a directory into an array with vbscipt/hta in order to sort and "call" them later by index. I've tried something like this, but it's not working:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFileFolder = "C:\"
Set objFolder = objFSO.GetFolder(objFileFolder)
Set colFiles = objFolder.Files
dim arrFileList()
For Each objFile in colFiles
ReDim Preserve arrFileList(UBound(arrFileList) + 1)
FileList(UBound(arrFileList)) = objFile.Name
Next
I'd be grateful for any help! THX in advance
You need to change two things, please see new code below. Comments in line.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFileFolder = "C:\"
Set objFolder = objFSO.GetFolder(objFileFolder)
Set colFiles = objFolder.Files
dim arrFileList()
ReDim Preserve arrFileList(0) 'If you wish to use the array UBound later on you must redim this here
For Each objFile in colFiles
ReDim Preserve arrFileList(UBound(arrFileList) + 1)
arrFileList(UBound(arrFileList)) = objFile.Name ' Here you were calling FileList, not arrFileList
Next
You could further tidy / improve this code as arrFileList(0) will have no value upon finishing.
I can't find anything to replace %~dp0 in my VBScript
On error resume next
Set shell= createobject("WSCRIPT.SHELL")
Shell.run "%~dp0test.bat", vbhide
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(Wscript.ScriptFullName)
Taken from here:
how to get working drive and directory?
To use the obtained value in your script, add a reference to the variable where you need its value. For instance:
Shell.run GetTheParent & "\test.bat", vbhide
To get the folder the executing .VBS resides in:
type dp0.vbs
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo "WScript.ScriptFullName", WScript.ScriptFullName
WScript.Echo "oFS.GetParentFolderName(WScript.ScriptFullName)", oFS.GetParentFolderName(WScript.ScriptFullName
)
cscript dp0.vbs
WScript.ScriptFullName E:\trials\SoTrials\answers\tools\jscript\ijs\dp0.vbs
oFS.GetParentFolderName(WScript.ScriptFullName) E:\trials\SoTrials\answers\tools\jscript\ijs
This should work.
set objShell = Createobject("wscript.shell")
strPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
Shell.run strPath & "test.bat", vbhide
I have a VBS file that I am trying to use to determine what folders and files are in a certain directory. I believe I have the code written correctly, but whenever I try to write out the file or current directory I get a blank text document with nothing but the root directory written out. Any advice would be greatly appreciated.
Dim NewFile
Function GetFolders (strFolderPath)
Dim objCurrentFolder, colSubfolders, objFolder, files
Set objCurrentFolder = objFSO.GetFolder(strFolderPath)
Set colSubfolders = objCurrentFolder.SubFolders
For Each objFolder In colSubfolders
NewFile.WriteLine(" - " & objFolder.Path)
Set files = folder.Files
For each folderIdx In files
NewFile.WriteLine(" - "& folderIdx.Name)
Next
Call GetFolders (objFolder.Path)
Next
End Function
Dim fso, sFolder
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = Wscript.Arguments.Item(0)
If sFolder = "" Then
Wscript.Echo "No Folder parameter was passed"
Wscript.Quit
End If
Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)
NewFile.WriteLine(sFolder)
Call GetFolders(sFolder)
NewFile.Close
You haven't payed sufficient attention to your variable naming. Your script is a good example of the reason why all VBScripts should start with the line:-
Option Explicit
This would highlight all the variables that haven't been declared which in turn will point out typos and inconsistencies in variable naming. Here is how I would write it:-
Option Explicit
Dim msFolder : msFolder = Wscript.Arguments.Item(0)
If msFolder = "" Then
Wscript.Echo "No Folder parameter was passed"
Wscript.Quit
End If
Dim mfso : Set mfso = CreateObject("Scripting.FileSystemObject")
Dim moTextStream : Set moTextStream = mfso.CreateTextFile(msFolder & "\FileList.txt", True)
moTextStream.WriteLine(msFolder)
WriteFolders mfso.GetFolder(msFolder)
moTextStream.Close
Sub WriteFolders(oParentFolder)
Dim oFolder
For Each oFolder In oParentFolder.SubFolders
moTextStream.WriteLine(" - " & oFolder.Path)
Dim oFile
For Each oFile In oFolder.Files
moTextStream.WriteLine(" - " & oFile.Name)
Next
WriteFolders oFolder
Next
End Sub
I got the code
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "C:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"ex3")> 0 Then
strLine = Replace(strLine,"ex3","ex5")
End If
WScript.Echo strLine
Loop
The strLine replacing part i can fix myself to use with my own purposes, but how do i do something like this so that it doesn't require the file's name, it just edits all text files within the document?
you can do it like this,
strFolder = "c:\myfolder"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
strFileName =strFile.Name
strFilePath = strFile.Path
strFileExt = objFS.GetExtensionName(strFile)
If strFileExt = "txt" Then
Set objFile = objFS.OpenTextFile(strFile)
' your current code here..
objFile.Close()
End If
Next