标题: [其他] [已解决] 批处理for语句多行文本怎样不使用临时文件? [打印本页]
作者: newswan 时间: 2021-8-13 18:31 标题: [已解决] 批处理for语句多行文本怎样不使用临时文件?
本帖最后由 newswan 于 2021-8-15 11:04 编辑
- (echo;list vol
- echo;list vdisk
- echo;list disk
- echo;sel disk=0
- echo;detail disk
- echo;list part
- for /l %%a in (1 1 99) do (
- echo;sel disk=next
- echo;detail disk
- echo;list part
- ))>"%temp%\dk.tmp"
-
- (for /f "skip=5 delims=" %%Z in ('diskpart /s "%temp%\dk.tmp"') do (echo %%z)
复制代码
请问,有没有办法,不使用临时文件,把多行文本保存在变量中,然后在 for 语句中使用
作者: idwma 时间: 2021-8-13 18:52
- (set a=list vol
- set a=%a%#list vdisk
- set a=%a%#list disk
- set a=%a%#sel disk=0
- set a=%a%#detail disk
- set a=%a%#list part
- setlocal enabledelayedexpansion
- for /l %%a in (1 1 99) do (
- set a=!a!#sel disk=next
- set a=!a!#detail disk
- set a=!a!#list part
- ))
- set a=%a:#=^^&echo;%
- (for /f "skip=5 delims=" %%Z in ('^(echo;%a%^)^|diskpart') do (echo %%z)
复制代码
作者: newswan 时间: 2021-8-13 19:55
回复 2# idwma
报错,- >set a=^ & echo;list part^ & echo;list part^ & echo;list part^ & echo;list part^ & echo;list part^ & echo;list part^ & echo;list part^ & echo;sel disk=next^ & echo;detail disk^ & echo;list part^ & echo;sel disk=next^ & echo;detail disk^ & echo;list part
- list part^
- list part^
- list part^
- list part^
- list part^
- list part^
- list part^
- sel disk=next^
- detail disk^
- list part^
- sel disk=next^
- detail disk^
- list part
- |diskpart was unexpected at this time.
复制代码
作者: newswan 时间: 2021-8-13 19:56
用过复制代码
没有试出来
作者: idwma 时间: 2021-8-13 20:29
不好意思又没有上机测试- set a=list vol
- set a=%a%#list vdisk
- set a=%a%#list disk
- set a=%a%#sel disk=0
- set a=%a%#detail disk
- set a=%a%#list part
- setlocal enabledelayedexpansion
- for /l %%a in (1 1 2) do (
- set a=!a!#sel disk=next
- set a=!a!#detail disk
- set a=!a!#list part
- )
-
- for /f "skip=5 delims=" %%Z in ('^(echo;%a:#=^&echo;%^)^|diskpart') do (echo %%Z)
复制代码
作者: newswan 时间: 2021-8-13 20:53
本帖最后由 newswan 于 2021-8-13 21:02 编辑
谢谢,要在执行时替换,以前见过,忘记这点了
作者: newswan 时间: 2021-8-14 12:37
本帖最后由 newswan 于 2021-8-14 17:46 编辑
- @echo off
- setlocal enableDelayedExpansion
-
- set _LF_=^&echo
- set _CL_= sel disk 0
- set _CL_=!_CL_! !_LF_! detail disk
- set _CL_=!_CL_! !_LF_! list part
-
- echo !_CL_!
- echo %_CL_%
-
-
- echo ----1
- for /f "usebackq tokens=* delims=" %%Z in (`
- cmd /c "(echo %_CL_%) | diskpart"
- `) do (
- echo %%Z
- )
- echo ----2
- for /f "usebackq tokens=* delims=" %%Z in (`
- cmd /c "(echo !_CL_!) | diskpart"
- `) do (
- echo %%Z
- )
复制代码
找到了以前的
作者: newswan 时间: 2021-8-15 11:04
还是这种方式吧,简单易读- set "_CL_= echo list disk & echo sel disk 0 & echo list part"
- for /f "usebackq skip=6 tokens=* delims=" %%a in (`cmd /c "%_CL_% | diskpart"`) do (
- echo %%a
- )
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |