views:

579

answers:

3

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
)

As you can see it is not able to produce the required output.

The output now being produced is like

sdfs|dfasf|sdfs|
dfs|dfsdffs|
asdfasfa|dfsdf|#sdfs|

I don't know why the string !@$%%*&! is also getting ripped !?

+2  A: 

I can't debug my code right now, but try:

:: drop first and last char

@echo off > xyz.txt
@echo off > xyz2.txt
@echo off > xyz3.txt

setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (E:\abc1.txt) do (
set /a N+=1
echo %%a >> xyz.txt
echo %%b >> xyz2.txt
echo %%%c >> xyz3.txt
lugte098
btw, this is what the output I am getting :: `~1,-1~1,-1~1,-1`
infant programmer
i've just edited my code, try it again
lugte098
Ok, i did some more tweaking, it sould work now
lugte098
plz give me some feedback on what kind of output my code produces, then i'll adapt the code accordingly
lugte098
well. This is the output now .. "ECHO is off.#sdfs|dfasf|sdfs|sdfs|dfsdffs|"
infant programmer
@lugte098, its ok .. pls take your time,
infant programmer
try this one....
lugte098
@lugte098, nope, same as previous o/p.
infant programmer
im curious what this piece of code gives you as output. Does it give you 3 seperate file, with each 1 line of code?
lugte098
@lugte098, output is one file, the content is "ECHO is off.[new_line] #sdfs|dfasf|sdfs| [new_line] sdfs|dfsdffs|
infant programmer
@lugte098, Implemented code from @B7ackAnge7z, thanx for the interaction. +1
infant programmer
+2  A: 

Hi,
Next script, call file far.vbs, anf create your new file.

Code:

set OldFile=E:\abc1.txt
set NewFile=E:\xyz.txt
echo. > "%NewFile%"
start far.vbs "%NewFile%" "%OldFile%"


far.vbs — delete first and last char from file.
Create new file far.vbs in the same folder as first script, and paste the following code:

Set OldFile = CreateObject("Scripting.FileSystemObject")
Set rdOldFile = OldFile.OpenTextFile(Wscript.Arguments(1), 1)
oContent = rdOldFile.ReadAll
rdOldFile.Close

Set lOldFile = CreateObject("Scripting.FileSystemObject")
Set lrdOldFile = OldFile.OpenTextFile(Wscript.Arguments(1), 1)
oLen = len(lrdOldFile.ReadAll)
lrdOldFile.Close

oData = oContent
oData = Right(oContent, oLen-1)
oData = Left(oData, oLen-2)

Set NewFile = CreateObject("Scripting.FileSystemObject")
Set fData = NewFile.OpenTextFile(Wscript.Arguments(0), 2)
fData.WriteLine (oData)
fData.Close
B7ackAnge7z
you mean .. should I add this to some other piece of code? I am a newbie in Batch script .. I hardly understand something about it. pls post all working code. Thank you.
infant programmer
I updated the answer.
B7ackAnge7z
infant programmer
Script removes text between !someText!, because ! is used by variables. Now I changed code script. Try it. Does work fine.
B7ackAnge7z
@B7ackAnge7z, works all fine.. thanx :-) btw I just want to confirm, does this script work if I don't have .Net or Visual-Studio, installed?
infant programmer
Yes, of course will work. Regards...
B7ackAnge7z
@B7ackAnge7z, oh! thank you very much :-)
infant programmer
+1 for a good technique, otherwise, I had come to conclusion, that its not possible.
infant programmer
I am glad, that could help you. Best regards ;)
B7ackAnge7z
+2  A: 

batch is never a strong point for file processing and text manipulation. If you have the luxury, you can download sed from GNU win32, then use this one liner.

C:\test>sed  "/#/{s/^#//;s/|$//}" file
sdfs|dfasf|sdfs
sdfs|df!@$%%*&!sdffs|
sasdfasfa|dfsdf|#sdfs
ghostdog74