1.

Solve : How to copy a file from source to all destination directory,all its sub dirs?

Answer»

Quote from: llmeyer1000 on June 15, 2008, 08:38:05 AM

Here is what I don't understand:
NORMALLY the <Enter> (carriage return/line feed) tells DOS to execute the command as written on that line. It appears to me that in Reply #7 you have added 2 extra <Enter>'s in the middle of what should be one line.

Thus the QUESTION(s):
1. Did you use the <Enter> to break up a single line?
(or was some other charactor that I can't see here?)
2. When is it OK to break the line this way for appearance sake.?

1. Yes. I used Enter.
2.

When you use (
brackets like this
)

Not just in FOR loops

IF "%var%"=="apple" (
ECHO it's an apple
call fruit.bat
)

Try typing ( at the command prompt.

Code: [Select]C:\>(
More? echo CAT
More? echo dog
More? echo horse
More? )
cat
dog
horse

C:\>



PS We are not talking about "DOS" here, but NT/Win2k/XP/Vista command language.
Is it possible to create sort of a batch like that?
'Course inserting a 50 line complicated batch script won't work lik this but what does?Quote from: Dias de verano on June 15, 2008, 08:45:14 AM
Try typing ( at the command prompt.

OK! After a bit of experimenting, I managed to duplicate your example as follows.

Code: [Select]C:\>(
More? echo test1
More? echo test2
More? echo test3
More? )
test1
test2
test3

C:\>
That's pretty cool, and it goes a very long way in explaining something that has long confused me about many of the posts on this site.

But it still appears to me that each section is treated as a separate command line. In this case 3 separate echo statements.

It appears that it works on the "for" command line, if used after the "do" , but would not work when I tried it on the "echo" command:

Code: [Select]C:\>(
More? echo
More? test1
More? )
ECHO is on.
'test1' is not recognized as an internal or external command,
operable program or batch file.

C:\>
Can I assume from this that there are only certain commands(like "for in do") that can be split this way? If so, where in the help does CMD explain this? I tried "( /?" to no avail.


Quote from: Dias de verano on June 15, 2008, 08:45:14 AM
PS We are not talking about "DOS" here, but NT/Win2k/XP/Vista command language.

I did say DOS, but I knew that the for command was way more limited back then than now. I should have said CMD instead to clarify.
OK! I have been going over your earlier posts (Reply #13)

Quote from: Dias de verano on June 15, 2008, 01:14:39 AM
NT/Win2K/XP/Vista batch language allows you to use brackets to enclose groups of commands so you can do this ...

and I think I can answer my last question. You can use the brackets to enclose any number of commands in a "for in do" statement, but they must be complete command lines, not broken up as I tried in my last example. Duh! I may be slow, but I think I will get it eventually.

Thanks again for explaining.

EDIT: I see now that in Reply #15, you also used the brackets in an "if then" command line:

Quote from: Dias de verano on June 15, 2008, 08:45:14 AM
Not just in FOR loops

Code: [Select]IF "%var%"=="apple" (
echo it's an apple
call fruit.bat
)

I have always thought of an "if then" or "for in do" as a single command, that should all be on one line. Apparently, the "then" or "do" parts are considered as separate commands and thus may appear on the same, or separate lines.

Is that correct, or am I still way out in left field?

Thanks! Quote from: Schop on June 15, 2008, 09:30:56 AM
Is it possible to create sort of a batch like that?
'Course inserting a 50 line complicated batch script won't work lik this but what does?

This much even I can answer.

Yes, most of the code Dias posted was intended for use in a batch file.

For example this code found in Reply #13:
Quote from: Dias de verano on June 15, 2008, 01:14:39 AM
Code: [Select]FOR %%A in (cat dog horse) do echo %%A

will work only in a batch file because of the use of the double percent signs. At the command line, you would need to use single percent signs, as in this code:

Code: [Select]@echo off&FOR %A in (cat dog horse) do echo %A

Quote from: llmeyer1000 on June 15, 2008, 10:08:49 AM
and I think I can answer my last question. You can use the brackets to enclose any number of commands in a "for in do" statement, but they must be complete command lines, not broken up as I tried in my last example.

Exactly. I was just about to post more or less exactly what you wrote.

Quote
Duh! I may be slow, but I think I will get it eventually.

You are not slow at all, believe me!

Thanks.

But sometimes, it is very difficult to fully grasp "new" concepts. (New to me, although not new to many of you!)

I was in the process of editing the post you QUOTED (Reply #18) while you posted the above. In the edit I am requesting a bit more clarification, if you would be so kind.

Thanks again. (With your expertise, maybe my questions and your answers will help a few others, who are weak in the "for-in-do" area like me. )Quote from: llmeyer1000 on June 15, 2008, 10:08:49 AM

I have always thought of an "if then" or "for in do" as a single command, that should all be on one line. Apparently, the "then" or "do" parts are considered as separate commands and thus may appear on the same, or separate lines.

Is that correct, or am I still way out in left field?


That is correct. They can be on separate lines if you use brackets as I have shown. In essence, brackets (or "parentheses" in N. America) can be used to extend statements over multiple lines. The commands contained within a pair of parentheses are treated like a single command. Such a construct is referred to as a "parenthetical expression". This is one of the major differences between MS-DOS/Win9x (which does not have them) and NT/Win2k/XP/Vista command languages.

They also serve to keep commands that need grouping, together.

Services.txt exists. Service.txt does not.

These don't work...

Code: [Select]C:\>if exist services.txt echo yes else echo no
yes else echo no
Code: [Select]C:\>if exist service.txt echo yes else echo no


But these do...

Code: [Select]C:\>if exist services.txt (echo yes) else (echo no)
yes
Code: [Select]C:\>if exist service.txt (echo yes) else (echo no)
no
These are equivalent in a batch...

Code: [Select]if exist services.txt (echo yes) else (echo no)
Code: [Select]if exist services.txt (
echo yes
[...]
) else (
echo no
[...]
)
One slight gotcha is that variables in parenthetical expressions are evaluated before the expression is executed, so sometimes you need to use delayed expansion, but I think you need not worry about that until you encounter it naturally.


Discussion

No Comment Found