Jump to content











Photo
- - - - -

Windows 10 and a script question.


  • Please log in to reply
8 replies to this topic

#1 911CD.net

911CD.net

    Member

  • Advanced user
  • 86 posts
  •  
    United States

Posted 08 April 2022 - 08:57 PM

Hi Guys,

 

The script is designed to open a command prompt window in the file manager folder being viewed.  A simple task and simple code.

@echo off
:: Check for Run as Admin
:: https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights
net sessions >nul 2>&1
if %errorLevel% == 0 (
    echo  Running as Admin.
    echo.
    pause
)
::@START /MAX cmd /K mode Con Lines=2000 Cols=100
@START cmd /K mode Con Lines=2000 Cols=100

 

The problem is if the script is run as admin the command prompt window opens to the c:\Windows\system32 folder not the folder the script is in.

 

Any suggestions?



#2 gbrao

gbrao

    Frequent Member

  • Advanced user
  • 474 posts
  •  
    India

Posted 09 April 2022 - 04:02 AM

deleted.

 

undeleted :-)

 

I thought the OP wanted a bat script to run as admin.

 

https://www.tenforum...ndows-10-a.html



#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 09 April 2022 - 07:05 AM

Try adding a:

CD /D %~dp0

at the end of the batch (as a parameter to the cmd), it should change the current directory to the one where the batch is.

Or maybe better

pushd %~dp0

 

You need to use quotes and &&, I believe:

https://superuser.co...s-administrator

https://ss64.com/nt/cmd.html 

 

 

:duff:

Wonko



#4 wimb

wimb

    Platinum Member

  • Developer
  • 3756 posts
  • Interests:Boot and Install from USB
  •  
    Netherlands

Posted 09 April 2022 - 09:20 AM

Try adding a:

CD /D %~dp0

at the end of the batch (as a parameter to the cmd), it should change the current directory to the one where the batch is.

Or maybe better

pushd %~dp0

 

You need to use quotes and &&, I believe:

https://superuser.co...s-administrator

https://ss64.com/nt/cmd.html 

 

 

Adding pushd %~dp0 just before the last line where the cmd is Opened works OK



#5 Zharif

Zharif

    Frequent Member

  • .script developer
  • 172 posts
  • Location:Germany
  •  
    Germany

Posted 09 April 2022 - 10:59 AM

Hi,

 

Yes, always use pushd (mandatory), especially if you know that your script might be located on an unmapped, local network share.

Furthermore, take care about errors during further file operations (e.g. copy, delete) regarding "%~dp0" or "%CD%" variables.

%~dp0 always contains a trailing backslash in path; CD only when on drive root.

 

Over the years, instead of creating a vbs script on the fly that uses the ShellExecute API I tend to prefer a powershell solution that also works from XP-SP3 and upwards (no writing procedure neccessary).

Example:

 


@ECHO OFF
:: if on a network share avoid nagging command line message that UNC paths are not allowed
CLS

:: check for enabled command line extensions [mandatory]
VERIFY OTHER 2>NUL
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 ECHO.-Command line extensions are not available...& EXIT /B 1

:: pushd will create/map a temporary driveletter if location of script is on a network share
:: mapping may be removed via POPD command
PUSHD "%~sdp0" >NUL 2>&1 || ECHO.  some error message.&& PAUSE>NUL && EXIT /B 1
SET "_HERE=%CD%"
:: remove trailing backslash from path if exists
IF "%_HERE:~-1%"=="\" SET "_HERE=%_HERE:~0,-1%"

:: conditionally start script with elevated priviliges, set color and title afterwards
NET FILE >NUL 2>&1||CLS && POWERSHELL.EXE -windowstyle hidden -noprofile "Start-Process '%~dpnx0' -Verb RunAs" && EXIT
COLOR 02
TITLE %_HERE%
ECHO.Script path is: "%_HERE%"
PAUSE
POPD
EXIT



 


  • gbrao likes this

#6 911CD.net

911CD.net

    Member

  • Advanced user
  • 86 posts
  •  
    United States

Posted 09 April 2022 - 07:39 PM

Thank you guys.  :wub:

 

I had tried the %cd% parm in many places but not familiar with the %~dp0 one or the pushd function.  pushd %~dp0 before the @START line solves the problem.

 

Thank you again.  You guys are the best.  :worship:



#7 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 10 April 2022 - 07:35 AM

Good. :)

 

The %~dp0 is expanded to the drive and path of parameter 0, i.e. the small batch itself.

 

gbrao had posted (but later deleted, why? :unsure:) a different approach, adding I believe a "cmdhere" entry to right click menu. :dubbio:

 

As a side-side note, I never found the "cmdhere" approach in earlier Windows (XP)  to work the way I wanted (maybe on recent windows is different) and the only "right" solution I found was this one:

http://www.roggel.co...CMD/index.shtml

no idea if it works on anything later than XP or if there is something similar for later (64 bit) windows that allows elevated command prompt.

 

This:

https://github.com/D...rtcuts-registry

https://dkcool.tailn...-in-windows-10/

should be :unsure: the current "state of the art" for Windows 10/11 (though still PUSHD is better than CD/ D). 

 

In Windows 10 most probably the easiest would be to add the link in Explorer "QAT" (Quick Access Toolbar), see:

https://winaero.com/...current-folder/

 

and - from keyboard - the ALT+{number} should still be working :unsure::

https://superuser.co...s-administrator

 

:duff:

Wonko



#8 wimb

wimb

    Platinum Member

  • Developer
  • 3756 posts
  • Interests:Boot and Install from USB
  •  
    Netherlands

Posted 10 April 2022 - 09:11 AM

In PE_Tools we can use PowerRun to make CMD and PowerShell Context Menus according to alacran

 

You need PowerRun to be Trusted Installer

 

Download: PE_Tools  pw=bootwimb

 

Attached File  Context_menus_2022-04-10.png   275.53KB   0 downloads == Attached File  PE_Tools_2022-04-10_105819.jpg   62.21KB   0 downloads



#9 911CD.net

911CD.net

    Member

  • Advanced user
  • 86 posts
  •  
    United States

Posted 11 April 2022 - 05:32 PM

While the registry approaches are certainly interesting and useful for a user's installed system, my cmd script is on a USB drive which gets used on multiple systems.  And I don't always need the admin function but in the cases where I do the pushd %~dp0 line is a very helpful option to have.      :thumbsup:   :victory:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users