1

SMBConnect has the following function, listPath, which lists out the the contents of a given directory.

listPath(service_name, path, search=55, pattern='*', timeout=30) Retrieve a directory listing of files/folders at path

Parameters:
service_name (string/unicode) – the name of the shared folder for the path

path (string/unicode) – path relative to the service_name where we are interested to learn about its files/sub-folders.

search (integer) – integer value made up from a bitwise-OR of SMB_FILE_ATTRIBUTE_xxx bits (see smb_constants.py). The default search value will query for all read-only, hidden, system, archive files and directories.

pattern (string/unicode) – the filter to apply to the results before returning to the client.

Returns:
A list of smb.base.SharedFile instances.

newConn=SMBConnection(arguments.username, password, DEFAULT_CLIENT_NAME, arguments.hostname, domain=arguments.domain,
            use_ntlm_v2=True, is_direct_tcp=True)
        assert newConn.connect(ip_address, 445, timeout=60)
        files = newConn.listPath('C$', '/' + 'testing', '*.pdf')
        for file in files:
            print(file.filename)

I cannot get the pattern matching to change to anything specific. Above, I want to print out only those filenames that contain ".pdf" in the listing. Instead when the code executes, I just get ALL the files. No errors or anything. I have tried with and without the '*' and '.' and get the same results.

3 Answers3

1

So, we got it to work with the re class as a work-around from the SMBConnection listPath function by using a variation of this that works with the created SMBConnection object. the ListPath function is still used but just not the pattern part of it. I built an "If-else" structure to handle the arg input and regex.

extensions = ['pdf', 'doc']
filenames = ['foobar.pdf', 'bar.doc']
for extension in extensions:
    compiled = re.compile('\.{0}$'.format(extension))
    for filename in filenames:
        results = re.search(compiled, filename)
        print results 
0

You can use the regular expression .+\.pdf.

.+: matches all characters except for a newline character 1 or more times.
\.pdf: escapes the dot character, since the . characters has a special meaning in regular expressions, and search for pdf after the .

  • This is incorrect. Regular expressions are not supported in pysmb. See link in my answer to another thread with more details. – FlexMcMurphy Feb 03 '20 at 19:54
0

The original poster wasn't specifying the pattern parameter in the listpath() method correctly.

Instead of this:

files = newConn.listPath('C$', '/' + 'testing', '*.pdf')

It should be like this:

files = newConn.listPath('C$', '/' + 'testing', pattern='*.pdf')

Actually I asked a question in another thread if it is possible to filter on more than one file extension possibly by way of a regular expression. The author of pysmb responded to say this is not possible.

To filter on more than one file extension the workaround created by the original poster to this thread could be used.

FlexMcMurphy
  • 420
  • 5
  • 21