Solved avoiding temporary file in CMD

June 5, 2019 at 09:54:03
Specs: Windows 7, 8GB
I have a specific CMD script in which I'm using a technique as shown below, which uses a temporary file to write a CMD statement to set a variable. This works just fine, but the problem is: I know this is not proper coding as there must be a better way to do this. Writing a temporary file for just setting a variable seems like a bit of overkill, but then again I may be wrong. I'm also thinking that creating such a file is not really a good idea performance wise. This is the second reason for my question; I'm running a script with such code in it for 500.000 subsequent calls .. and it takes around 8 hours.

Note I'm using a non-Windows executable (awk) in this example. It may be replaced by something else, I'm open for any option.

set p_temp_gd=C:\temp\gdtemp.cmd
vol C: | findstr /C:"Volume in drive C is" | awk " { printf \"set myparam=\" $6 } " > %p_temp_gd%
call %p_temp_gd%

See More: avoiding temporary file in CMD

Report •

June 5, 2019 at 13:21:31
✔ Best Answer
All im doing is executing the output of awk right away instead of writing it to a file and then executing the code. Removed output to console, for faster execution aswell.

i have no clue what awk does but try this(save as FILE.bat or FILE.cmd):

@echo off&for /f "tokens=*" %%a in ('vol C: ^| findstr /C:"Volume in drive C is" ^| awk " { printf \"set myparam=\" $6 } "') do %%a

edit: change @echo off to @echo on to keep console output visible

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded

message edited by hidde663

Report •

June 5, 2019 at 18:43:48
Hide's looks right, but I'm still confused. F/e, the output of "vol X:" might look like "Vol in drive X is Pics Video Music". Taking just item 6, on assumption of similar delimitation for awk as for batch, you would get only "Pics", and your output would be "set myparam = Pics" (dbl quotes part of output? or escaped? can't remember my unix off-hand). I'm guessing/assuming you want to run another routine with this output as an argument, but I'm probably wrong in this assumption. Anyway, pursuing my folly to the conclusion, if you want to pass the drive's volume label to a process down the line:
for /f "tokens=5*" %%a in ('vol c:^|find "Volume in drive'") do set myparam="%%a"
echo myparam is: %myparam%
Then you would call the process with %myparam% as an argument, if set globally, or passed as %1 to another batch.

message edited by nbrane

Report •

June 6, 2019 at 01:12:06
You're right; in my initial example I'm only worrying about 1 (space delimited) parameter, but the example itself can cause multiple space delimited parameters. It just happen to be that the volumes I have (because that is what is being queried) all do not have any spaces in them. I'm assuming that when an answer is given (as done by Hidde663 in the second post) it'll be not that hard to be adapted to be used for multiple white spaced parameters.
The command used ("vol") is just an example, I have several other cases each having a different command (because they serve different purposes and different variables) ... but all using those temporary files. And indeed, some have multiple white space separated parameters (and some have not).

Report •
Related Solutions

Ask Question