Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

FOR command: /Q option to add quotes to the expanded variable.

Nov
339
7
I use a lot the FOR command in the TCC command line to process sets of files (to check on them, move them around, etc.). A quickly typed command does well and good... until a filename containing spaces shows up, which produces wonky results. For example:

1705158784840.png



Sometimes, it's an unexpected result (as above), sometimes is a "file not found" error per offending file, et cetera. Yes, it's a simple matter of placing quotes around each occurence of the variable name ("%A", in the example above) to avoid the problem, and yet... the user can forget to add them.

I think it'd be quite convenient to add a /Q option to the FOR command, where its presence indicates that each occurrence of the variable is to be enclosed in quotes. In my example, the command would internally become:

for %a in (201*) echo %@cksum["%A"] "%a"

and processed as such.
 
Hmm, but the second quotes are not necessary resp. not desired.

for %a in (201*) echo %@cksum["%A"] %a

would be enough, but I don't know if this would be easy to parse.
 
How about using the @QUOTE function instead?

Returns a double quoted argument if it contains any whitespace characters.

If it does not contain any whitespace characters,
then it is not quoted.

Joe
 
I don't recall ever using the FOR command directly in a command line (cmd or tcc). In a batch file, yes, but not in the command line. For MOVE or DEL, I generally use the "/p" option, and file names with spaces are properly handled by TCC.

2024-01-13_14-32-12.png
 
Try this;
Code:
for %a in (201*) echo %@cksum[%@quote[%A]]  %@quote[%a]

Joe
 
Also a variant, yes. But then it's even quicker (for typing) to use the quotes ALWAYS as in @mfarah example.

The "only" advantage (or not? it's a point of viewing) with your solution is that - as you described - it's only quoted if needed.

However, thanks for that example. Could be useful for me too at certain point.
 
@ohenryx

But why the /P? It handles it without /P properly too.

Of course if you really need a prompt to check it before, then it's clear. But for automated processing you don't need it. Additionally if you have really many files which give many findings and you have to answer the prompt for each ...

However: thank you for that variant too.
 
Last edited:
I am aware of what the /P option does. I like to use it just to be on the safe side. My point was that I don't need to use a FOR loop on the command line to implement a MOVE or DEL.

There are probably some circumstances under which the FOR loop might be preferable, but offhand I can't think of one.
 

Similar threads

Back
Top