Author Topic: Help with a command that reads files names  (Read 2186 times)

Chaos Introvert

  • Newbie
  • *
  • Posts: 7
Help with a command that reads files names
« on: November 20, 2020, 11:06:08 PM »
Hi everyone.
I've been working on a command to read lists of filenames from the disk using "cmd.exe /c dir <etc>" to output to a text variable.

Code: [Select]
Set text [~VAR_TXT_FILE_PATH] to 'C:\Some\Directory\Structure'
Run application 'C:\Windows\System32\cmd.exe' -with parameters '/c dir *.* /a-d /b /s' - wait until it completes
Set text [~VAR_TXT_FILE_LIST] to [~VAR_TXT_FILE_LIST] (Trim)
Write [Blue] 'Files: {TXT:~VAR_TXT_FILE_LIST}' to log

In the above, ~VAR_TXT_FILE_PATH is used as the working directory for the cmd.exe invocation, and ~VAR_TXT_FILE_LIST captures output from STDOUT (with "Wait until the launched application exists before continuing" selected).

This works fine for most folders, but for some - I think ones with a lot of files - the cmd.exe window gets 'stuck' open, and the command doesn't progress. If I close the cmd.exe window manually, the output gets picked up and things continue, but it should close automatically with the "/c" parameter.

Can anyone think of a way to make this work consistently?

I have a workaround using a temporary text file to capture the output, and then reading that file into the text variable instead of reading STDOUT directly, but I'd prefer to avoid using a file if I can.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Help with a command that reads files names
« Reply #1 on: November 21, 2020, 03:35:54 AM »
While using cmd.exe in this manner can be useful for a number of things, in this case I'd use an inline function instead, as the API methods for listing files are available, and can get that data more directly.

E.G. this should duplicate the parameters of your current "dir" command by default:
Code: [Select]
using System;
using System.IO;

public class VAInline
{
public void main()
{
string path = VA.GetText("~~targetPath");

if (!Directory.Exists(path))
{
VA.WriteToLog("'" + path + "' is not a valid directory");
return;
}

string filter = VA.GetText("~~targetFilter") ?? "*";

SearchOption includeSubDirectories = SearchOption.AllDirectories;
if (VA.GetBoolean("~~excludeSubDirectories") == true)
{
includeSubDirectories = SearchOption.TopDirectoryOnly;
}

string[] files = Directory.GetFiles(path, filter, includeSubDirectories);

VA.SetText("~~fileList", String.Join(Environment.NewLine, files));
}
}

Usage example:
Code: [Select]
Set text [~~targetPath] to 'C:\Some\Directory\Structure'
Inline C# Function: Create file list, wait until execution finishes
Write [Blue] '{TXT:~~fileList}' to log

It optionally allows you to specificy a filter by setting the "~~targetFilter" text variable, but will otherwise list all files (and files only, not directories).

Searching subdirectories can be disabled by setting the "~~excludeSubDirectories" Boolean variable to true.

Chaos Introvert

  • Newbie
  • *
  • Posts: 7
Re: Help with a command that reads files names
« Reply #2 on: November 21, 2020, 10:20:17 AM »
Aha, inline functions to the rescue.
Thanks Pfeil!
I haven't played around with inline functions as yet, but I'll give this a shot.