How to make a batch file edit a text file - batch-file

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

Related

VBscript create array but omit blank variables

I am looking to create a script that will compare the last modified date of up to 4 files then capture the one most recently modified. My current road block is that some files might not exist.
The set will fail if the file does not exist but I can easily overcome that by looking for the file and if it does not exist simply skip the set command. This would cause the creation of the array to fail because my variable is now blank.
Any suggestions how to resolve this?
Here is what I have so far:
Option Explicit
Dim objFSO, path, file, recentDate, recentFile, File1, File2, File3, File4, afiles, File1date, date1
Set objFSO = CreateObject("Scripting.FileSystemObject")
set File1=objFSO.getfile("c:\temp\file.txt")
set File2=objFSO.getfile("c:\test\File.txt")
set File3=objFSO.getfile("c:\users\%profile%\documents\File.txt")
set File4=objFSO.getfile("c:\users\public\documents\File.txt")
'Prepare variables to store the required information
Dim dateMin, dateMax
date1 = File1.datelastmodified
wscript.echo date1
wscript.echo now
dateMin = date1
dateMax = date1
afiles = Array( File1.datelastmodified, File2.datelastmodified, File3.datelastmodified, File4.datelastmodified )
Dim i
For i=1 to UBound(aFiles)
If aFiles(i) < dateMin Then dateMin = aFiles(i)
if aFiles(i) > dateMax Then dateMax = aFiles(i)
Next
'
' Output Information
WScript.Echo "Highest: " & CStr( dateMax )
WScript.Echo " Lowest: " & CStr( dateMin )
I think this would be much easier if you use an ArrayList to capture the DateLastModified for each file.
An ArrayList lets you dynamically add values unlike a VBScript Array.
Furthermore, the ArrayList has a very useful method called Sort() we can use here to determine the dateMin and dateMax values.
Try:
Option Explicit
Dim objFSO, objList, objFile, arrFiles, fileName
arrFiles = Array("c:\temp\file.txt","c:\test\File.txt","c:\users\%profile%\documents\File.txt","c:\users\public\documents\File.txt")
Set objList = CreateObject("System.Collections.ArrayList")
Set objFSO = CreateObject("Scripting.FilesystemObject")
For Each fileName In arrFiles
If objFSO.FileExists(fileName) Then
Set objFile = objFSO.GetFile(fileName)
objList.Add objFile.DateLastModified
End If
Next
objList.Sort()
' Prepare variables to store the required information
Dim dateMin, dateMax
dateMin = objList.item(0)
dateMax = objList.item(objList.Count - 1)
' Output Information
WScript.Echo "Highest: " & CStr( dateMax )
WScript.Echo "Lowest: " & CStr( dateMin )
' Clean up
Set objFSO = Nothing
Set objFile = Nothing
Set objList = Nothing
Edit
The above code was a remake of your original code. I may have misunderstood then that your goal is to find the file most recently modified.
This should do what you have in mind:
Option Explicit
Dim objFSO, objFile, objLatest, fileName, arrFiles
arrFiles = Array("c:\temp\file.txt","c:\test\File.txt","c:\users\%profile%\documents\File.txt","c:\users\public\documents\File.txt")
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objLatest = Nothing
For Each fileName In arrFiles
If objFSO.FileExists(fileName) Then
Set objFile = objFSO.GetFile(fileName)
If (objLatest Is Nothing) Then
Set objLatest = objFile
ElseIf (objFile.DateLastModified > objLatest.DateLastModified) Then
Set objLatest = objFile
End If
End If
Next
If objLatest Is Nothing Then
WScript.Echo "None of the files in 'arrFiles' exist.."
Else
' Here you decide if you want to keep the latest file as Object (-> objLatest) or just as a string to the full pathname of that file
Dim latestFile
latestFile = objFSO.GetAbsolutePathName(objLatest) ' store the full path and filename
' Info on FormatDateTime() at https://www.w3schools.com/asp/func_formatdatetime.asp
WScript.Echo "Most recently modified file is '" & latestFile & "' at " & FormatDateTime(objLatest.DateLastModified, 0)
End If
' Clean up
Set objFSO = Nothing
Set objFile = Nothing
Set objLatest = Nothing

Read from array one by one - VBScript

I am trying to read from a file, create an array, then write to a second file line by line (element by element) while adding some additional information. The script below will read correctly from a file, but will only write the last line in the initial file. How can I write from the array one at a time? (I can write the contents to the second file in a different manner, but I want to do it directly from the array).
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(input,
ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
arrFields = Split(strLine, ",")
If InStr(arrFields(1), "False") Then
strContents = strContents & arrFields(0) & vbCrlf
End If
Loop
objFile.Close
Set objFile = objFSO.CreateTextFile(output)
' below correctly outputs contents
' objFile.Write strContents
For each i in arrFields
objFile.Write arrFields(0)
objFile.Write "Number" & vbCrlf
Next
objFile.Close
My test csv file reads as below saved as "test.csv" from notepad.
1,FALSE
2,FALSE
3,FALSE
4,FALSE
5,FALSE
6,FALSE
7,FALSE
8,FALSE
9,FALSE
10,FALSE
11,FALSE
12,FALSE
13,TRUE
Sample code
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Users\pankaj.jaju\Desktop\test.csv", ForReading)
arrFields = Split(objFile.ReadAll, vbCrLf)
objFile.Close
Set objFile = objFSO.CreateTextFile("C:\Users\pankaj.jaju\Desktop\test1.txt")
For i = LBound(arrFields) To UBound(arrFields)
objFile.Write "Number " & i+1 & "," & Split(arrFields(i),",")(0) & vbCrlf
Next
objFile.Close

vbs file search for extention in a directory

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

How to find and read file using wild cards with VBscript?

I am trying unsuccessfully to read an XML file (The file name is change in each computer).
How it possible to read the file using wild card?
for example: D:\Logs\*.xml
Script:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\Logs\*.xml", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "server1 ", "server889 ")
Set objFile = objFSO.OpenTextFile("D:\Logs\*.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Closeenter code here
There is no wild card in VBScript. Repeat your group of statements for each element in Files collection of Folder object obtained by GetFolder Method:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("D:\Logs")
Set colFiles = objFolder.Files
For Each oFile in colFiles
If UCase(objFSO.GetExtensionName(oFile.name)) = "XML" Then
Set objFile = objFSO.OpenTextFile(oFile.Path, ForReading)
strText = objFile.ReadAll
objFile.Close
If Instr( 1, strText, "server1 ", vbTextCompare) > 0 Then
strNewText = Replace(strText, "server1 ", "server889 ", 1, -1, vbTextCompare)
Set objFile = objFSO.OpenTextFile(oFile.Path, ForWriting)
objFile.WriteLine strNewText
objFile.Close
End If
End If
Next
Resource: FileSystemObject

vbs read from file and parse it to the batch file

I had a script which worked on win xp but beacuse some of the methods are not available anymore I had to rewrite the script and use mshta.exe.
VBS script should do two things:
Read from file
Parse the info to the batch file
I have done the 1st bit but in the 2nd part I am getting an error. Could you please point me to the right direction?
Option Explicit
Dim objFso
Dim strFileName
Dim strFile
Dim objShell
Dim cimv2
Dim RemoteMachine
Dim YesNo
Dim out
Dim crt
Dim objDialog, intResult
Dim objTextFile, strText, iintresult
Dim objExec, strMSHTA, wshShell
mainMenu
Sub mainMenu()
do
out = inputbox("Choose option:" & vbcr & "1 - Deployment" & vbcr & "0 - Exit", "Menu", "0")
If out="1" then
call SelectFile
End If
If out="0"
then WScript.Quit
End if
loop
End Sub
Sub bgInfo(param)
YesNo = Msgbox("deployment? " & param, 4)
if YesNo = vbYes Then
do while not strMSHTA.AtEndOfStream
RemoteMachine = strMSHTA.ReadLine()
On Error Resume Next
objShell.Run "bginfo.bat " & RemoteMachine, 1, true
On Error Goto 0
loop
end if
End Sub
Sub SelectFile( )
strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _
& "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
& ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""
Set wshShell = CreateObject( "WScript.Shell" )
Set objExec = wshShell.Exec( strMSHTA )
intResult = objExec.StdOut.ReadLine( )
msgbox iintresult
If intResult <> "" Then
call bgInfo(intResult)
End If
Set objExec = Nothing
Set wshShell = Nothing
End Sub
Set objFso = Nothing
Set strFileName = Nothing
Set strFile = Nothing
Set objShell = Nothing
Set cimv2 = Nothing
Set RemoteMachine = Nothing
Set fso = Nothing
I am getting an error in line 33 char 5
Error: Object required: strMSHTA
thank you! :)
I had to add this code:
Set objFso = CreateObject("Scripting.FileSystemObject")
set strFile = objFso.OpenTextFile(param2, 1, True)
Set objShell = WScript.CreateObject("Wscript.Shell")`

Resources