locked
CreateObject( "System.Collections.ArrayList" ) issue in .Net4.5 RRS feed

  • Question

  • Hi ,

    We have a VBS code which uses the below line of code. In Windows 7 with .Net3.5 the line of code works fine but in Windows 10 with .net4.2 enabled this is not working fine and asking to install .Net3.5. For our client that is not acceptable. How can we resolve this issue.

    On Error Resume Next
    Const ForWriting = 2
    Const ForReading=1
    Dim fso, folder, sourcefolder,newfolder
    Dim timestamp , normalizedName,i
    
    Set objArgs = WScript.Arguments
    inputFolder = objArgs(0)
    textfilepath = objArgs(1)
    set fso = createobject("scripting.filesystemobject") 
    sourcefolder = fso.GetAbsolutePathName(inputFolder) & "\"
    textfilepathfolder = fso.GetAbsolutePathName(textfilepath) & "\"
    textFile=textfilepathfolder & "application_file_integrity.txt"
    
    set fso1 = createobject("scripting.filesystemobject") 
    set folder = fso.getfolder(sourcefolder)  
    
    Dim obj_datadict   ' Create a variable.
    Set obj_datadict = CreateObject("Scripting.Dictionary")
    Set myArrayList = CreateObject( "System.Collections.ArrayList" )
    Set filenameList = CreateObject( "System.Collections.ArrayList" )
    
    if not fso.folderexists(textfilepathfolder) then
    	newfolder = fso.createfolder(textfilepathfolder)
    	end if
    	
    'create text file to store result of integrity
    
    if not fso.fileExists(textFile) then
    	Set newtextfile = fso.CreateTextFile(textFile)
    	WScript.Sleep (100)
    	newtextfile.close
    end if
    
    ' Sort the timestamp to find the latest timestamp
    for each file in folder.files
    If LCase(fso.GetExtensionName(file.Name)) = "ist" Then
    	normalizedName = file.name
    	timeStamp= extracTimestamp(normalizedName)
    	myArrayList.add timeStamp
    	 End If
    	Next
    	myArrayList.sort()
    	'Getting the latest timestamp from the filename
    	latesttimestamp=myArrayList.Item(myArrayList.Count - 1)
    		
    for each file in folder.files
    If LCase(fso.GetExtensionName(file.Name)) = "ist" Then
    If InStr(file.name, latesttimestamp) Then
    filenameList.add file.name
    End If
    End If
    next
    
    	For each filenm in filenameList
    	'open text file to update
    	Set openedFile = fso.OpenTextFile(textFile, ForWriting, True)
    	Set myFile = fso1.OpenTextFile(sourcefolder & filenm,ForReading,True)
    	Do While Not myFile.AtEndofStream
    	myLine = myFile.ReadLine
    	' To get the application date value inside the file
    	If InStr(myLine, "ApplicationDate=") Then
    	appDt = Right(myLine,len(myLine)-16 )
    	' To get the timestamp value from the filename
    	fileNameDt=extracTimestamp(filenm)
    	fileNameDt=left(fileNameDt,4) & "/" & mid(fileNameDt,5,2) & "/"  & mid(fileNameDt,7,2)& " "& mid(fileNameDt,9,2)& ":"& mid(fileNameDt,11,2)& ":"& mid(fileNameDt,13,2)
    	filenameDate=cDate(fileNameDt)
    	dayoftheyear=mid(appDt,3,3) 
    	date1=DateSerial("20" & left(appDt,2),1,dayoftheyear)
    	dateformatoftheday=date1 & " " & mid(appDt,6,2)& ":"& mid(appDt,8,2)& ":"& mid(appDt,10,2)
    	appnameDate=cDate(dateformatoftheday)
    	formattedvalueF =FormatDateTime(filenameDate, 0)
    	formattedvalueA=FormatDateTime(appnameDate, 0)
    	'Comparing dates
    	if(formattedvalueF =formattedvalueA)	then
    	openedFile.WriteLine("FILENAME=Not Found"  &vbCr & vbLf &"RESULT=True" &vbCr & vbLf &"DATEOFFILEONE=Not Found"  &vbCr & vbLf &"DATEOFFILETWO=Not Found" &vbCr & vbLf)		
    	openedFile.close
    	else
    	openedFile.WriteLine("FILENAME=" & filenm &vbCr & vbLf &"RESULT=Fail" &vbCr & vbLf &"DATEOFFILEONE=" &formattedvalueF &vbCr & vbLf &"DATEOFFILETWO=" &formattedvalueA&vbCr & vbLf)
    	openedFile.close
    	end if
    	End If
    
    Loop
    myFile.close
    Next
    
    
    Function extracTimestamp(appsFileName)
    'Count the total number of characters in appsFileName
    	totalCharCount = len(appsFileName)
    	roleNameSize = totalCharCount - 18
    	extracTimestamp = Right(appsFileName,totalCharCount-roleNameSize)
    	extracTimestamp = Left(extracTimestamp,len(extracTimestamp)-4)
    	
    End Function 

    • Moved by Neda Zhang Monday, February 20, 2017 2:13 AM Moved from VB.NET
    Friday, February 17, 2017 1:05 PM

Answers

  • I take that back.  Net 3.5 is an optional install on W10.  I just lit up a raw W10 VM.  Just run "Programs & Features" and add features and check 3.5 + 2.0 support. It is the first item in the list.


    \_(ツ)_/

    Monday, February 20, 2017 2:46 AM
  • As an alternate you can use the Dictionary object as a list just add the items with an index.

    dct.Add(idx,<your string or item>)


    \_(ツ)_/

    Monday, February 20, 2017 2:48 AM

All replies

  • You are attempting to use a .NET namespace (System.Collections.ArrayList) from your VBScript code. If you do not want a .NET Framework dependency then you should use a VBScript Array or Collection object instead.

    For further coding questions regarding VBScript, please post to The Official Scripting Guys Forum!.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Cor Ligthert Friday, February 17, 2017 5:31 PM
    Friday, February 17, 2017 2:04 PM
  • Well AFAIK you either have to install .Net 3.5 on the Win10 system or possibly enable .Net 3.5 on the Win10 system since by default it may be available but not enabled.

    Acceptable or not acceptable regarding systems and applications isn't really in your purview as that has nothing to do with reality.

    I don't know how you run the script but perhaps alter it to work under something higher than .Net 3.5.


    La vida loca

    Friday, February 17, 2017 5:25 PM
  • Hi soumya_cts,

    Thank you for posting here.

    Since your issue is more related to VBScript, and this forum is discussing and asking questions about the VB.NET. So I will move this thread to The Official Scripting Guys Forum for better support.

    Thank you for your understanding and support.

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 20, 2017 2:11 AM
  • I take that back.  Net 3.5 is an optional install on W10.  I just lit up a raw W10 VM.  Just run "Programs & Features" and add features and check 3.5 + 2.0 support. It is the first item in the list.


    \_(ツ)_/

    Monday, February 20, 2017 2:46 AM
  • As an alternate you can use the Dictionary object as a list just add the items with an index.

    dct.Add(idx,<your string or item>)


    \_(ツ)_/

    Monday, February 20, 2017 2:48 AM