简体   繁体   中英

How can I write a config file from batch script using vbscript variables in output redirect

In a bash script I write for creating a config file for dosbox, I can define the Current Resolution:

#!/bin/bash
# 

if [ "$Resolucion" = "1152x864" ]; then
    windowresolution=$(echo windowresolution=1024x768)
    scaler=$(echo scaler=2xsai) 
fi

Full script is available here .

In bash I can make

echo '
Line 1
Line 2
Line with Varible 1 fullResolution='"$Resolucion"'
Line with Varible 2 '"$windowresolution"'
Line with Varible 3 '"$output"'
Line with Varible 4 '"$scaler"'
Lines :
@echo off
mount c '"$Ruta_Actual/.$Titulo"'
c:
'"$Ejecutable"'
exit' | tee "$PWD/dosbox.conf" &> /dev/null

Now I need to do the same in a batch script, but I don't know how make batch + VBScript working.

I wrote the following for testing purposes:

@echo off
setlocal enabledelayedexpansion

color A
title BattleChess
set DIR="%CD%"
set PWD=%CD%\Juegos\Inukaze\BattleChess
set TITULO="BattleChess"

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo strComputer = "." >> %SCRIPT%
echo Set objWMIService = GetObject("winmgmts:" _ >> %SCRIPT%
echo    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") >> %SCRIPT%
echo Set colItems = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")  >> %SCRIPT%
cscript /nologo %SCRIPT%

for /F  %%* in (%SCRIPT%) do set RES=%%A
echo %RES% >> %CD%\RES.TXT

The RES.TXT just have a %A in it, but I can get the current resolution with this VBScript:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")

For Each objItem in colItems
        msgbox( "Current Resolution : " & objItem.ScreenWidth & "x" & objItem.ScreenHeight)
Next

However, I don't know how to make a file %CD%\\dosbox.conf with multiples lines and using the "Current Resolution" from VBScript.

The lines of VBScript show me an error.


The Error :

""{impersonationLevel=impersonate}\\"" is not recognized as an internal or external command, program or batch file.

"strComputer" is not recognized as an internal or external command,
 program or batch file.

The system can not find the path specified.

Well i dont know how to export the script .

But the answer :

for /F %%A in (
  'wmic desktopmonitor get ScreenHeight^,ScreenWidth /value ^| find "="'
) do set "%%A"
set "RES=%ScreenWidth%x%ScreenHeight%"

echo %RES%

That solves that for me.

OK, this almost ready, I just need to know how I can make the following from bash script:

# Resolutions 4:3
if [ "$Resolucion" = "640x480" ]; then
        windowresolution=$(echo windowresolution=512x384)
        scaler=$(echo scaler=2xsai)
fi
if [ "$Resolucion" = "800x600" ]; then
        windowresolution=$(echo windowresolution=640x480)
        scaler=$(echo scaler=2xsai)
fi

if [ "$Resolucion" = "1024x768" ]; then
        windowresolution=$(echo windowresolution=800x600)
        scaler=$(echo scaler=2xsai)     
fi
if [ "$Resolucion" = "1152x864" ]; then
        windowresolution=$(echo windowresolution=1024x768)
        scaler=$(echo scaler=2xsai)     
fi

into batch script. I really don't know if the next are right:

REM "Resolutions 4:3"

if %RES% = 800x600
set windowresolution=640x480
set scale=2xsai

I need specify multiple resolutions and windowresolutions in the script for better config file.

echo "lines" >> %CONFIG%
echo "%variable%" >> %CONFIG%

which I need use for each

"if %RES%=Numbers X Numbers"
windowresolution=%WINRES%
scale=something

to export to the config file the follow

Resolution=800x600
windowresolution=640x480
scale=2xsai

I can't help you with the vbs-parts, but:

for /F  %%* in (%SCRIPT%) do set RES=%%A

seems quite wrong. first, you have to use then same for-variable : EITHER %%* OR %%A :

for /F  %%A in (%SCRIPT%) do set RES=%%A

Second: with this syntax you read the file %script% , assingning every line to RES , resulting in %RES% being the last line of your file.

To use the result of the executed script, use:

for /F  %%A in ('%SCRIPT%') do set RES=%%A

Note the single qoutes ( ' ), which tell for to execute the script.

Note: you can also get your screen-parameters with batch:

for /f %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value^|find "="') do set %%i
echo %Screenheight%x%ScreenWidht%

First and foremost: always post the actual error information (error message, error number, the actual line raising the error, …). Since we're not sitting in front of your computer screen you need to tell us what's on it.

With that said, you're probably getting an error, because the & operator has a special meaning in batch files. It separates two commands from each other, so you need to escape it if you want to print a literal & :

C:\>
foo
bar

C:\>
foo & echo bar

You're getting %A in the output file, because the syntax of your for loop is entirely wrong. The loop variable must be defined as %%A , not %%* , and the statement you want to run must be put between single quotes (or backticks, if you set the usebackq option). Also, you must run the script with cscript.exe , because the default interpreter ( wscript.exe ) doesn't write to StdOut , so you'd have no output to process. Change this:

for /F  %%* in (%SCRIPT%) do set RES=%%A

into this:

for /F %%A in ('cscript //NoLogo %SCRIPT%') do set RES=%%A

to make the loop work correctly.

Just fixing the loop won't help much, though, because the VBScript you generate doesn't produce any output in the first place. You can't pass VBScript variables back to the batch script, only printed output ( WScript.Echo or WScript.StdOut.WriteLine ), or perhaps variables in the volatile environment .

However, as @Stephan already pointed out, you don't need the VBScript in the first place, because you can run WMI queries from batch files with the wmic command-line utility:

for /F %%A in (
  'wmic desktopmonitor get ScreenHeight^,ScreenWidth /value ^| find "="'
) do set "%%A"
set "RES=%ScreenHeight%x%ScreenWidth%"

Note that you need to escape both , and | in the subexpression (escape character in batch is ^ ).

To create an output file with multiple lines you have to either echo one line at a time:

set "CONFIG=%CD%\dosbox.conf"

type nul >"%CONFIG%"

echo foo >>"%CONFIG%"
echo windowresolution=%RES% >>"%CONFIG%"
echo bar >>"%CONFIG%"

or escape line breaks like this:

set "CONFIG=%CD%\dosbox.conf"

echo foo ^

windowresolution=%RES% ^

bar >"%CONFIG%"

Writing several lines depending on the value of %RES% can be handled like this:

REM "Resolutions 4:3"

if "%RES%" = "800x600" (
  echo Resolution=%RES% >>"%CONFIG%"
  echo windowresolution=640x480 >>"%CONFIG%"
  echo scale=2xsai >>"%CONFIG%"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM