简体   繁体   English

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

[英]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.

相关问题 从Windows批处理文件中的字符串中删除第一个和最后一个字符 - Remove first and last character from a String in a Windows Batch file 我想使用批处理脚本/cmd 从目录中的每个 txt 文件中删除最后一个字符 - I want to remove last character from each txt file in a directory using batch script/cmd 如何使用批处理脚本删除文件的第一行(标头)和最后一行(标头) - How to delete first line(header) and last line(trailer) of a file using batch script 使用批处理脚本解析文本文件并从每行中删除前 2 个字符 - Parse a text file using batch script and remove the first 2 characters from each line 从批处理文件中的变量中删除最后一个字符 - Removing last character from a variable in a Batch file 如何使用批处理文件从文本文件的第一行中删除换行(LF) - How to remove Linefeed (LF) from first row in a text file using a batch file 删除Windows批处理中文件的最后一个换行符 - Remove last new line character of a file in windows batch 使用批处理删除文件的最后n个字符 - remove last n characters of a file using batch 如何通过批处理脚本从TXT文件的最后一行提取最后一个单词 - How to extract the last word from the last line of a TXT file through a batch script 如何使用批处理从文件的第一行提取字符串? - How to extract a string from the first line of a file using batch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM