Hi there, I have searched online and found the following, unfortunately I think that the method, using wmic, is now depreceated.
https://stackoverflow.com/questions/25532444/get-screen-resolution-as-a-variable-in-cmd
This is one of the suggestions, it would have been very useful as ideally I would like to get either width or height, both if possible, and put into variables. Then if resolution was less than say 1080, warn user and exit.
for /f "tokens=2 delims==" %%i in ('wmic desktopmonitor get screenheight /value ^| find "="') do set height=%%i
echo %height%
Thanks in advance for any help.
+ Reply to Thread
Results 1 to 30 of 48
-
-
code taken/copied/stolen from
@echo off
setlocal
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"
if version lss 62 (
:: set " wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value "
for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenwidth /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
)
for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenheight /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
)
) else (
::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,Cur rentHorizontalResolution /format:value
for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentHorizontalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
)
for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
)
)
echo Resolution %x%x%y%
endlocalLast edited by jack_666; 19th Oct 2022 at 16:27. Reason: site keeps insisting that there should be smileys in the code.
-
Hi Jack, saw your post a little while ago, thank you so much. So the wmic item is still in use. Anyway Initially when I tested the above I was getting the very same as what I got with the code I posted, just nothing. So if I understand how it works the version has to be established first, then jump to the relevant section.
On the Win 10 laptop that i'm using its version 100. Not sure what the "version" is actually referring to ?
The good news is that if I use this piece then I get 1920 x 1080.
REM This section works ok ... Version = 100
::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,Cur rentHorizontalResolution /format:value
for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentHorizontalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
)
for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
)
echo [%x%] x [%Y%]
pause
exit
This also works, to get the "version" ...
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"
This just does nothing ...
if version lss 62 (
:: set " wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value "
for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenwidth /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
)
Of course, because the version is NOT lss 62. DUH
But I do have a problem with the code, because as it is if I run it as is it does nothing, it never goes to the second section, which would work if it got there.Last edited by JN-; 19th Oct 2022 at 17:13.
-
Got it !
:: set " wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value "
if version lss 62 (
The REMarks :: has to be moved above the if version lss 62 ( then it works.
Thanks again Jack. Is the <62 >62 a WIN 10 version ?
I have an old Win 7 PC, so obviously I would like to prove the lss 62 part. -
Maybe worth mentioning. I am using a 1920 x 1080 FHD laptop screen. When I selected a lower resolution than this using Windows display settings the code still returns FHD.
It made no difference doing a re-boot. So the code returned values seems to be the screens hardware resolution, not the actual in use resolution. -
Some of the tools from nirsoft (which includes some CLI and some GUI apps) might help.
Scott -
Thanks. My main reason for this is simply to add it into some small mainly ffmpeg based batch script video related utils that I have put together to make sure that users are not attempting to run the utils on less than 1920 x 1080. If less than FHD then the layout wouldn't look good at all.
Granted it's unlikely anyone is using less than FHD screens nowadays.
I will use the script supplied by JACK, it's a big step up from not having any. Need to retrofit now.Last edited by JN-; 19th Oct 2022 at 20:43.
-
EDITED!
On Windows 10 21H2. Cheers.Code:powershell.exe Get-WmiObject win32_videocontroller | find "CurrentHorizontalResolution" > temp1.txt powershell.exe Get-WmiObject win32_videocontroller | find "CurrentVerticalResolution" >> temp1.txt for /f "tokens=1-2 delims=^:^ " %%a in (temp1.txt) do echo %%a %%b pause
Last edited by pcspeak; 20th Oct 2022 at 02:58. Reason: Changed code.
-
Thank you pcspeak, that works very nicely.
Again, worth mentioning, that when the screen resolution is changed on my FHD laptop to say a lower resolution, the Powershell code still reports it as 1920 x 1080. Did a re-boot also. -
Expanding on my previous post. Because I can't help myself.
Code:@echo off powershell.exe Get-WmiObject win32_videocontroller | find "CurrentHorizontalResolution" > temp1.txt powershell.exe Get-WmiObject win32_videocontroller | find "CurrentVerticalResolution" >> temp1.txt for /f "tokens=1-2 delims=^:^ " %%a in (temp1.txt) do set %%a=%%b if %CurrentVerticalResolution% geq 1080 goto :process echo Resolution is too low. Press any key to exit. &pause>nul exit :process echo Yay! this is where it all happens. pause
-
A slight revision that asks the user.
Code:@echo off powershell.exe Get-WmiObject win32_videocontroller | find "CurrentHorizontalResolution" > temp1.txt powershell.exe Get-WmiObject win32_videocontroller | find "CurrentVerticalResolution" >> temp1.txt for /f "tokens=1-2 delims=^:^ " %%a in (temp1.txt) do set %%a=%%b if %CurrentVerticalResolution% geq 1080 goto :process echo Resolution is very low. set /p DoIt=Do you wish to proceed? [y/N] if /i %DoIt% neq y exit :process echo Yay! this is where it all happens. pause
-
Hi PCSPEAK, thank you so much for that, very nice.
Have you tried it on your own monitor, and if so did you see that it only appears to check the monitors in place, fixed size. I found that if say the user reduces the screen resolution then the code doesn't detect it, it still returns the monitors fixed resolution. See my previous comments about this. -
Hi.
You're right in that if I right click on the Windows Desktop and change the resolution Powershell only gives the original resolution (default settings?). Nothing changes.
I tried a cold boot between adjustments. There was no change. Still 1920x1080 every time.
This is the result when I used the NVIDIA app to change the resolution and then ran my batch file. I didn't need to do any reboots.
CurrentHorizontalResolution=1280
CurrentVerticalResolution=720
Resolution is very low.
Do you wish to proceed? [y/N]
CurrentHorizontalResolution=720
CurrentVerticalResolution=576
Resolution is very low.
Do you wish to proceed? [y/N]
And back to my default.
CurrentHorizontalResolution=1920
CurrentVerticalResolution=1080
Yay! this is where it all happens.
Press any key to continue . . .
Cheers. -
Hi PCSPEAK. Thanks for doing that, I didn’t think of using the Nvidia app to change the screen resolution.
The problem remains though that if the user uses the Desktop method then the code never sees that it’s running in a lower resolution.
There may be no way to know. If that is the case it’s still better than no code at all. Also its unlikely that a user will run a screen at less than FHD. -
I found a way. I've no idea if this will work for you or how to do it via a p.s. script or batch file.
right click on the Windows Desktop / Display settings / Advanced display settings / Display adapter properties for Display 1 / List All Modes
And then select you're poison. OK you're way back out. PHEW! -
Well, I admire your application ! I tried that and the code does then see the lower resolution set in that way. But the most common way a user will choose to set a lower resolution isn't the Nvidia app or the "Advanced" settings as you have demonstrated, but just clicking on the desktop and selecting the Display settings, that's what I normally use, which is not seen by the code.
To be clear, I don't want to set/change the screen resolution in the batch file, I just want to know what it is. And if it's set/changed by a user using what is probably the most common method then the code doesn't pick it up. Thank you all for the code snippets, what I have now is still a lot more than when I started here. -
It looks like wmic is on its last legs … https://superuser.com/questions/1546599/has-wmic-been-un-deprecated
Maybe a better long term approach is to use the Powershell script.
@echo off
cls
setlocal EnableDelayedExpansion
CALL :GET-SCREEN-SIZE
echo.
echo %_SCREEN_WIDTH%
echo %_SCREEN_HEIGHT%
echo.
echo Is there a problem with the screen size ? ... %_SCR_RES_PROBLEM%
echo.
pause
EXIT
:GET-SCREEN-SIZE
powershell.exe Get-WmiObject win32_videocontroller | find "CurrentHorizontalResolution">screenwidth.txt
SET /p _SCREEN_WIDTH=<"screenwidth.txt"
powershell.exe Get-WmiObject win32_videocontroller | find "CurrentVerticalResolution">screenheight.txt
SET /p _SCREEN_HEIGHT=<"screenheight.txt"
if exist screenwidth.txt del screenwidth.txt
if exist screenheight.txt del screenheight.txt
SET /A _SCREEN_WIDTH=%_SCREEN_WIDTH:~31,4%
SET /A _SCREEN_HEIGHT=%_SCREEN_HEIGHT:~31,4%
SET _SCR_RES_PROBLEM=NO
IF %_SCREEN_WIDTH% LSS 1920 SET _SCR_RES_PROBLEM=YES
IF %_SCREEN_HEIGHT% LSS 1080 SET _SCR_RES_PROBLEM=YES
EXIT /BLast edited by JN-; 21st Oct 2022 at 09:21.
-
Hi pcspeak. FOR commands are not my strong point, to put it mildly, I guess thats why I'm posting in the Newbie section
.
for /f "tokens=1-2 delims=^:^ " %%a in (temp1.txt) do set %%a=%%b
The above FOR is very clever, I don't fully understand how it works.
Part of it removes all non numbers, although it can leave the equals char =.
Is that the ^:^ part ?
#1 How can it be made to also remove the = char.
#2 How to expand it to say handle an output file temp1.txt that has 3 lines in it and put the 3 resultant lines also into variables.
3# How to use it to handle only 1 line and therefore 1 var.
Thanks.
Ok I think I have #1 nailed, using this ^:=^Last edited by JN-; 21st Oct 2022 at 10:26.
-
I managed this ...
@echo off
cls
setlocal EnableDelayedExpansion
powershell.exe Get-WmiObject win32_videocontroller | find "CurrentHorizontalResolution">temp1.txt
powershell.exe Get-WmiObject win32_videocontroller | find "CurrentVerticalResolution">>temp1.txt
echo Third-Line =0123456789=>>temp1.txt
SET /A _COUNTER=0
for /f "tokens=2 delims=^:=^ " %%a in (temp1.txt) do (
SET /A _COUNTER+=1
if !_COUNTER! equ 1 SET _SCREEN_WIDTH=%%a
if !_COUNTER! equ 2 SET _SCREEN_HEIGHT=%%a
if !_COUNTER! equ 3 SET _THIRD_LINE=%%a
)
echo Width [%_SCREEN_WIDTH%]
echo Height [%_SCREEN_HEIGHT%]
echo Third line [%_THIRD_LINE%]
echo.
pause -
The caret symbol (^) signifies the following character is a literal character, not part of a math or script formula.
If I am writing a For-In-Do command line and 'funny' characters are involved I tend to slightly overdo the use of the ^. I figure it can't hurt. The ^ before the space character is not needed, but for consistency I put it in.Code:for /f "tokens=1-2 delims=^:^ ^=" %%a in (temp1.txt) do set %%a=%%b
Code:"C:\Program Files\MKVToolNix\mkvmerge.exe" --output ^"D:\TV\My Favourite-New.mkv^" ^"^(^" ^"D:\TV\My Favourite.mkv^" ^"^)^" --track-order 0:0,0:1
This can be boiled down to:Code:"C:\Program Files\MKVToolNix\mkvmerge.exe" --output "D:\TV\My Favourite-New.mkv" "D:\TV\My Favourite.mkv" --track-order 0:0,0:1
Basically, if you need a character to be interpreted literally, insert a ^.
I hope I explained it a little, somewhat, maybe? A teacher, I'm not.
Cheers. -
try this .... another approach
Code:@echo off powershell.exe Get-WmiObject win32_videocontroller | awk " BEGIN{FS=OFS=\":\"} /Resolution/{ print $2}" > temp1.txt sed -n "1,1p" temp1.txt > h_temp1.txt set /p h_res=< h_temp1.txt set /a h_res = %h_res% + 0 echo Horizontal Resolution : %h_res% sed -n "2,2p" temp1.txt > v_temp1.txt set /p v_res=< v_temp1.txt set /a v_res = %v_res% + 0 echo Vertical Resolution : %v_res% del temp1.txt, h_temp1.txt, v_temp1.txt
-
Hi Jack, thanks for taking the time, and pcspeak, to do the Powershell bits. I see that its much more powerful than cmd batch files, but I make up my small utils using batch.
I dropped in the powershell piece into one of my utils, ConformFR this morning, with fingers crossed that I wouldn’t have the same problem, I had a few months ago, unfortunately I had.
I had previously required a division piece, months back, and added in a PS snippet to do that, acquired from inet but I discovered for the 1st time a major problem, anyway I had to discard the PS piece.
Division in batch is problematic, but the PS code was so much simpler,
Sorry for the ramble, i’m nearly there D:, basically what I do is I use obfuscation on the batch files, then use a batch file compiler to convert to .exe.
What I found was that when PS is included in the batch file that the screen layout, font size etc changes for the worse. Note that without obfuscation there is of course no problem using PS.
So I can’t use wmic method because it will at some future date be depreceated and I can’t use PS because it messes up the display, once I use obfuscation.
I did leave a message re: the issue, for the author of the obfuscation but never received a replyLast edited by JN-; 22nd Oct 2022 at 14:54.
-
This is a slightly different method. Perhaps obfuscation will work with it.
Maybe using double quotes around powershell.exe AND a different 'Get' will help.
This still only returns the system default 'Display settings'. In my case; 1920x1080
Code:@echo off "powershell.exe" (Get-CimInstance -ClassName Win32_VideoController -Property VideoModeDescription).VideoModeDescription >temp1.txt for /f "tokens=1-2 delims=^x^ " %%a in (temp1.txt) do set width=%%a&set height=%%b echo Width=%width% echo Height=%height% del /q temp1.txt pause
-
Thanks pcspeak, i'll try that shortly and get back.
These are the notes that I left to myself RE: the changes that happen when using obfuscation in conjunction with Powershell.
Basically the font size gets changed from Size = 18 Font name = Consolas to Size = 8 x 12 Raster Fonts.
Issue with cmd window, font size and type change to Font.
Tabs
----
Below is whats in use normally.
------------------------------
Options .... Cursor Size = Medium
Font ....... Size = 18 Font name = Consolas
Terminal ... Cursor Shape = Use Legacy Style
Below is what things get changed to when using the Obfuscate util that has a Powershell call.
--------------------------------------------------------------------------------------------
Options .... Cursor Size = Medium ..... Stays the same
Font ....... Size = 8 x 12 Raster Fonts ..... Changed
Terminal ... Cursor Shape = Use Legacy Style ..... Stays the same
-------------------------------------------------------------------------------------------- -
No, same issue as above, cmd window gets changed from Consolas size 18 to Raster Font size 8 x 12.
If you are ever curious heres the link ... http://www.dostips.com/forum/viewtopic.php?f=3&t=7990&start=15#p53278
by dbenham.Last edited by JN-; 22nd Oct 2022 at 18:55.
-
Of course, the issue can be fixed by clicking on the Command windows menu, selecting properties and changing back to Consolas with font size 18. If I could do that from within the batch file, it would be a workaround, but I was never able to find a way to do that.
The cmd window stays ok until the PowerShell section is processed within the batch file, then its font and style gets changed.
I accept the limitation as is, it's not the end of the world.
On my Win 10 laptop at location C:\Windows\System32\wbem\WMIC.exe
wmic:root\cli>/? gives WMIC is deprecated in redLast edited by JN-; 22nd Oct 2022 at 20:03.
-
Of course, the issue can be fixed by clicking on the Command windows menu, selecting properties and changing back to Consolas with font size 18. If I could do that from within the batch file, it would be a workaround, but I was never able to find a way to do that.
try exiftool (https://exiftool.org/) in your batch script -
I have used exiftool in one of my utils, get Metadata, i’m not aware of how it can be used to reset to the desired cmd windows menu. Do you know if that is the case that it can be used to reset, or are you just throwing it out there ?
-
This is the contents of x1.bat:
Code:@echo off "powershell.exe" (Get-CimInstance -ClassName Win32_VideoController -Property VideoModeDescription).VideoModeDescription >temp1.txt for /f "tokens=1-2 delims=^x^ " %%a in (temp1.txt) do set width=%%a&set height=%%b echo Width=%width% echo Height=%height% pause
Code:D:\a>ObfuscateBatch.bat x1.bat
Code:D:\a>x1_obfuscated.bat Width=1920 Height=1080 Press any key to continue . . .
The output from the obfuscated batch file was as expected.
Double clicking on 'x1_obfuscated.bat' from inside Windows File Manager gave me the same result.
The font name and size did NOT change.
I can't find a problem, maybe I need to obfuscate a larger batch file.
FYI.
One of the first things I do when I install/upgrade Windows is run all the following 'cmd.exe' prompts (as Administrator) because I want to change the default fonts.
Your locations for cmd.exe may/will be different. I'm running Windows 10 21H2 x64.
Code:"C:\Windows\System32\cmd.exe" "C:\Windows\SysWOW64\cmd.exe" "C:\Windows\WinSxS\amd64_microsoft-windows-commandprompt_31bf3856ad364e35_10.0.19041.746_none_69061189792bce34\cmd.exe" "C:\Windows\WinSxS\wow64_microsoft-windows-commandprompt_31bf3856ad364e35_10.0.19041.746_none_735abbdbad8c902f\cmd.exe"
This makes the default font stick as 'Lucida Console' for any scripts I run, from wherever.
I use a 24" monitor and my eyesight could be better.
Cheers.Last edited by pcspeak; 23rd Oct 2022 at 15:20.
Similar Threads
-
batch that verify if all the video files parsed have the same resolution
By marcorocchini in forum Newbie / General discussionsReplies: 7Last Post: 20th Oct 2022, 18:17 -
Batch edit SSA subs style whilst also resampling resolution
By randombuddy in forum SubtitleReplies: 2Last Post: 22nd Mar 2021, 14:52 -
PotPlayer copy either current subtitle timestamps OR current audio clip
By Sakusuke in forum Software PlayingReplies: 0Last Post: 29th Aug 2020, 12:35 -
MeGui - How to get path/name of current file in the queue dynamically ?
By untymage in forum Video ConversionReplies: 5Last Post: 16th May 2020, 08:51 -
Current PAL/NTSC DVD Conversion Guide? Current Guides are outdated
By isj209h23h in forum User guidesReplies: 3Last Post: 20th Jul 2018, 21:53