【问题标题】:How to remove everything after the first word?如何删除第一个单词后的所有内容?
【发布时间】:2017-09-29 03:57:50
【问题描述】:

我想删除此文本文档每一行中第一个单词之后的所有内容。它适用于简单的字符删除,但当我尝试删除“|”时不起作用文本文件。

我正在尝试使用以下代码:

setlocal enableextensions disabledelayedexpansion

set /p txtfile=Text File Name: 
    Echo.
set /p "search=Search for: "
    Echo.
set /p "replace=Replace to: "

    for /f "delims=" %%i in ('type "%txtfile%.txt" ^& break ^> "%txtfile%.txt" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%txtfile%.tmp" echo(!line:%search%=%replace%!
        endlocal
    )

    ren "%txtfile%".tmp "%txtfile%".txt 

pause

但出现以下错误:

【问题讨论】:

  • 请将示例数据插入为文本而不是图像,以便回答者可以简单地复制它们以测试他们的代码...

标签: string file batch-file text


【解决方案1】:

问题是管道字符| 由于变量%search% 的立即扩展而试图执行。

一种可能的解决方法是使用允许引号的临时变量分配 (set "line=..."),因此管道不再被识别,然后在 echo 命令行中仅使用延迟扩展 - 比如这个:

setlocal EnableExtensions DisableDelayedExpansion

set /P txtfile="Text File Name: "
echo/
set /P search="Search for: "
echo/
set /P replace="Replace to: "

for /F "delims=" %%i in ('type "%txtfile%.txt" ^& break ^> "%txtfile%.txt"') do (
    set "line=%%i"
    setlocal EnableDelayedExpansion
    set "line=!line:%search%=%replace%!"
    >>"%txtfile%.txt" echo(!line!
    endlocal
)

pause
endlocal

另一种解决方案是对!search!!replace! 也使用延迟扩展;为此,您不能直接嵌套延迟扩展,因此您需要使用for /F 循环来进行嵌套——像这样:

setlocal EnableExtensions DisableDelayedExpansion

set /P txtfile="Text File Name: "
echo/
set /P search="Search for: "
echo/
set /P replace="Replace to: "

for /F "delims=" %%i in ('type "%txtfile%.txt" ^& break ^> "%txtfile%.txt"') do (
    set "line=%%i"
    setlocal EnableDelayedExpansion
    for /F "delims=" %%j in ("!search!=!replace!") do (
        >>"%txtfile%.txt" echo(!line:%%j!
    )
    endlocal
)

pause
endlocal

您也可以通过> 重定向整个for /F 循环,而不是单独重定向(>>)每个输出行,但是您需要使用与您阅读的文件不同的文件 - 看这个(仅显示上面的第一个变体):

setlocal EnableExtensions DisableDelayedExpansion

set /P txtfile="Text File Name: "
echo/
set /P search="Search for: "
echo/
set /P replace="Replace to: "

> "%txtfile%.tmp" (
    for /F "usebackq delims=" %%i in ("%txtfile%.txt") do (
        set "line=%%i"
        setlocal EnableDelayedExpansion
        set "line=!line:%search%=%replace%!"
        echo(!line!
        endlocal
    )
)

move /Y "%txtfile%.tmp" "%txtfile%.txt" > nul

pause
endlocal

这种方法在性能方面要好得多,尤其是对于巨大的输入文件。

【讨论】:

  • 您好!它工作得几乎完美,但很难保存最终结果,请参阅:imgur.com/9PWtq0a
  • 我猜这个错误是由ren引起的,因为文件%txtfile%.txt已经存在;实际上您不需要重命名文件,只需将循环中的>>"%txtfile%.tmp" 替换为>>"%txtfile%.txt"...
  • 谢谢,我解决了!问题在于他将.txt的信息传递给同一个“.txt”,所以在保存时,该文件已经存在。所以我只需将信息写入“.tmp”文件,然后再转到“.txt”文件。
  • 查看我编辑的答案,我提供了另一种改进的文件处理方法...
【解决方案2】:

了解了之前的几个代码,终于用这段代码解决了问题:

@Echo off

setlocal EnableExtensions DisableDelayedExpansion

set /P txtfile="Text File Name: "
echo/
set /P search="Search for: "
echo/
set /P replace="Replace to: "

for /F "delims=" %%i in ('type "%txtfile%.txt" ^& break ^> %txtfile%.tmp') do (
    set "line=%%i"
    setlocal EnableDelayedExpansion
    set "line=!line:%search%=%replace%!"
    >> %txtfile%.tmp echo(!line!
    endlocal
)

ren %txtfile%.tmp CleanText.txt

pause
endlocal

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    相关资源
    最近更新 更多