<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 1:59 PM, Tim Roberts wrote:
<blockquote cite="mid:%3C4B423A29.9050305@probo.com%3E" type="cite">
  <pre wrap="">Kelvin Lomboy Mendez wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">
I&#8217;m currently writing a script to access approximately 400 computers
for removing a particular app from the system. I&#8217;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&#8217;t get nothing back, however, if I hard code the path, I
get results. See snippet below for an example of what I&#8217;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)

    </pre>
  </blockquote>
  <pre wrap="">
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.

  </pre>
  <blockquote type="cite">
    <pre wrap="">#Close query path with double quotes

file = '"'+path+'"'

    </pre>
  </blockquote>
  <pre wrap="">
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.

  </pre>
  <blockquote type="cite">
    <pre wrap="">#I'm having problems here, query brings back nothing

queryFile(file, '172.27.1.5')

    </pre>
  </blockquote>
  <pre wrap="">
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"

  </pre>
  <blockquote type="cite">
    <pre wrap=""> 

#Here, I hard code the path to the exe and it works

queryFile2(file, '172.27.1.5')

    </pre>
  </blockquote>
  <pre wrap="">
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.

  </pre>
</blockquote>
<br>
The OP should check out Tim Golden's winshell module as it gives access
to the common folders quite easily:<br>
<br>
<a class="moz-txt-link-freetext" href="http://timgolden.me.uk/python/winshell.html">http://timgolden.me.uk/python/winshell.html</a><br>
<br>
<br>
<br>
<div class="moz-signature">-- <br>
<strong>Mike Driscoll</strong>
<p>Blog: <a class="moz-txt-link-freetext" href="http://blog.pythonlibrary.org">http://blog.pythonlibrary.org</a>
</p>
</div>
</body>
</html>