Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need some help with batch file coding
New Posts  All Forums:Forum Nav:

Need some help with batch file coding - Page 2

post #11 of 17
Thread Starter 
Quote:
Originally Posted by ALUCARDVPR View Post
Can you post your full code?

There are some batch commands that don’t work at all or work differently.
Code:
:admincheck
OPENFILES > nul
cls
IF %ERRORLEVEL%==0 (goto :start)
IF %ERRORLEVEL%==1 (goto :notadmin)
ECHO Fatal error: OPENFILES ERRORLEVEL returned value not "0" nor "1"
goto :admincheck



REM FIND CURRENT STATUS
REM --------------------------------------------------------------------------

:start
reg query HKLM\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E968-E325-11CE-BFC1-08002BE10318}\\0000 /v %REGKEYNAME% > nul
if not ErrorLevel 1 (
  goto :statuscontinue
) else (
  goto :statuserror
) 
:statuscontinue
FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E968-E325-11CE-BFC1-08002BE10318}\\0000" /v %REGKEYNAME%') DO SET VALUE=%%B
IF "%VALUE%"=="0x0" (goto :disabled)
IF "%VALUE%"=="0x1" (goto :enabled)

.........

goto :menustart

.........

:notadmin
cls
ECHO You are not running this program as an administrator.
ECHO This utility makes registry modifications that will not complete successfully unless given elevated administrator priviledges.
ECHO Please re-run this utility as an administrator by right-clicking the utility and clicking on "Run as administrator"
ECHO.
PING 1.1.1.1 -n 1 -w 3000 >NUL
:midnotadmin
ECHO Press enter to exit the utility.
PING 1.1.1.1 -n 1 -w 500 >NUL
set /p EXIT=...Or if you want to run the utility anyways, type "continue" and press enter.  
if "%EXIT%" == "" goto :7
if /i "%EXIT%" == "continue" goto :start
ECHO.
ECHO Command not recognized.
PING 1.1.1.1 -n 1 -w 500 >NUL
goto :midnotadmin
I should note that even if I run the converted exe file, I can still make registry modifications even though OPENFILES returned ERRORLEVEL=1.

If you need the entire 700+ line batch I can post that too
Edited by kiwiasian - 8/11/11 at 2:07pm
post #12 of 17
Quote:
Originally Posted by kiwiasian View Post
Code:
IF %ERRORLEVEL%==0 (goto :start)
IF %ERRORLEVEL%==1 (goto :notadmin)

--------------------------------------------

if not ErrorLevel 1 (
  goto :statuscontinue
) else (
  goto :statuserror
)
....I should note that even if I run the converted exe file, I can still make registry modifications even though OPENFILES returned ERRORLEVEL=1

This may sometimes fail, since some executables return negative numbers for errorlevels. Try this instead to check for non-zero return codes...

Code:
IF %ERRORLEVEL% NEQ 0 (
  goto :notadmin
) else (
  goto :start
) 

--------------------------------------------

IF %ERRORLEVEL% NEQ 0 (
  goto :statuserror
) else (
  goto :statuscontinue
)

Edited by ALUCARDVPR - 8/11/11 at 7:25pm
post #13 of 17
Thread Starter 
Quote:
Originally Posted by ALUCARDVPR View Post
This may sometimes fail, since some executables return negative numbers for errorlevels. Try this instead to check for non-zero return codes...

Code:
IF %ERRORLEVEL% NEQ 0 (
  goto :notadmin
) else (
  goto :start
) 

--------------------------------------------

IF %ERRORLEVEL% NEQ 0 (
  goto :statuserror
) else (
  goto :statuscontinue
)
Just tried that. It worked fine in batch file, but the same thing happened when I converted it to an exe.

This is what I originally had before:

Code:
IF %ERRORLEVEL%==0 (goto :start) ELSE (goto :notadmin)
To eliminate the negative possibility. It still did not work.


SO: What I have done is do a quick ECHO to see what %ERRORLEVEL% is and I was returned with the following:

(in batch file)


(converted to exe)


This is the code I used for what you saw above:

Code:
@ECHO OFF
OPENFILES > nul
ECHO Error level is %ERRORLEVEL%
pause
My guess is the OPENFILES command does not like a 64 bit working environment. I will try a different command that requires admin priviledges and see what happens.

EDIT: My guess was right. I just tried to do REG ADD (key) instead of OPENFILES and it returned ERRORLEVEL=0 in both batch and exe. So now my question is: what command requires admin priviledges, but is not "destructive?" For example REG ADD will add a reg key but OPENFILES by itself is completely nonintrusive and does not do anything.
Edited by kiwiasian - 8/11/11 at 9:58pm
post #14 of 17
Right click your .exe and select 'Run as administrator'?

You could also look into REG QUERY, but it doesn't check for admin like OPENFILES.

And from what I understand, your right, OPENFILES is 32-bit executable (Openfiles.exe).
Edited by ALUCARDVPR - 8/12/11 at 11:17pm
post #15 of 17
Thread Starter 
Quote:
Originally Posted by ALUCARDVPR View Post
Right click your .exe and select 'Run as administrator'?

You could also look into REG QUERY, but it doesn't check for admin like OPENFILES.

And from what I understand, your right, OPENFILES is 32-bit executable (Openfiles.exe).
I found that if I try to mkdir in %PROGRAMFILES% it requires admin privileges, so my script works perfectly with that

Here is the code, a little ghetto but you know

Code:
mkdir "%PROGRAMFILES%\\Tempdir" > nul
IF %ERRORLEVEL%==0 (goto :removedir) ELSE (goto :notadmin)
:removedir
rmdir "%PROGRAMFILES%\\Tempdir" > nul
goto :start
post #16 of 17
Hey whatever works
post #17 of 17
Quote:
Originally Posted by Razinhail View Post
Ok, my above post was a little bit off. You only need the @ before "echo" if it's a "@echo on" or an "@echo off" command. For the rest of the time you use echo, "echo" is all you need, followed by a space, then what you want on the screen. Sorry about that. My batch file skills have rusted more than I thought!

I tested out the method recommended by ALUCARDVPR, and on my Windows 7 machine, it works, but it creates a file called "Null". You can avoid this by adding this

pause > null
del null

You may not have the same result when you run "pause > null".

I feel like an idiot for completely forgetting about the "null" command. +1 to you ALUCARDVPR!
It doesn't do it if you write to NUL instead of null.
#well
(19 items)
 
Lenovo L530
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Gigabyte Z87X-UD4H XFX Radeon HD 6950 Corsair CMX8GX3M2A2000C9 
Hard DriveHard DriveOptical DriveCooling
Samsung EVO 840 Hitachi HDS722020ALA330 Generic DVD±RW Burner Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows 8.1 Dell U2711 LG W2453 Ducky DK9008 Overclock.net Edition, Cherry MX B... 
PowerCaseMouseMouse Pad
Corsair TX850 Antec Three Hundred Corsair Raptor M40 QPAD HeatoN M 
AudioAudioAudio
E-MU Tracker|pre Beyerdynamic DT-770 250 Ohm AntLion ModMIc 
CPUGraphicsRAMRAM
Intel Ivy Bridge 3210M Intel HD 4000 Graphics Soldered Corsair Vengeance  
Hard DriveOptical DriveOSMonitor
500GB DVD-+RW Windows 7 Professional 1600x900 
  hide details  
Reply
#well
(19 items)
 
Lenovo L530
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Gigabyte Z87X-UD4H XFX Radeon HD 6950 Corsair CMX8GX3M2A2000C9 
Hard DriveHard DriveOptical DriveCooling
Samsung EVO 840 Hitachi HDS722020ALA330 Generic DVD±RW Burner Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows 8.1 Dell U2711 LG W2453 Ducky DK9008 Overclock.net Edition, Cherry MX B... 
PowerCaseMouseMouse Pad
Corsair TX850 Antec Three Hundred Corsair Raptor M40 QPAD HeatoN M 
AudioAudioAudio
E-MU Tracker|pre Beyerdynamic DT-770 250 Ohm AntLion ModMIc 
CPUGraphicsRAMRAM
Intel Ivy Bridge 3210M Intel HD 4000 Graphics Soldered Corsair Vengeance  
Hard DriveOptical DriveOSMonitor
500GB DVD-+RW Windows 7 Professional 1600x900 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need some help with batch file coding