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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// StringBuilder compares text by value in every generated language.
CkStringBuilder textCompare = new CkStringBuilder();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkJsonObject config = new CkJsonObject();
config.put_Strict(true);
success = config.LoadFile("qa_data/azure/storage.json");
if (success != true) {
System.out.println("Could not load private Azure configuration.");
return;
}
textCompare.Clear();
textCompare.SetString(config.stringOf("account"));
boolean sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
System.out.println("Supply the storage account name.");
return;
}
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.stringOf("access_key"));
boolean sameText2 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
System.out.println("Supply the base64 account access key.");
return;
}
CkStringBuilder endpoint = new CkStringBuilder();
success = endpoint.Append(config.stringOf("account"));
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
success = endpoint.Append(".file.core.windows.net");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
String resourcePath = "/examples/files";
CkRest rest = new CkRest();
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
CkAuthAzureStorage azAuth = new CkAuthAzureStorage();
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) {
System.out.println("Could not configure Shared Key signing.");
return;
}
CkSocket connection = new CkSocket();
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
System.out.println("TLS connection failed; inspect diagnostics privately.");
return;
}
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
String responseText;
CkXml result = new CkXml();
result.put_Strict(true);
success = rest.AddQueryParam("restype","directory");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
success = rest.AddQueryParam("comp","list");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
success = rest.AddHeader("x-ms-file-extended-info","true");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
success = rest.AddQueryParam("maxresults","1000");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
CkHashtable seenMarkers = new CkHashtable();
String marker = "";
boolean more = true;
int page = 0;
while ((more == true)) {
if (page >= 1000) {
System.out.println("Page limit reached; results are incomplete. Save the continuation marker.");
return;
}
responseText = rest.fullRequestNoBody("GET",resourcePath);
if (rest.get_LastMethodSuccess() != true) {
System.out.println("Request did not complete; outcome may be uncertain.");
return;
}
if (rest.get_ResponseStatusCode() != 200) {
System.out.println("HTTP status: " + rest.get_ResponseStatusCode());
System.out.println("Azure error code: " + rest.responseHdrByName("x-ms-error-code"));
System.out.println("Request ID: " + rest.responseHdrByName("x-ms-request-id"));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
return;
}
success = result.LoadXml(responseText);
if (success != true) {
System.out.println("Expected well-formed XML.");
return;
}
if (result.TagEquals("EnumerationResults") != true) {
System.out.println("Unexpected XML response root.");
return;
}
success = result.FindChild2("Entries");
if (success != true) {
System.out.println("Missing directory Entries element.");
return;
}
int directoryCount = result.NumChildrenHavingTag("Directory");
int directoryIndex = 0;
while ((directoryIndex < directoryCount)) {
result.put_I(directoryIndex);
String directoryName = result.getChildContent("Directory[i]|Name");
textCompare.Clear();
textCompare.SetString(directoryName);
boolean sameText3 = textCompare.ContentsEqual("",true);
if (sameText3 != true) {
textCompare.Clear();
textCompare.SetString(result.getChildAttrValue("Directory[i]|Name","Encoded"));
boolean sameText4 = textCompare.ContentsEqual("true",true);
if (sameText4 == true) {
CkStringBuilder decodedDirectory = new CkStringBuilder();
success = decodedDirectory.Append(directoryName);
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
success = decodedDirectory.Decode("url","utf-8");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
directoryName = decodedDirectory.getAsString();
}
System.out.println("Directory: " + directoryName);
System.out.println("File ID: " + result.getChildContent("Directory[i]|FileId"));
}
directoryIndex = directoryIndex+1;
}
int fileCount = result.NumChildrenHavingTag("File");
int fileIndex = 0;
while ((fileIndex < fileCount)) {
result.put_I(fileIndex);
String fileName = result.getChildContent("File[i]|Name");
textCompare.Clear();
textCompare.SetString(fileName);
boolean sameText5 = textCompare.ContentsEqual("",true);
if (sameText5 != true) {
textCompare.Clear();
textCompare.SetString(result.getChildAttrValue("File[i]|Name","Encoded"));
boolean sameText6 = textCompare.ContentsEqual("true",true);
if (sameText6 == true) {
CkStringBuilder decodedFile = new CkStringBuilder();
success = decodedFile.Append(fileName);
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
success = decodedFile.Decode("url","utf-8");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
fileName = decodedFile.getAsString();
}
System.out.println("File: " + fileName);
System.out.println("File ID: " + result.getChildContent("File[i]|FileId"));
System.out.println("Size: " + result.getChildContent("File[i]|Properties|Content-Length"));
}
fileIndex = fileIndex+1;
}
success = result.GetParent2();
if (success != true) {
System.out.println("Could not return to enumeration root.");
return;
}
marker = result.getChildContent("NextMarker");
page = page+1;
textCompare.Clear();
textCompare.SetString(marker);
boolean sameText7 = textCompare.ContentsEqual("",true);
if (sameText7 == true) {
more = false;
}
else {
if (seenMarkers.Contains(marker) == true) {
System.out.println("Repeated marker; results are incomplete.");
return;
}
success = seenMarkers.AddInt(marker,1);
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
if (page > 1) {
success = rest.RemoveQueryParam("marker");
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
}
success = rest.AddQueryParam("marker",marker);
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
rest.Disconnect(10);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
System.out.println("TLS reconnection failed.");
return;
}
success = rest.UseConnection(connection,false);
if (success != true) {
System.out.println("Could not prepare the request.");
return;
}
}
}
System.out.println("Directory listing complete.");
}
}