从批处理创建批处理文件或如何编写未执行的代码

时间:2013-11-06 16:07:45

标签: windows batch-file

我想从另一批创建批处理文件。那么如何在新的bat中编写代码而不在原始内部执行呢? 到目前为止我的代码是:

SET var1=%~dp0
SET var2
for %%a in ("%cd%") do set var2=%%~na
echo bla %var1% bla >> build%var2%.bat

而不是“bla”应该有多行代码。我如何存档?

编辑:

现在我的代码看起来像这样:

SET var1=%~dp0
SET var2
for %%a in ("%cd%") do set var2=%%~na

SET string = (
echo SET mdbname=%var2%
echo SET OfficeDIR=%%ProgramFiles(x86)%%\Microsoft Office\Office14
echo SET WorkDIR=%var1%
echo md build

echo copy %%mdbname%%.mdb build\%%mdbname%%.mda /Y
echo "%%officedir%%\msaccess.exe" /compact "%%WorkDir%%\build\%%mdbname%%.mda" 

echo "D:****.exe" "%%WorkDir%%\build\%%mdbname%%.mda" "D:\****** Key - Nicht weitergeben" "%%officedir%%"

echo copy "%%WorkDir%%\build\%%mdbname%%.mda" "%%ProgramFiles(x86)%%\*******" /Y
)
echo %string% >> built%var2%.bat

但是在新创建的批处理中,只写了:“ECHO ist eingeschaltet(ON)。” /“ECHO被激活(ON)。”

EDIT2:

现在它看起来如此,并且没有发生任何事情,甚至没有创建文件

SET var1=%~dp0
SET var2
for %%a in ("%cd%") do set var2=%%~na

(echo SET mdbname=%var2%
echo SET OfficeDIR=%%ProgramFiles(x86)%%\Microsoft Office\Office14
echo SET WorkDIR=%var1%
echo md build

echo copy %%mdbname%%.mdb build\%%mdbname%%.mda /Y
echo "%%officedir%%\msaccess.exe" /compact "%%WorkDir%%\build\%%mdbname%%.mda" 

echo "D:\TFS\RepV\RepV 6.1\ITPBuildTool61.exe" "%%WorkDir%%\build\%%mdbname%%.mda" "D:\TFS\RepV\RepV 6.1\ITPEAK.OLKey Private Key - Nicht weitergeben" "%%officedir%%"

echo copy "%%WorkDir%%\build\%%mdbname%%.mda" "%%ProgramFiles(x86)%%\Sage\Office Line\6.1\Addin\Work" /Y) > built%var2%.bat

2 个答案:

答案 0 :(得分:3)

jeb's answer上构建此处是您的代码,其中包含应用原则。

@echo off
set "var1=%~dp0"
set "var2="
for %%A in ("%cd%") do set "var2=%%~nA"

(
echo @echo off
echo set "mdbname=%var2%"
echo set "OfficeDIR=%%ProgramFiles(x86)%%\Microsoft Office\Office14"
echo set "WorkDIR=%var1%"
echo md build

echo copy "%%mdbname%%.mdb" "build\%%mdbname%%.mda" /Y
echo "%%officedir%%\msaccess.exe" /compact "%%WorkDir%%\build\%%mdbname%%.mda" 

echo "D:\TFS\RepV\RepV 6.1\ITPBuildTool61.exe" "%%WorkDir%%\build\%%mdbname%%.mda" "D:\TFS\RepV\RepV 6.1\ITPEAK.OLKey Private Key - Nicht weitergeben" "%%officedir%%"

echo copy "%%WorkDir%%\build\%%mdbname%%.mda" "%%ProgramFiles(x86)%%\Sage\Office Line\6.1\Addin\Work" /Y
) > built%var2%.bat

echo %cd%
type built%var2%.bat
pause

这对我有用,并在工作目录中创建一个包含以下内容的文件。

@echo off
set "mdbname=Desktop"
set "OfficeDIR=%ProgramFiles(x86)%\Microsoft Office\Office14"
set "WorkDIR=C:\Users\User\Desktop\"
md build
copy "%mdbname%.mdb" "build\%mdbname%.mda" /Y
"%officedir%\msaccess.exe" /compact "%WorkDir%\build\%mdbname%.mda"
"D:\TFS\RepV\RepV 6.1\ITPBuildTool61.exe" "%WorkDir%\build\%mdbname%.mda" "D:\TFS\RepV\RepV 6.1\ITPEAK.OLKey Private Key - Nicht weitergeben" "%officedir%"
copy "%WorkDir%\build\%mdbname%.mda" "%ProgramFiles(x86)%\Sage\Office Line\6.1\Addin\Work" /Y

答案 1 :(得分:2)

使用echo回显所有行并转义所有特殊字符 &|<>需要前面的插入符号或引号 %需要加倍。

(
  echo @echo off
  echo echo This is batch2
  echo set var=abc
  echo echo %%var%%
  echo echo redirect ^> file2.ttx
) > build%var2%.bat