Welcome!

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

SignUp Now!

Substituting for SET variables

Nov
8
0
This is really elementary, but it's been a long time(*) since I've had to deal with it:

I set a local variable:
set NRoot=N:\LocalFBu\CRoot\

Which I want to use in something like
set QQSV=cd "%NRoot%%@instr[2,,%@full[.]]" ^ copy /u /p . %NRoot% %&

Much to my surprise, apparently %NRoot% isn't evaluated between back-quotes.
But without the back-quotes, of course, it sets QQSV to the first part and then starts executing the copy command.

How?

Thanks,
BG

(*) I'm still using version 12.11 from 2011
 
(Apparently back-quotes (`) don't show up very well (as witness that invisible one between the parens)

set QQSV='cd "%NRoot%%@instr[2,,%@full[.]]" ^ copy /u /p . %NRoot% %&',
with the wrong quotes to make it clear.
 
Backquotes (``) are super strong. I think (?) the only thing expanded inside backquotes is the escape character (the modern one being ^).
 
Yes, that's the problem. I don't think what I'm trying to do with that set statement is unreasonable, but then how to do it?
 
I'm not sure what you're trying to do. You set QQSV and don't use it; what do you want QQSV to be?. So I'm guessing. You should only have to "quote" file/directory names in case they contain special characters. Maybe something like this.

Code:
CD "%[NRoot]%@instr[2,,%@full[.]]" & copy /u /p ...

Note that I'm using the modern command separator (&) and %[NRoot] to make sure TCC knows where the NRoot variable's name ends.
 
I didn't show the whole btm file.

I want to create an alias called sync that will do
cd "%NRoot%%@instr[2,,%@full[.]]" ^ copy /u /p . %NRoot% %&
obviously with %NRoot% expanded appropriately

Then from any directory on Machine A I can sync all the files to Machine B (which has the identical file tree).

[QQSV was a failed attempt at a workaround, thinking it was the alias command that suppressed expansion. But alas it's the back-quotes, which are needed to keep the string as a string so the copy doesn't get executed.]
 
Typically, if you're defining an alias, you want backquotes to prevent things being expanded while you're creating the alias. Use the ALIAS command. Again, I'm using & as the command separator

Code:
alias sync `cd ... & copy ...`

Newer versions of TCC have a built-in SYNC command. You should be able to see its help here.
 
Yes, thanks, I know how to write the alias sync ... command. I don't know how to make my problem clearer.

I want to write (I'm using single quotes (') because the back-quote doesn't show up here):

alias sync 'cd "%NRoot%%@instr[2,,%@full[.]]" ^ copy /u /p . %NRoot% %&'
(A)The back-quotes are necessary because the whole line is to be the alias, NOT just the first part, leaving a copy command doing something.
BUT
(2) The back-quotes prevent expanding %NRoot% to the desired base path (N:\XXX\RootDirectory, not that it matters; it used to be just N:, which didn't raise this difficulty).

This seems to me to be a reasonable thing to ask, though I couldn't find anything relevant in the Help system).
 
Hmmm! The forum uses backquotes to indicate a quote. So when you backquote the word string you see string. I had never realized that.

Generally, one wants variables in an alias to be expanded when the alias in executed. Surrounding backquotes should be removed when you execute the ALIAS command. Anyway, please show me ...

(1) any variables your SYNC alias will depend on
(2) an example of exactly what you want to type when you execute the alias
(3) an example of the fully-expanded command you want executed when you execute what you typed in (2)

Disclaimer: It has been a while since I used version 12.
 
The attached experiment is free standing. There's a contradiction between needing the back-quotes to keep from executing the copy and needing the environment variable to be expanded.

Let's say I'm in my C:\MyWorkFiles\July and I just modified some file, so I want to type
sync
and have all the newer files in C:\MyWorkFiles\July to N:\LocalFBu\CRoot\MyWorkFiles\July on a local backup device.

Please don't make me type out the full resulting command line. The expansion of %NRoot% is obvious.

Maybe asking the question in another direction? Is there another way besides using back-quotes to keep a string of concatenated commands (i.e., the cd and the copy in the example at hand) as a string, NOT executing whatever is after the concatenator (i.e., the copy)
 

Attachments

I've expanded the demonstration btm file to be more explanatory, I hope, between the internal remarks and its output.
 

Attachments

Maybe asking the question in another direction? Is there another way besides using back-quotes to keep a string of concatenated commands (i.e., the cd and the copy in the example at hand) as a string, NOT executing whatever is after the concatenator (i.e., the copy)
Yes. To do that, you must

(1) escape the command separator. For me that would be ^& because ^ is my escape character and & is my command separator
(2) double the %s on all the variables\variable_functions you want expanded when the alias is executed. Any with a single % will be expanded when you create the alias.

For the COPY, . is the source and N:\LocalFBu\CRoot is the destination, right? What is the "%&" at the end?

Do you need to expand %QQSV% to make the copy happen? Why not just use an alias called SYNC?

Does NRoot ever change?

rem what I want: alias sync cd "N:\LocalFBu\CRoot%@instr[2,,%@full[.]]" ^ copy /u /p . N:\LocalFBu\CRoot %&

I'd use that as a starting point.

Code:
ALIAS sync `cd "N:\LocalFBu\CRoot%@instr[2,,%@full[.]]" ^ copy /u /p . N:\LocalFBu\CRoot %&`
 
%& means "all command line parameters", so I can say sync /s and sync up subdirectories as well.

NRoot shouldn't change very often, but esthetics dictates that there SHOULD be some straightforward way of doing this. Apparently not, so I'll manually change N: to N:\LocalFBu\CRoot where necessary.

Far be it from me to follow your escape this and that. Thanks for trying to help.
 
I may not be understanding this discussion, but have you considered defining the alias in a file, where you don't have to worry about expansion of variables, and then loading that file using alias /r? (How neat to learn that back-quotes can be used to designate inline code. Like Vince, I never knew that.) In fact, you could work on it in batch file to get the code right and then turn it into a single line for an alias definition.
 
Back
Top