如何删除使用批处理脚本文件中的第一个和最后一个字符? [英] How to remove the first and last character from a file using batch script?
问题描述
这是我使用复制到输出文件我输入的文件内容。
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
批处理脚本是新的给我,但我尽力了,并试图这些codeS,
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.
现在正在生产的产量如
sdfs|dfasf|sdfs|
dfs|dfsdffs|
asdfasfa|dfsdf|#sdfs|
我不知道为什么字符串 @ $ %% *放大器;!!?
也越来越撕开
推荐答案
下一个脚本,调用文件的 far.vbs ,ANF创建新的文件。
code:
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 - 从文件中删除第一和最后一个字符
创建新文件的 far.vbs 在同一文件夹中的第一个脚本,并粘贴以下code:
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
这篇关于如何删除使用批处理脚本文件中的第一个和最后一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!