作为项目的一部分,我需要使用批处理脚本将文本添加到许多文件的中间。我能够成功添加文本,但在将文件复制到新位置后,我注意到HTML标记丢失了。我只在Windows Server 2012/2008中遇到此问题;在Windows 7中,HTML标记仍然是完整的。
My Code snippet:
@echo off
set SrcFolder=C:\Users\emlfilessample
set DstFolder=C:\Users\output
FOR %%f in (%SrcFolder%*.eml) do (
(FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ %%f"`) do (
SETLOCAL EnabledDelayedExpansion
set "var=%%a"
set "var=!var:*:=!"
if "!var:~0,10" == "x-globalre" (
echo X-SUBTYPE=RETURES
)
echo(!var!
ENDLOCAL
)) >> "%DstFolder%\%%~nxf"
)
以下是我的示例输入文件...
**Sample input eml:**
Date Mon,20 mar 2017
From:ALan@vc.com
To:Weijie@vc.com
Message-ID:<10091223>
Subject:Symphony
x-globalrelay-MsgType: XXXX
x-StreamType:xxxx
x-contentstartdate:XXX
<html><body> Message ID:sm9atRNTnMA=Yay1R0QgoH.............. </html>
在Server 2012中执行我的脚本后,我能够在中间成功注入所需的文本,但正如我所说的那样,HTML标签丢失了:
**Sample input eml:**
Date Mon,20 mar 2017
From:ALan@vc.com
To:Weijie@vc.com
Message-ID:<10091223>
Subject:Symphony
X-SUBTYPE=RETURES
x-globalrelay-MsgType: XXXX
x-StreamType:xxxx
x-contentstartdate:XXX
<Yay1R0QgoH.............. </html>
如上所述,我可以通过在Windows 8中间添加相同脚本的文本来生成所需的输出。我无法确定为什么它在Windows Server 2012中提供不同的输出(缺少html标签)。
答案 0 :(得分:2)
稍微重写,解释主要在rem
评论中:
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set SrcFolder=C:\Users\emlfilessample
set DstFolder=C:\Users\output
FOR %%f in (%SrcFolder%\*.eml) do (
> "%DstFolder%\%%~nxf" (
FOR /F "tokens=1,* delims=:" %%a in ('findstr /n "^" "%%~f"') do (
rem remove leading spaces from original line if any
FOR /F "tokens=*" %%G in ("%%~b") do (
set "var=%%~G"
SETLOCAL EnableDelayedExpansion
if "!var:~0,10!" == "x-globalre" (
echo X-SUBTYPE=RETURES
)
ENDLOCAL
)
rem output original line including all ! exclamation marks
rem AND all leading spaces if any
echo(%%b
)
)
)
rem debugging output type "%DstFolder%\*.eml"
答案 1 :(得分:0)
原始源代码中存在一些语法错误(!
中缺少"!var:~0,10"
,d
中缺少enableDdelayedexpansion
),但我看不出原因没有在2012/2008年工作。
尝试
@echo off
setlocal enableextensions disabledelayedexpansion
set "SrcFolder=C:\Users\emlfilessample"
set "DstFolder=C:\Users\output"
for %%f in ("%SrcFolder%\*.eml") do (
(
FOR /F "delims=" %%a in ('findstr /n "^" "%%~ff"') do (
set "var=%%a"
setlocal EnableDelayedExpansion
set "var=!var:*:=!"
if "!var:~0,10!" == "x-globalre" (
echo X-SUBTYPE=RETURES
)
echo(!var!
endlocal
)
) > "%DstFolder%\%%~nxf"
)
请注意,if "!var:~0,10!" == "x-globalre" (
行没有考虑行开头是否存在空格(与原始代码相同)。