Using variables in advanced?

I finally got the time to try to create a popup menu for the toolbar to do various copy operations I always did for the same files. I have it working for static drives but the problem is that when I insert a USB flash drive, it can be installed as different drive letters. Right now, this is what I have as instructions:

copy file "D:\My Documents\file.txt" to {dlgfolder} force
go {dlgfolder}

I want to be able to go to the USB drive to verify that the file was copied. Obviously I don't want to get this dialog twice. Is there a way to set the value of it to a variable and then use that variable more than once?

Try switching the commands around - something like:

go {dlgfolder}
copy file "D:\My Documents\file.txt" here force

That sounds good, thanks. So I guess there is no way to use variables in the advanced commands.

There aren't any particularly good ways to use variables, at the moment at least. There are workarounds, though, like calling a VBScript (or whatever) which takes the arguments and then runs some Opus commands via DopusRT (and/or does things by itself).

You can also create advanced commands that use an MS-DOS Batch function (read that as NT Command Script if you use NT/2000/XP/Vista). Such commands allow you to use environmental variables and conditional logic branching inside of a batch script. If you are using Windows 2000/XP/Vista, you can even create Label Functions which can be called.

There are some issues with such commands, but at this point I'm pretty sure Leo or I have already stumbled on all of them and reported them to GPSoftware. I can work around most issues that I'm aware of at this point, but it sometimes things get difficult-to-impossible, depending on what I want to accomplish. Despite the unworkable issues, I have still managed to create several powerful commands using NT Command Scripts.

If you go to the Button & Toolbars forum and download any of the User Commands I've posted, you will have examples to work from. I also have others that I have not published yet.

Try this. It's what you wanted to do in the first place. Don't be intimidated by how long it is, most of it is me typing up comments so you can see and understand exactly what I'm doing at each step of the script.

Copy that code and paste it to a toolbar in Customize mode.

<?xml version="1.0"?> <button display="both" effect="gray"> <guid>{912A7887-668E-455D-A20B-559BDC0D93F9}</guid> <label>Script-Based Copy</label> <tip>Copy named file using NT Command Script variables - Prompt for Target Location</tip> <icon1>2</icon1> <function type="batch"> <instruction>:OpusDirectives</instruction> <instruction> :: Runmode Hide tells Opus to hide the resulting Command Prompt.</instruction> <instruction> :: If you want to see and use the Command Console window, delete</instruction> <instruction> :: that line. (This is useful during debugging.)</instruction> <instruction> ::</instruction> <instruction> :: Opus sometimes still has issues passing quotes with spaces,</instruction> <instruction> :: quotes and environmental variables, and other such things. </instruction> <instruction> :: @nofilenamequoting helps us gets quotes where we want them in</instruction> <instruction> :: our script. This is not always error-free though.</instruction> <instruction/> <instruction> :: NOTE: Opus Directives must always start in the first column!</instruction> <instruction/> <instruction>RunMode Hide</instruction> <instruction>@NoFileNameQuoting</instruction> <instruction> </instruction> <instruction>:Start</instruction> <instruction> :: This command will not </instruction> <instruction> @Echo OFF</instruction> <instruction> Title Directory Opus - Advanced Copy Command</instruction> <instruction> setlocal enableextensions</instruction> <instruction/> <instruction>:SetVars</instruction> <instruction> :: When working with Environmental Variables and Opus, the Windows Set</instruction> <instruction> :: command MUST be protected with a Call command. Otherwise, Opus thinks</instruction> <instruction> :: Set is its own internal Set command. This is only required when Set is</instruction> <instruction> :: the first command on the line of code.</instruction> <instruction> ::</instruction> <instruction> :: In the first command below, I am combining the best of Opus and Windows.</instruction> <instruction> :: The Opus Folder selection dialog will be defaulted to the System Drive.</instruction> <instruction> </instruction> <instruction> Call Set TargetPath={dlgfolder|Enter the path to copy to:|%SystemDrive%}</instruction> <instruction> Call Set FilePath=T:\My Documents\File.txt</instruction> <instruction/> <instruction>:Logic</instruction> <instruction> :: There is no sense trying to copy something that does not exist.</instruction> <instruction/> <instruction> IF NOT Exist %FilePath% GOTO :END</instruction> <instruction/> <instruction>:Execute</instruction> <instruction> :: The command "dopusrt" is a shortcut Opus will replace with the complete</instruction> <instruction> :: path to dopusrt.exe. This is an external interface for calling internal</instruction> <instruction> :: Opus commands as external commands, using the "/CMD" parameter. Opus will</instruction> <instruction> :: run this script as an external program. Here, we use dopusrt to call the</instruction> <instruction> :: Copy and Go commands, while passing our environmental variables to them.</instruction> <instruction> </instruction> <instruction> dopusrt /CMD Copy FORCE WHENEXISTS=prompt FILE="%FilePath%" TO="%TargetPath%"</instruction> <instruction> dopusrt /CMD Go NEWTAB PATH="%TargetPath%"</instruction> <instruction/> <instruction>:END</instruction> </function> </button>

Thanks for the very detailed script. I learned a lot of things. First, I thought that when specifying a batch file, there actually had to be a batch file instead of just specifying the batch commands in the script. And lastly, there is a LOT more I need to learn about DO if I need the functionality as it can do a lot!

For most "DOS" commands this is true, however some of the more powerful ones, such as the FOR iterator, can give unexpected results when directly used in an Opus button. For example, the following command in an NT script file would assign the current month number, day number, and 4 digit year, to variables and then display their values (for date formats in the USA at least).

rem begin script
for /f "tokens=2-4 delims=/ " %%f in ('date /t') do (set MM=%%f& set DD=%%g& set YYYY=%%h)

echo %MM% and %DD% and %YYYY%

pause
rem end script

However when this is used in an Opus button set up as a MS-DOS BATCH function, only the variable names are returned.