windows 如何使用批处理脚本从文件中删除第一个和最后一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2505857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:10:48  来源:igfitidea点击:

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

windowsbatch-file

提问by InfantPro'Aravind'

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 !?

我不知道为什么字符串!@$%%*&!也被撕裂了!?

采纳答案by Victor

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

Code:

下一个脚本,调用文件far.vbs,然后创建新文件。

代码:

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.vbsin the same folder as first script, and paste the following code:


far.vbs— 从文件中删除第一个和最后一个字符。在与第一个脚本相同的文件夹中
创建新文件far.vbs,并粘贴以下代码:

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

回答by lugte098

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

回答by ghostdog74

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

批处理从来都不是文件处理和文本操作的强项。如果你有条件,你可以从 GNU win32下载sed,然后使用这一行。

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