[英]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
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 !? 也被撕裂了!
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
Next script, call file far.vbs , anf create your new file. 下一个脚本,调用文件far.vbs ,然后创建新文件。
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. far.vbs-从文件中删除第一个和最后一个字符。
Create new file far.vbs in the same folder as first script, and paste the following code: 在与第一个脚本相同的文件夹中创建新文件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
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.
如果您喜欢的话,可以从GNU win32下载sed ,然后使用此衬垫。
C:\test>sed "/#/{s/^#//;s/|$//}" file
sdfs|dfasf|sdfs
sdfs|df!@$%%*&!sdffs|
sasdfasfa|dfsdf|#sdfs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.