create a command line argument .bat file that can change all .exe file compatibility settings in a specific directroy

£可爱£侵袭症+ 提交于 2020-05-09 06:50:50

问题


I have multiple drives with a different directory structures. I have a directory called "test files" with of several .exe files that I need to change the compatibility settings to "run this program as administrator"

Is it possible to create a windows .bat file that runs as Admin and that can change all the .exe files compatibility settings in a specific directory and all its sub-directories, regardless of where "test files" is located, to "run this program as an Administrator"
This is what I have thus far

for /r "J:test files\" %%A in (*.exe) do reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%%~A" /d "RUNASADMIN"

回答1:


In batch-file named runasadmin.cmd:

@echo off
for /r "C:\test files\" %%A in (*.exe) do reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%%~A" /d "RUNASADMIN"

Using C:\test files\ as example path as most users will have a C: drive.

In interactive CMD Prompt:

for /r "C:\test files\" %A in (*.exe) do reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%~A" /d "RUNASADMIN"

This batch-file code will recurse the directory C:\test files\ for exe files and register them in the Windows Registry to run as admin. The interactive CMD Prompt does not need the for variable %A to be escaped with another % as the command is parsed less times than the batch-file.

To make a batch-file that can accept a variable path, then this might be suitable, though has no argument check to validate:

@echo off
for /r %1 %%A in (*.exe) do reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%%~A" /d "RUNASADMIN"

The %1 is a named variable that is replaced by the 1st script argument. If %2 were used, it would be replaced by the 2nd script argument. %0 is the command and %1 to %9 is the available arguments.

To pass the arguments, you can do it from a CMD Prompt, from a Shortcut, from the Windows Registry and from anywhere else that supports passing arguments.

The script runasadmin.cmd can be executed from anywhere on the system if placed in the system PATH. The Windows directory is in the system PATH so can copy runasadmin.cmd to that location. With runasadmin.cmd in PATH, open the File Explorer and navigate to the root directory of the exe files to register the exe files. Once there, type cmd into the Address Bar. A CMD Prompt will open and will be at the current directory of the File Explorer location. Enter runasadmin.cmd or the shorter runasadmin without the extension. This will run the batch-file code and register all exe files in the current directory and subdirectories.

If placing the script in PATH is unwanted, then open the File Explorer and navigate to the root directory of the script. Enter the path as an argument of where the root directory of the exe files are located. Once there, type cmd into the Address Bar. A CMD Prompt will open and will be at the current directory of the File Explorer location. If the exe files root directory is C:\test files\, then enter the command runasadmin "C:\test files\". This will run the batch-file code and register all exe files in the directory of C:\test files\ and subdirectories. Any valid directory path can be passed as the 1st argument.

Some variations to runasadmin.cmd:

@echo off

rem Ensure 1st argument is valid.
if not "%~1" == "" (
    if not exist "%~1" (
        >&2 echo Require a valid directory path as the 1st argument.
        exit /b 1
    )
)

rem Register the exe files.
for /r %1 %%A in (*.exe) do (
    reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%%~A" /d "RUNASADMIN" /f >nul
)
  • A check is done to ensure the 1st argument is valid.
  • %~1 is %1 with surrounding double quotes removed.
  • >&2 echo echoes the following error message text to the stderr stream.
  • If exit /b 1 happens, the errorlevel variable can be checked if it's value is 1.
  • /f >nul forces the add to registry and the success message is redirected to nul, which silences the success message. Error messages are not silenced.
@echo off
setlocal

@rem Display help message.
@if "%~1" == "/?" goto :help
@if "%~1" == "-h" goto :help

rem Assign all arguments to allargs.
set allargs=%*

rem Assign value of 1st argument to variable named root.
set "root=%~1"

rem Assign value of 2nd argument to variable named undo.
set "undo=%~2"

rem Enter a root path if no argument passed.
if not defined root set /p "root=Enter root path of exe files: " || exit /b 0

rem Remove any double quotes.
set "root=%root:"=%"

rem Ensure 1st argument is valid.
if not exist "%root%" (
    >&2 echo Require a valid directory path as the 1st argument.
    exit /b 1
)

rem Enter y or n to undo registration.
if defined undo (
    if "%undo%" == "1" set "undo=y"
) else if not defined allargs (
    set /p "undo=Undo registration [n|y]: "
)

if /i not "%undo%" == "y" set "undo="

rem Register the exe files.
for /r "%root%" %%A in (*.exe) do (
    echo ENTRY: "%%~A"

    if defined undo (
        reg delete "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%%~A" /f >nul
    ) else (
        reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%%~A" /d "RUNASADMIN" /f >nul
    )
)
exit /b 0

:help
echo Syntax: "%~nx0" [root [undo]]
echo:
echo Examples:
echo   To register exe files in "C:\test files\".
echo       "%~nx0" "C:\test files\"
echo:
echo   To undo register of exe files in "C:\test files\".
echo       "%~nx0" "C:\test files\" 1
echo:
echo 1st arg is root directory to search recursively.
echo 2nd arg set to 1 to undo registration.
echo:
echo No args will display prompt for root directory and if to undo.
exit /b 0
  • setlocal to keep variables set in the script as local to the script.
  • Value of all arguments is assigned to variable named allargs to later check if allargs is defined.
  • Value of 1st argument is assigned to variable named root.
  • Value of 2nd argument is assigned to variable named undo.
  • 1st argument is required, else a prompt displays to ask for a root path.
  • if not defined root is if variable name has no value which makes the variable name undefined.
  • || exit /b 0 happens if input from the prompt is empty.
  • Enter runasadmin.cmd /? at CMD Prompt for help with command line usage.

Suggest one of the later variations as they check for mistakes in the command line or input. The last code is the most featured.

if need help with a command such as reg, enter reg /? at a CMD Prompt to view the builtin help of the command.


References:

  • An A-Z Index of Windows CMD commands
  • How-to: Pass Command Line arguments (Parameters) to a Windows batch file
  • Command line parameters


来源:https://stackoverflow.com/questions/61302750/create-a-command-line-argument-bat-file-that-can-change-all-exe-file-compatibi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!