[英]create a command line argument .bat file that can change all .exe file compatibility settings in a specific directroy
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"
我有一个名为“测试文件”的目录,其中包含几个.exe 文件,我需要将兼容性设置更改为“以管理员身份运行此程序”
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"是否可以创建一个以管理员身份运行的 windows.bat 文件,并且可以更改特定目录及其所有子目录中的所有 .exe 文件兼容性设置,无论“测试文件”位于何处,以“运行此程序作为管理员”
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"
In batch-file named runasadmin.cmd
:在名为
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.使用
C:\test files\
作为示例路径,因为大多数用户将拥有C:
驱动器。
In interactive CMD Prompt:在交互式 CMD 提示中:
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.此批处理文件代码将递归目录
C:\test files\
用于exe
文件,并将它们注册到 Windows 注册表中以以管理员身份运行。 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.交互式 CMD 提示不需要
for
变量%A
用另一个%
转义,因为该命令的解析次数少于批处理文件。
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. %1
是由第一个脚本参数替换的命名变量。 If %2
were used, it would be replaced by the 2nd script argument.如果使用
%2
,它将被第二个脚本参数替换。 %0
is the command and %1
to %9
is the available arguments. %0
是命令, %1
到%9
是可用的 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.要通过 arguments,您可以从 CMD 提示符、快捷方式、Windows 注册表以及其他任何支持传递 ZDBC11CAABZEDA29 的地方进行。
The script runasadmin.cmd
can be executed from anywhere on the system if placed in the system PATH.如果将脚本
runasadmin.cmd
放在系统 PATH 中,则可以从系统上的任何位置执行。 The Windows directory is in the system PATH so can copy runasadmin.cmd
to that location. Windows 目录位于系统路径中,因此可以将
runasadmin.cmd
复制到该位置。 With runasadmin.cmd
in PATH, open the File Explorer and navigate to the root directory of the exe
files to register the exe
files.使用 PATH 中的
runasadmin.cmd
,打开文件资源管理器并导航到exe
文件的根目录以注册exe
文件。 Once there, type cmd
into the Address Bar.在那里,在地址栏中键入
cmd
。 A CMD Prompt will open and will be at the current directory of the File Explorer location. CMD 提示将打开,并位于文件资源管理器位置的当前目录中。 Enter
runasadmin.cmd
or the shorter runasadmin
without the extension.输入
runasadmin.cmd
或不带扩展名的较短的runasadmin
。 This will run the batch-file code and register all exe
files in the current directory and subdirectories.这将运行批处理文件代码并注册当前目录和子目录中的所有
exe
文件。
If placing the script in PATH is unwanted, then open the File Explorer and navigate to the root directory of the script.如果不需要将脚本放在 PATH 中,则打开文件资源管理器并导航到脚本的根目录。 Enter the path as an argument of where the root directory of the
exe
files are located.输入路径作为
exe
文件根目录所在位置的参数。 Once there, type cmd
into the Address Bar.在那里,在地址栏中键入
cmd
。 A CMD Prompt will open and will be at the current directory of the File Explorer location. CMD 提示将打开,并位于文件资源管理器位置的当前目录中。 If the
exe
files root directory is C:\test files\
, then enter the command runasadmin "C:\test files\"
.如果
exe
文件根目录是C:\test files\
,则输入命令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.这将运行批处理文件代码并注册
C:\test files\
目录和子目录中的所有exe
文件。 Any valid directory path can be passed as the 1st argument.任何有效的目录路径都可以作为第一个参数传递。
Some variations to runasadmin.cmd
: 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
)
%~1
is %1
with surrounding double quotes removed. %~1
是%1
,去掉了周围的双引号。>&2 echo
echoes the following error message text to the stderr stream. >&2 echo
将以下错误消息文本回显到标准错误 stream。exit /b 1
happens, the errorlevel
variable can be checked if it's value is 1
.exit /b 1
发生,则可以检查errorlevel
变量的值是否为1
。/f >nul
forces the add to registry and the success message is redirected to nul
, which silences the success message. /f >nul
强制添加到注册表,成功消息被重定向到nul
,这会使成功消息静音。 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. setlocal
将脚本中set
的变量保持为脚本的本地变量。allargs
to later check if allargs
is defined.allargs
的变量,以便稍后检查是否定义了allargs
。root
.root
的变量。undo
.undo
的变量。if not defined root
is if variable name has no value which makes the variable name undefined
. if not defined root
是如果变量名没有值,这使得变量名undefined
。|| exit /b 0
|| exit /b 0
happens if input from the prompt is empty.|| exit /b 0
发生。runasadmin.cmd /?
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 /?
如果需要有关
reg
等命令的帮助,请输入reg /?
at a CMD Prompt to view the builtin help of the command.在 CMD 提示查看命令的内置帮助。
References:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.