BATCH - Run as Admin from current path instead system32 folder.......


  1. Posts : 88
    Windows 10 X64 Home
       #1

    BATCH - Run as Admin from current path instead system32 folder.......


    For order a my folder i have created a similar .bat files:
    Code:
    md A
    md B
    ...
    ...
    md Z
    move a*.* .\A
    move b*.* .\B
    ..
    ..
    move z*.* .\Z
    There's a big issue......... i need to run this batch as admin and i have used the command "Run as Admin" from context menu and i hope i don't have create a BIG ISSUE on my windows 10 installation.
    Because this BATCH it's not corrently been executed from the path how it's stored the batch but all commands it's be correctly runned from C:\WINDOWS\SYSTEM32 folder!!!

    I don't have idea because there's append but right now on C:\Windows\System32 i have all subforder called A B C etc whit all files MOVED inside!!
    Some files it's not be moved because i see from windows prompt it's say it's protect (and problably all files it's currently used by windows 10).

    I have right now move back all files from all subfolder and i hope i need to reformat my windows or obtain boot problems at next reboot of my pc.... instead this because there's append !?!?

    How i can fix this issue ? Because if i use RUN AS ADMIN for a batch files the bat it's runned from system32 folder and not from the corrent path how it's stored the bat files ??
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @DjDiabolik,

    Place this code at the top of your batch file . . .

    Code:
    set "params=%*"
    cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs") && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && Exit /b)
    %windir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )

    Also, look at this => Run as Administrator in Windows 10

    I hope this helps!
      My Computer


  3. Posts : 88
    Windows 10 X64 Home
    Thread Starter
       #3

    mmmmmmmm.... i don't have understand if it's really need all this code.

    On some other guide found on internet i see someone suggest to use "PUSHD" commands.....

    Read the /h i thinks i can put it at very first line of my batch

    PUSHD %~dp0

    I can also use @ECHO for maintain hide this..... i need to test if it's works.
      My Computer


  4. Posts : 8,111
    windows 10
       #4

    What errors do you get and what are the permissions on the folder your running from?
      My Computer


  5. Posts : 14,046
    Windows 11 Pro X64 22H2 22621.1848
       #5

    Code:
    @Echo off
    ::
    :: Restart as Admin if not already running as Admin
    ::
    (Net session >nul 2>&1)||(PowerShell start """%~fn0""" -verb RunAs & Exit /B)
    
    md A
    md B
    ...
    ...
    md Z
    move a*.* .\A
    move b*.* .\B
    ..
    ..
    move z*.* .\Z
      My Computers


  6. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #6

    Do you always run the batch file from the same place? If so, you can just add a new first line to change the folder to that location.
    cd /d E:\Wherever\WhereverElse

    If not, please explain how you use the batch file including why it needs to be run as admin and which folders might it be used in.
    There are several possible approaches and the most suitable ones will vary with your explanation.

    Denis
      My Computer


  7. Posts : 88
    Windows 10 X64 Home
    Thread Starter
       #7

    Samuria said:
    What errors do you get and what are the permissions on the folder your running from?
    I don't know because but if i lauch this batch whiout admin permission on command prompt windows i see some many erros reports i can create the folder.
    So i tried to run the same .bat file using "Run As Admin" and boom...... i made a possibly disaster because the file .bat it's be executed but not from the path how it's the batch but all command it's be executed from System32 folder....

    Try3 said:
    Do you always run the batch file from the same place? If so, you can just add a new first line to change the folder to that location.
    cd /d E:\Wherever\WhereverElse

    If not, please explain how you use the batch file including why it needs to be run as admin and which folders might it be used in.
    There are several possible approaches and the most suitable ones will vary with your explanation.

    Denis
    It could be a solution but I needed to move the same bat file from different folders so if it was I had to adapt it every time.
    Ztruker said:
    Code:
    @Echo off
    ::
    :: Restart as Admin if not already running as Admin
    ::
    (Net session >nul 2>&1)||(PowerShell start """%~fn0""" -verb RunAs & Exit /B)
    
    md A
    md B
    ...
    ...
    md Z
    move a*.* .\A
    move b*.* .\B
    ..
    ..
    move z*.* .\Z
    I understood what I was advised to do .... as I said using "Pushd" is cmq a good alternative.

    in the end, friends, I used this stuff here.. this is my ORDINA.bat:
    Code:
    echo %~dp0
    pushd %~dp0
    ::dir B*.nes
    pause
    ::exit
    ::goto comment
    md A
    md B
    md C
    md D
    md E
    md F
    md G
    md H
    md I
    md L
    md M
    md N
    md O
    md P
    md Q
    md R
    md S
    md T
    md U
    md V
    md Z
    md J
    md K
    md W
    md X
    md Y
    md 0-9
    move A*.nes .\A
    move B*.nes .\B
    move C*.nes .\C
    move D*.nes .\D
    move E*.nes .\E
    move F*.nes .\F
    move G*.nes .\G
    move H*.nes .\H
    move I*.nes .\I
    move L*.nes .\L
    move M*.nes .\M
    move N*.nes .\N
    move O*.nes .\O
    move P*.nes .\P
    move Q*.nes .\Q
    move R*.nes .\R
    move S*.nes .\S
    move T*.nes .\T
    move U*.nes .\U
    move V*.nes .\V
    move Z*.nes .\Z
    move J*.nes .\J
    move K*.nes .\K
    move W*.nes .\W
    move X*.nes .\X
    move Y*.nes .\Y
    move *.nes .\0-9
    
    ::comment
    You can also see my test commented out to see if this works from desidered path and not from system32 folder :)
    Extension of all files... .nes... now you know what i order on my HD :)
      My Computer


  8. Posts : 14,046
    Windows 11 Pro X64 22H2 22621.1848
       #8

    If it worked it's a good solution for you.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 07:35.
Find Us




Windows 10 Forums