Batch file 通过批处理文件从文件中读取
我有一个file.txt,其中包含安装在我的机器上的oracles的路径。 从登记处 此文件包含的示例:Batch file 通过批处理文件从文件中读取,batch-file,Batch File,我有一个file.txt,其中包含安装在我的机器上的oracles的路径。 从登记处 此文件包含的示例: ORACLE_HOME REG_SZ C:\oracle\product\11.2.0\dbhome_1 ORACLE_HOME REG_SZ C:\oracle\product\11.2.0\dbhome_2 我想通过我的批处理文件将所有神谕路径插入一个列表或类似的列表。 如何在批处理文件中执行此操作? 谢谢 假设您希望在内存中而不是文件中有一个值
ORACLE_HOME REG_SZ C:\oracle\product\11.2.0\dbhome_1
ORACLE_HOME REG_SZ C:\oracle\product\11.2.0\dbhome_2
我想通过我的批处理文件将所有神谕路径插入一个列表或类似的列表。
如何在批处理文件中执行此操作?
谢谢 假设您希望在内存中而不是文件中有一个值的“列表”:
我认为大多数人更喜欢通过索引值访问的值的“数组”。(注意-批处理没有正式的数组,但可以模拟它们)
只要没有主路径包含,下面的简单代码就非常有用代码>。%%B的扩展如果包含,将被损坏代码>和延迟扩展已启用
@echo off
setlocal enableDelayedExpansion
:: Read the file and create an "array" of home paths
:: This will fail if any of the paths contain !
set /a cnt=0
for /f "usebackq tokens=2*" %%A in ("file.txt") do (
set /a cnt+=1
set "home.!cnt!=%%B"
)
:: Access the "array" members
for /l %%N in (1 1 %cnt%) do echo !home.%%N!
您可能会在许多环境中运行上述代码多年,并且永远不会遇到问题。但是某个地方的某个人可能会包含。有许多策略可以解决上述问题以处理代码>。以下是三种选择:
选项1-代码量最少,但调用速度最慢
@echo off
setlocal disableDelayedExpansion
:: Read the file and create an "array" of home paths
:: This will safely process all paths, regardless of value
set /a cnt=0
for /f "usebackq tokens=2*" %%A in ("file.txt") do (
set /a cnt+=1
call set "home.%%cnt%%=%%B"
)
:: Access the "array"
setlocal enableDelayedExpansion
for /l %%N in (1 1 %cnt%) do echo !home.%%N!
选项2-一种有趣而有效的方法,使用FINDSTR计算行数
@echo off
setlocal disableDelayedExpansion
:: Read the file and create an "array" of home paths
:: This will safely process all paths, regardless of value
set /a cnt=0
for /f "tokens=1,3* delims=: " %%A in ('findstr /n "^" "file.txt"') do (
set "home.%%A=%%C"
set "cnt=%%A"
)
:: Access the "array" members
setlocal enableDelayedExpansion
for /l %%N in (1 1 %cnt%) do echo !home.%%N!
选项3-一种使用延迟扩展切换的有效方法,但代码最多
@echo off
setlocal disableDelayedExpansion
:: Read the file and create an "array" of home paths
:: This will safely process all paths, regardless of value
set /a cnt=0
for /f "usebackq tokens=2*" %%A in ("file.txt") do (
set /a cnt+=1
setlocal enableDelayedExpansion
for %%N in (!cnt!) do (
endlocal
set "home.%%N=%%B"
)
)
:: Access the "array"
setlocal enableDelayedExpansion
for /l %%N in (1 1 %cnt%) do echo !home.%%N!
也可以在单个变量中包含主路径列表。每个路径都应该用引号括起来。路径可以用空格、逗号、分号、等号或制表符分隔。我选择了空间
列表的大小受到限制,因为批处理环境变量的最大大小约为8191字节。此解决方案也由于呼叫而相对较慢。这两个问题在现实世界中都不可能成为问题
@echo off
setlocal disableDelayedExpansion
:: Read the file and create a space delimited list of quoted home paths
:: This will safely process all paths, regardless of value
for /f "usebackq tokens=2*" %%A in ("file.txt") do (call set list=%%list%% "%%~B")
:: optional - remove leading space
(set list=%list:~1%)
:: Display the list
echo list=%list%
:: Access the list members
for %%F in (%list%) do echo %%~F
你想把它们添加到环境变量列表中吗?是的。我想最后输入每个路径并做一些事情。我想OP想要的是内存中的值,而不是文件中的值。此外,如果路径包含空间,则写入时将失败。使用“tokens=2*
和%%b
。非常感谢!你能给我解释一下最后一个选项吗?我不知道什么是usebackq、~B等,如果你也能解释上一个选项。我是批处理文件中的新成员,这将对我非常有帮助。谢谢@zipi-USEBACKQ在您的案例中并不是必需的,但我在您处理包含空格的文件名时使用了它。通常,带引号的IN()子句被视为字符串。USEBACKQ选项将quoted IN()子句视为文件名。在命令提示符下键入有关
的帮助以获取更多信息。我不明白你所问的其余部分。一般来说,您可以通过键入help commandName
或commandName/?
获得任何命令的帮助。您可能知道。我使用您的解决方案,现在我需要一些帮助:我使用:for%%F In(%list%)do,我想验证我尝试的每个路径中是否有文件名ss.tns:for%%F In(%list%)do if exist%%~F\bin\ss.tns(埃科成功了)但对我不起作用
@echo off
setlocal disableDelayedExpansion
:: Read the file and create a space delimited list of quoted home paths
:: This will safely process all paths, regardless of value
for /f "usebackq tokens=2*" %%A in ("file.txt") do (call set list=%%list%% "%%~B")
:: optional - remove leading space
(set list=%list:~1%)
:: Display the list
echo list=%list%
:: Access the list members
for %%F in (%list%) do echo %%~F