[python-win32] Question concerning wmi c.CIM_DataFile

Mike Driscoll mdriscoll at co.marshall.ia.us
Mon Jan 4 22:18:43 CET 2010


On 1:59 PM, Tim Roberts wrote:
> Kelvin Lomboy Mendez wrote:
>    
>> I'm currently writing a script to access approximately 400 computers
>> for removing a particular app from the system. I'm having problems
>> getting results using the wmi c.CIM_DataFile (name=file). When I use
>> file as a variable which equals to the path to the app I want to
>> remove I don't get nothing back, however, if I hard code the path, I
>> get results. See snippet below for an example of what I'm talking
>> about. I appreciate in advance for any help I can get. Thank you.
>>
>>
>>
>> import wmi
>>
>> import re
>>
>>
>>
>> def queryFile(file, host):
>>
>>      print '[Debug nukeXmas()]:' + file
>>
>>      c = wmi.WMI(host)
>>
>>      for f in c.CIM_DataFile (name=file):
>>
>>          print 'Install Date:', f.InstallDate
>>
>>
>>
>> def queryFile2(file, host):
>>
>>      print '[Debug nukeXmas()]:' + file
>>
>>      c = wmi.WMI(host)
>>
>>      for f in c.CIM_DataFile
>> (name="C:\\DOCUME~1\\profileName\\LOCALS~1\\Temp\\Temporary Directory
>> 1 for deluxetreee (2).zip\\Christmas.exe"):
>>
>>          print 'Install Date:', f.InstallDate
>>
>>
>>
>>   #File to query
>>
>> string = "C:\DOCUME~1\profileName\LOCALS~1\Temp\\Temporary Directory 1
>> for deluxetreee (2).zip\Christmas.exe"
>>
>>
>>
>> #Substitute "\" to "\\"
>>
>> path = re.sub('\\\\', '\\\\\\\\', string)
>>
>>      
> Don't do this.  You WANT the string to contain single backslashes.  This
> is one of the most confusing things about working with strings in Python
> on Windows.  Here's a test.  How many characters are in this string?
>
>      xyz = "a\\b\tc"
>
> The answer is 5.  There's a letter "a", a backslash, a letter "b", a
> tab, and a letter "c".
>
> When you pass a file name into an API, the name should only contain
> single backslashes (or forward slashes -- both work equally well in the
> Win32 API).  In order to GET single backslashes in a Python string
> literal, you need to type TWO (or use the raw r"string" concept), but
> the string itself only contains one.
>
>    
>> #Close query path with double quotes
>>
>> file = '"'+path+'"'
>>
>>      
> Don't do this either.  The file name does not contain quotes.  If you
> are typing the file name on a command-line, you have to provide quotes
> so they get handled by the command line parser properly, but when you're
> calling an API, you should NEVER use quotes.
>
>    
>> #I'm having problems here, query brings back nothing
>>
>> queryFile(file, '172.27.1.5')
>>
>>      
> Right, because the file name you are passing here is literally this:
>      "C:\\DOCUME~1\\profileName\\LOCALS~1\\Temp\\Temporary Directory 1
> for deluxetreee (2).zip\\Christmas.exe"
>
>    
>>
>>
>> #Here, I hard code the path to the exe and it works
>>
>> queryFile2(file, '172.27.1.5')
>>
>>      
> Right, because the file name you are passing here is literally this:
>      C:\DOCUME~1\profileName\LOCALS~1\Temp\Temporary Directory 1 for
> deluxetreee (2).zip\Christmas.exe
>
> And THAT is what the file name really is.
>
> By the way, your whole concept is loony.  This will not work at all on
> Vista or Win 7, because the "Documents and Settings" tree is now called
> "Users".  You are assuming that every system is going to have this file
> as "...tree (2).zip", but depending on how many times it has been
> downloaded, it might be called something different (like "...tree.zip"
> or "...tree(3).zip").  Further, the directory names you are using are
> configurable.  They don't have to be called "Documents and Setting" or
> "Local Settings", and they don't have to be located on "C:".  You should
> be using the shell folder APIs to find the names of the "well-known
> folders".
>
> Plus, the whole "Local Settings\Temp" folder is volatile.  You should
> just be able to wipe out that whole directory without causing any damage.
>
>    

The OP should check out Tim Golden's winshell module as it gives access 
to the common folders quite easily:

http://timgolden.me.uk/python/winshell.html



-- 
*Mike Driscoll*

Blog: http://blog.pythonlibrary.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100104/c0833297/attachment.htm>


More information about the python-win32 mailing list