ForFiles is not For

Today I was surprised to find a new batch command. FORFILES. This is essentially a re-worked FOR command, with a slightly different skew.

Whereas FOR is targeted at files or text, FORFILES is exclusively targeted at files. What makes it different?

The advantage I needed is it’s ability to work with filenames that contain parentheses characters, which are “(” and “)”. These cause problems in the FOR command for two reasons. First, FOR itself uses parentheses as part of its syntax, so a close-parentheses will kick it’s filename parser back out to the FOR command processing logic. Second, parentheses are used by CMD script to group commands, so this also interferes with a variety of situations.

I was hoping to get the last-modified-time for a repository synchronization tool. The FOR command can do this, but many of the files in my repository contain parentheses so it was a hard fail.

I was passing in a full absolute path/filename/ext, and struggled to find the right combo of path (/P) / mask (/M) filters. FORFILES was complaining about nonexistent paths. Then I hit upon using a path filter of the full absolute PLUS appending a trailing “\..” to bring the context from the file back down to its containing folder. And that’s close to working!

I still haven’t nailed how to filter the single file I want processed. Using my single file on the filter makes FORFILES complain, while using the filetype returns multiple files. I’m going to give up for now. I’ve tried a few other single files and they seemed to work, so maybe I’ll come back to this later. Here’s the command as I’ll leave it:

forfiles /P "C:\Users\Public\Documents\NAMEPLATES - Honeywell PX940V\Badger\270457(267070) MISSING IMAGE.fmt\.." /M "*.fmt" /C "cmd /c echo @path MOD DATE IS @fdate@ftime"

I was thinking of using FIND or FINDSTR to filter out the single file, but hit a stop with the need for double-quotes needed to contain the search string that contains spaces, backslashes, and parentheses. Plus the need to CALL :subroutine in the main batch script, that might not play nicely with FORFILES’ syntax of using CMD /C as a prefix of everything it does.

Also note that @path is the full abs path\file\ext, @file is file\ext, @fname is file, @ext is extension.

…And I missed a few months of posting. Life changes. Maybe I’ll post more later. I’m considering moving this all elsewhere.

This entry was posted in Cmd scripting, FunTimes, Programming. Bookmark the permalink.

One Response to ForFiles is not For

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.