我正在尝试使用下面的查询来获取文件夹内容,还可以处理错误。但是,我仍然可以使用警告图标获得文字 - " datasource.notfound:file或文件夹:我们找不到文件夹"请参阅下面的M代码:
let
Source = Folder.Files("\serverpathDesktop"),
AlternativeOutput=#table(type table [Name=text,Extension=text,Availability=text], {{"Error", "Error", "Folder not available"}}),
TestForError= try Source,
Output =
if TestForError[HasError] then AlternativeOutput
else Source
in输出
您需要在每次尝试/否则尝试使用table.buffer
对文件夹进行评估。= try Table.Buffer(Folder.Files(path1)) otherwise Folder.Files(path2)
然后,您可以评估多个路径,例如,如果在具有不同登录凭据的不同计算机上使用,即在一台PC上使用userName,但在另一个PC上名称user.name(例如,个人计算机,工作计算机(
= let SourceA = try Folder.Files("C:UsersUsernameOneDriveDocuments"),
SourceB = try Folder.Files("C:UsersUser.nameOneDriveDocuments"),
DynamicSource = try Table.Buffer(SourceA[Value])
otherwise Table.Buffer(SourceB[Value])
in DynamicSource
这可能是一个错误,这解释了为什么try...otherwise
也无法使用。即使我们得到DataSource.NotFound
错误,HasError
字段也正在返回错误。我已经在我们的结尾提交了一个跟踪此问题的项目。
由于我需要,并且可以确认 @alejandro错误仍然发生(DataSource.NotFound
在2023年仍然没有提高HasError
标志(,我不得不结合以前的两个答案才能使其起作用:<</p>
let
mainPath = "\my-distant-srvdistant_dir",
testPath = try Table.Buffer(Folder.Files(mainPath)), // https://stackoverflow.com/a/60146119/5510196
curatedPath = if (testPath)[HasError] = false then mainPath & "dist_file.csv" else "C:templocal_csv.csv", // https://stackoverflow.com/q/48637708/5510196
Source = Csv.Document(File.Contents(curatedPath),[Delimiter=",", Columns=5, Encoding=1252, QuoteStyle=QuoteStyle.None])
in
curatedPath
尝试填充Table.Buffer
时,空虚会增加所需的HasError
。然后通过 if.. then.. else
使用它来策划结果。
感谢您的帮助!