Azure Files: List Directories and Files with Pagination
See more Azure Cloud Storage Examples
List the immediate children of an existing directory in an SMB file share. Follow continuation markers even after an empty page, distinguish files from directories, and keep FileId and Content-Length as strings. This is not recursive; an application can queue discovered directories for traversal.
x-ms-file-extended-info requests current SMB file sizes. Enumeration is not an immutable snapshot when other clients are changing files. Name elements marked Encoded=true are URL-decoded for display; encode individual path segments again when constructing a subsequent request.
Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.
Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.
Chilkat Ruby Downloads
require 'chilkat'
success = false
# StringBuilder compares text by value in every generated language.
textCompare = Chilkat::CkStringBuilder.new()
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
config = Chilkat::CkJsonObject.new()
config.put_Strict(true)
success = config.LoadFile("qa_data/azure/storage.json")
if (success != true)
print "Could not load private Azure configuration." + "\n";
exit
end
textCompare.Clear()
textCompare.SetString(config.stringOf("account"))
sameText1 = textCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("account") != 1) or (sameText1 == true))
print "Supply the storage account name." + "\n";
exit
end
# access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear()
textCompare.SetString(config.stringOf("access_key"))
sameText2 = textCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("access_key") != 1) or (sameText2 == true))
print "Supply the base64 account access key." + "\n";
exit
end
endpoint = Chilkat::CkStringBuilder.new()
success = endpoint.Append(config.stringOf("account"))
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = endpoint.Append(".file.core.windows.net")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
# resourcePath is already URL-encoded. Encode each path segment once when adapting it.
# Keep slash separators; names and paths are case-sensitive.
resourcePath = "/examples/files"
rest = Chilkat::CkRest.new()
rest.put_ConnectTimeoutMs(20000)
rest.put_IdleTimeoutMs(60000)
azAuth = Chilkat::CkAuthAzureStorage.new()
azAuth.put_Account(config.stringOf("account"))
azAuth.put_AccessKey(config.stringOf("access_key"))
azAuth.put_Service("File")
azAuth.put_Scheme("SharedKey")
azAuth.put_XMsVersion("2026-04-06")
success = rest.SetAuthAzureStorage(azAuth)
if (success != true)
print "Could not configure Shared Key signing." + "\n";
exit
end
connection = Chilkat::CkSocket.new()
connection.put_RequireSslCertVerify(true)
connection.put_MaxReadIdleMs(60000)
connection.put_MaxSendIdleMs(60000)
success = connection.Connect(endpoint.getAsString(),443,true,20000)
if (success != true)
print "TLS connection failed; inspect diagnostics privately." + "\n";
exit
end
# Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
result = Chilkat::CkXml.new()
result.put_Strict(true)
success = rest.AddQueryParam("restype","directory")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddQueryParam("comp","list")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddHeader("x-ms-file-extended-info","true")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = rest.AddQueryParam("maxresults","1000")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
seenMarkers = Chilkat::CkHashtable.new()
marker = ""
more = true
page = 0
while (more == true)
if (page >= 1000)
print "Page limit reached; results are incomplete. Save the continuation marker." + "\n";
exit
end
responseText = rest.fullRequestNoBody("GET",resourcePath)
if (rest.get_LastMethodSuccess() != true)
print "Request did not complete; outcome may be uncertain." + "\n";
exit
end
if (rest.get_ResponseStatusCode() != 200)
print "HTTP status: " + rest.get_ResponseStatusCode().to_s() + "\n";
print "Azure error code: " + rest.responseHdrByName("x-ms-error-code") + "\n";
print "Request ID: " + rest.responseHdrByName("x-ms-request-id") + "\n";
# Inspect any error body privately; do not log credentials, SAS URLs or payloads.
exit
end
success = result.LoadXml(responseText)
if (success != true)
print "Expected well-formed XML." + "\n";
exit
end
if (result.TagEquals("EnumerationResults") != true)
print "Unexpected XML response root." + "\n";
exit
end
success = result.FindChild2("Entries")
if (success != true)
print "Missing directory Entries element." + "\n";
exit
end
directoryCount = result.NumChildrenHavingTag("Directory")
directoryIndex = 0
while (directoryIndex < directoryCount)
result.put_I(directoryIndex)
directoryName = result.getChildContent("Directory[i]|Name")
textCompare.Clear()
textCompare.SetString(directoryName)
sameText3 = textCompare.ContentsEqual("",true)
if (sameText3 != true)
textCompare.Clear()
textCompare.SetString(result.getChildAttrValue("Directory[i]|Name","Encoded"))
sameText4 = textCompare.ContentsEqual("true",true)
if (sameText4 == true)
decodedDirectory = Chilkat::CkStringBuilder.new()
success = decodedDirectory.Append(directoryName)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = decodedDirectory.Decode("url","utf-8")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
directoryName = decodedDirectory.getAsString()
end
print "Directory: " + directoryName + "\n";
print "File ID: " + result.getChildContent("Directory[i]|FileId") + "\n";
end
directoryIndex = directoryIndex + 1
end
fileCount = result.NumChildrenHavingTag("File")
fileIndex = 0
while (fileIndex < fileCount)
result.put_I(fileIndex)
fileName = result.getChildContent("File[i]|Name")
textCompare.Clear()
textCompare.SetString(fileName)
sameText5 = textCompare.ContentsEqual("",true)
if (sameText5 != true)
textCompare.Clear()
textCompare.SetString(result.getChildAttrValue("File[i]|Name","Encoded"))
sameText6 = textCompare.ContentsEqual("true",true)
if (sameText6 == true)
decodedFile = Chilkat::CkStringBuilder.new()
success = decodedFile.Append(fileName)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
success = decodedFile.Decode("url","utf-8")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
fileName = decodedFile.getAsString()
end
print "File: " + fileName + "\n";
print "File ID: " + result.getChildContent("File[i]|FileId") + "\n";
print "Size: " + result.getChildContent("File[i]|Properties|Content-Length") + "\n";
end
fileIndex = fileIndex + 1
end
success = result.GetParent2()
if (success != true)
print "Could not return to enumeration root." + "\n";
exit
end
marker = result.getChildContent("NextMarker")
page = page + 1
textCompare.Clear()
textCompare.SetString(marker)
sameText7 = textCompare.ContentsEqual("",true)
if (sameText7 == true)
more = false
else
if (seenMarkers.Contains(marker) == true)
print "Repeated marker; results are incomplete." + "\n";
exit
end
success = seenMarkers.AddInt(marker,1)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
if (page > 1)
success = rest.RemoveQueryParam("marker")
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
end
success = rest.AddQueryParam("marker",marker)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
rest.Disconnect(10)
success = connection.Connect(endpoint.getAsString(),443,true,20000)
if (success != true)
print "TLS reconnection failed." + "\n";
exit
end
success = rest.UseConnection(connection,false)
if (success != true)
print "Could not prepare the request." + "\n";
exit
end
end
end
print "Directory listing complete." + "\n";