从批处理文件中的.txt文件读取多个路径

时间:2019-12-29 13:42:14

标签: windows batch-file filepath

我有一个批处理脚本,它需要读取config.txt中提到的多个目录路径。我可以为一个目录路径实现此功能,但是它的修改版本不适用于多个路径。

下面是运行良好的示例。

@echo off
for /F "usebackq delims=" %%L in (config.txt) do set "DataPath=%%L"
set "DataPath=%DataPath:/=\%"
echo Application path is: %DataPath%

如何修改它以处理多个目录路径。

编辑:

下面是我尝试获取两个路径,而'%DataPath%'正在打印该值。

@echo off
for /F "usebackq tokens=1,2 delims=" %%L in (config.txt) do set 
"DataPath=%%L"&set "filepath=%%M"
 set "DataPath=%DataPath:/=\%"
 set "filepath=%filepath:/=\%"
 echo Application path is: %DataPath%
 echo Application path is: %filepath%

2 个答案:

答案 0 :(得分:0)

基于我在评论中给出的建议(您在编辑中没有实现),以下是我希望您提供的方法:

@Echo Off
SetLocal DisableDelayedExpansion
For /F "UseBackQ Delims=" %%L In ("config.txt") Do (
    Set "DataPath=%%L"
    SetLocal EnableDelayedExpansion
    Set "DataPath=!DataPath:/=\!"
    Echo Application path is: !DataPath!
    EndLocal
)
Pause

这假设config.txt包含仅文件路径的列表,每行一个。

答案 1 :(得分:0)

从文件恢复路径的阵列版本。

@ECHO OFF
SETLOCAL EnableDelayedExpansion
Set "_P=0"

FOR /F "tokens=* USEBACKQ" %%a IN (%userprofile%\desktop\logfile.txt) DO (
    Set /a _P+=1
    Set "Pathway[!_P!]=%%a"
)

::: use the below to take actions with elements in the array.
::: For example, search the array for specific files to delete/copy/move/open.

FOR /L %%e IN (1,1,!_P!) DO (
    ECHO Path !%%e! = !Pathway[%%e]!
)
pause