How to remove the first and last character from a file using batch script?

Posted by infant programmer on Stack Overflow See other posts from Stack Overflow or by infant programmer
Published on 2010-03-24T06:55:15Z Indexed on 2010/03/24 7:13 UTC
Read the original article Hit count: 163

Filed under:
|

This is my input file content which I am using to copy to the output file.

#sdfs|dfasf|sdfs|
sdfs|df!@$%%*&!sdffs|
sasdfasfa|dfsdf|#sdfs|

What I need to do is to omit the first character '#' and last character '|' in the output file. So the output will be,

sdfs|dfasf|sdfs|
sdfs|df!@$%%*&!sdffs|
sasdfasfa|dfsdf|#sdfs

Batch script is new to me, but I tried my best and tried these codes,

:: drop first and last char

@echo off > xyz.txt & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in (E:\abc1.txt) do (
set str=%%a
set str=!str:~1!
echo !str!>> xyz.txt
)

and

 @echo off > xyz.txt

setLocal EnableDelayedExpansion

for /f "tokens=1,2 delims= " %%a in (E:\abc1.txt) do (
set /a N+=1
if !N! gtr 2 ( echo %%a >> xyz.txt
)
else ( 
set str=%%a
set str=!str:#=!
echo !str! >> xyz.txt
)
)

As you can see they are not able to produce the required output.

© Stack Overflow or respective owner

Related posts about Windows

Related posts about batch-file