malxau/yori

DIR command displays statistics with -S switch even with -M switch

aleaksunder opened this issue · 3 comments

Hi!

If DIR command is used with -m switch it displays only filenames, but if we use it in combination with -s switch to process subdirectories than it will display additional stats, which is not needed when we for example piping the results of DIR to another command... C:\temp>dir -s -m
will output:

C:\temp\folder\file
C:\temp\file
C:\temp\folder

     Total Files Listed:
           2 File(s)                  0 bytes
           1 Dir(s)

And by the way can you please give the cue how to use SDIR command to product as simple output as it can, this command is very powerful but I do not understand how to use it's output in CMD for example or how to configure it's output to be something like dir -s -m with only file names

SDIR was really designed for a visual file display, not for scripting. By default it displays names and sizes; you can turn off sizes with -hfs to only display names but the result is still not suitable for scripting.

For scripting, you might want to consider FOR. It supports all of the metadata types from SDIR to capture the set of files that are of interest. For example, recursively find all read only files: for /r /i "fa&R" %i in (*) do echo %i .

The difference in FOR command is that I need to manually specify what kind of file system objects I need: files or folder with -d swith of FOR command. So in basic scenario when I want to rename all objects which mathing exact pattern I need to run FOR command twice... it is appropriate solution but one command is always better =) I use ydir.exe in pure CMD with batch file in this way:

	@ECHO.  Renaming pattern objects...

	@SET "pattern=name1"
	@SET "newname=name3"

	@FOR /F "usebackq tokens=* delims=" %%S IN (

		`""%yori_path%\ydir.exe" -s -m "%pattern%.*" | "%yori_path%\iconv.exe" -i utf8 -e ascii -w"`

	) DO @(

		@"%yori_path%\ymove.exe" -k "%%~dpnxS" "%%~dpS%newname%%%~xS"

		@IF ERRORLEVEL 1 @(

			@ECHO.  [ ERROR ]   ^^!^^!^^!   Can not rename existing object:
			@ECHO.  Path :  %%~dpnxS
			@SET "exit_code=2222"
			@GOTO :exit_label

		)

	)

It's a bit complicated but I do not think you will have troubles to get in to... the problem is that if I will use FOR command first for files and second for folders, that I will have two cycles with exact the same logic: making operation and checking the result of it. And there is huge problem for that if I only need one operation such as renaming, but if code block describing what to do with finded object is huge, for example 10-50 lines, than I will have two such blocks and in situation when I need to change that logic I will have to do that twice again.

Temporary for me I've added a couple of lines in local source checking if -m switch was specified beform output statistics in recursive dir -s...
So that code:

yori/dir/dir.c

Lines 1212 to 1217 in a89220e

if (DirContext.FilesFound == 0 && DirContext.DirsFound == 0) {
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("dir: no matching files found\n"));
return EXIT_FAILURE;
} else if (DirContext.Recursive) {
DirOutputEndOfRecursiveSummary(&DirContext);
}

becomes

    if (DirContext.FilesFound == 0 && DirContext.DirsFound == 0) {
        YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("dir: no matching files found\n"));
        return EXIT_FAILURE;
    } else if (DirContext.Recursive) {
        if (!DirContext.MinimalDisplay) {
            DirOutputEndOfRecursiveSummary(&DirContext);
        }
    }

It's a temporal solution of course since I need it ASAP and I think that maybe there can be better solution with another new switch which suppresses any kind of output of statistics information in any situation with '-s' or without it and with -m or without it.

Thank you!