2

As part of a project I need to add text to the middle of many files using batch scripting. I am able to add the text successfully, but after copying the files to a new location I noticed that the HTML tags are missing. I only have this problem in Windows Server 2012/2008; in Windows 7 the HTML tags remain intaact.

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"
)

Below is my sample input file...

 **Sample input eml:**
   Date Mon,20 mar 2017
   From:[email protected]
   To:[email protected]
   Message-ID:<10091223>
    Subject:Symphony
   x-globalrelay-MsgType: XXXX
   x-StreamType:xxxx
   x-contentstartdate:XXX
 
   <html><body>  Message ID:sm9atRNTnMA=Yay1R0QgoH.............. </html> 

After executing my script in Server 2012 I am able to successfully inject the required text in the middle, but as I said the HTML tags are missing:

 **Sample input eml:**
   Date Mon,20 mar 2017
   From:[email protected]
   To:[email protected]
   Message-ID:<10091223>
    Subject:Symphony
   X-SUBTYPE=RETURES
   x-globalrelay-MsgType: XXXX
   x-StreamType:xxxx
   x-contentstartdate:XXX
 
   <Yay1R0QgoH.............. </html> 

As said I am able to generate the desired output by adding the text in the middle in Windows 8 with the same script. I am not able to identify why it is giving different output(html tags are missing)in Windows Server 2012.

1
  • Hi can anyone help me with this... Commented Apr 25, 2017 at 2:29

2 Answers 2

2

Rewritten slightly, explanation mostly in rem comments:

@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"
Sign up to request clarification or add additional context in comments.

Comments

0

There were some syntax errors in original source code (missing ! in "!var:~0,10", additional d in enableDdelayedexpansion), but I don't see a reason for this not working on 2012/2008.

Try with

@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"
    )

Note that the if "!var:~0,10!" == "x-globalre" ( line does not take into account the presence of spaces at the start of the line (the same your original code does).

1 Comment

Thanks MC ND.....adding setlocal enableextensions disabledelayedexpansion had done the trick thanks for your valuable suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.