批处理脚本回显另一个批处理脚本

Batch Script to Echo another batch script

如何在批处理文件中回显(创建)另一个批处理文件到 windows 中的 运行 并在 运行ning 之后删除它? [实际尝试使用一个批处理脚本安装 WSl 2]

我试过了,

@echo off
:: BatchGotAdmin
// asking one time admin priv code  here
@echo off
title wsl setup Part 1 ! 


dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

// and here is my another batch to need echo correctly in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\ for run after reboot  
 
(echo @echo off^
:: BatchGotAdmin
// admin priv for  seconf batch  

@echo off
title wsl setup part 2 !

//other steps for download kernel and set default wsl version here

echo "Setup Finished, Deleting this bat" && del C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\wsl-part2.bat && pause
) > "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\wsl-part2.bat"


//First batch again for asking reboot now or later


:PROMPT
SET /P REBOOTNOW=Do you want reboot now second script will run after reboot (Y/[N])?
IF /I "%REBOOTNOW%" NEQ "N" GOTO END

shutdown -r


:END
endlocal

但我卡住了 dism 命令循环(因为 echo 不明白它是一个字符串)并且只回显到目标文件

@echo off:: BatchGotAdmin
Requesting administrative privileges...

尽管我的意见与此相反,但如果您想使用如此复杂的方法来执行此操作,请使用以下一般语法:

(   Echo @Echo Off
    Echo Rem BatchGotAdmin
    Echo // admin priv for second batch  
    Echo Title WSL Setup Part 2.
    Echo // other steps for download kernel and set default WSL version here
    Echo Echo Setup Finished, deleting this script.
    Echo (GoTo^) 2^>Nul ^& Del "%%~f0"
) 1> "%ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp\wsl-part2.cmd"

您应该注意到我已经将自我删除命令更改为更有效和更实用的命令。它决定这样做是因为它向您表明,使用此方法,您需要使用脱字符号 (包括与号、重定向符号、竖线和右括号) 来转义有问题的字符。您还需要通过加倍转义 % 个字符。