csv - WMI Query returning null collection when querying Win32_Directory -
i'm trying use vbscript select csv's in specific folder , concatenate them one. adding of csv's collection using execquery on win32_directory, specifying path , extension properties. have 5 csv's in folder test. collection returned null.
here's code:
strcomputer = "." set wshshell = wscript.createobject( "wscript.shell" ) set objwmiservice = getobject("winmgmts:" _ & "{impersonationlevel=impersonate}!\\" & strcomputer & "\root\cimv2") 'options createtextfile booloverwrite = true boolunicode = true 'options opentextfile constforreading = 1 constforwriting = 2 constforappending = 8 boolcreate = false consttristate = -1 strpath = "c:\users\adam\documents\test\temp.csv" strdrivepath = "c:\users\adam\documents\test" 'creates object reference files in relevant folder. set objfso = createobject ("scripting.filesystemobject") 'creates new csv write others' contents to. set objnew = objfso.createtextfile(strpath,booloverwrite,boolunicode) set colcsv = objwmiservice.execquery _ ("select * win32_directory path = strdrivepath , extension = 'csv'") 'concatenates contents of csvs each objcsv in colcsv set objtemp = objfso.opentextfile(objcsv.path & objcsv.filename & objcsv.extension,constforreading,boolcreate,consttristate) set strline = objtemp.readline objnew.write strline next i unsure of whether way i've specified path opentextfile going work or not. main concern right getting query return files want.
thanks help!
you've got few issues here.
if want select files, use
cim_datafileclass.win32_directoryfor, guessed it, directories.backslashes in
pathproperty must escaped (\\).strdrivepathvariable you're using literally in wmi query.is intention run script on remote machine? if not, why not use
filesystemobjectmethods? have fso object created.
here similar question answered in past showing how can use filesystemobject or wmi query find files matching specification.
in situation, query might this:
strfilespec = "c:\\users\\adam\\documents\\test\\%.csv" set colcsv = objwmiservice.execquery _ ("select * cim_datafile name '" & strfilespec & "'") but query run faster (often noticeably) if specify values drive , path in statement. see linked post example.
edit: realized op of question linked well!
Comments
Post a Comment