文件名:test.txt
文件内容:
aaa
str1=c:\aaa
str2=d:\bbb
str3=e\ccc
bbb
str1=aaa
str2=bbb
str3=ccc
要求:
批处理脚本,查找文件中aaa下的str1的值,赋值给变量并在此目录下创建文件,文件内容为bbb中的str1.
第1个回答 推荐于2017-10-08
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims==" %%a in (test.txt) do (
if "%%b"=="" set "n=%%a"
if "%%a"=="str1" (
if "!n!"=="aaa" set "a=%%b"
if "!n!"=="bbb" set "b=%%b"
)
)
cd /d %a%
echo %b% > "%b%.txt"本回答被提问者采纳
for /f "tokens=1* delims==" %%a in (test.txt) do (
if "%%b"=="" set "n=%%a"
if "%%a"=="str1" (
if "!n!"=="aaa" set "a=%%b"
if "!n!"=="bbb" set "b=%%b"
)
)
cd /d %a%
echo %b% > "%b%.txt"本回答被提问者采纳
第2个回答 2015-03-06
@echo off
setlocal enabledelayedexpansion
for /f "delims=` tokens=* eol=a" %%a in (test.txt) do (
if "%%a"=="bbb" goto :n
set %%a
set pth=!str1!
)
:n
find /n "bbb" test.txt>temp.tmp
for /f "delims=] tokens=1-2 eol=`" %%a in (temp.tmp) do (
set l=%%a
set l=!l:[=!
set l=!l: =!
goto :o
)
:o
for /f "delims=` tokens=* eol=` skip=!l!" %%a in (test.txt) do (
set %%a
)
echo !str1!>!pth!\aaa.txt
setlocal enabledelayedexpansion
for /f "delims=` tokens=* eol=a" %%a in (test.txt) do (
if "%%a"=="bbb" goto :n
set %%a
set pth=!str1!
)
:n
find /n "bbb" test.txt>temp.tmp
for /f "delims=] tokens=1-2 eol=`" %%a in (temp.tmp) do (
set l=%%a
set l=!l:[=!
set l=!l: =!
goto :o
)
:o
for /f "delims=` tokens=* eol=` skip=!l!" %%a in (test.txt) do (
set %%a
)
echo !str1!>!pth!\aaa.txt