批量删除字符串中的字符 [英] Batch To Remove Character From A String

查看:129
本文介绍了批量删除字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很基本,但是有没有办法创建一个批处理,以从txt文件的字符串中删除char. ?

this may seem basic but Is there a way to create a batch to remove char from a string from a txt file. ?

如果我已经在.txt文件中包含此文件

If I already have this inside the .txt file

2005060.png  
2005070.png  
2005080.png  
2005090.png

因此,有一种方法可以创建一个批处理文件,该文件将在最后删除.png以便仅在新的.txt文件中显示该文件.

so is there a way to create a batch file that will remove the .png at the end to show only this in a new .txt file

2005060  
2005070  
2005080  
2005090

谢谢您的帮助! :)

推荐答案

您可以按照以下命令脚本进行操作:

You can do it as per the following command script:

@setlocal enableextensions enabledelayedexpansion
@echo off
set variable=2005060.png
echo !variable!
if "x!variable:~-4!"=="x.png" (
    set variable=!variable:~0,-4!
)
echo !variable!
endlocal

这将输出:

2005060.png
2005060

神奇的路线当然是:

set variable=!variable:~0,-4!

这将删除最后四个字符.

which removes the last four characters.

如果您的文件testprog.in带有一行,例如:

If you have a file testprog.in with lines it it, like:

2005060.png
1 2 3 4 5      leave this line alone.
2005070.png
2005080.png
2005090.png

您可以稍作修改:

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "delims=" %%a in (testprog.in) do (
    set variable=%%a
    if "x!variable:~-4!"=="x.png" (
        set variable=!variable:~0,-4!
    )
    echo.!variable!
)
endlocal

输出:

2005060
1 2 3 4 5      leave this line alone.
2005070
2005080
2005090

请记住,它不会输出空行(尽管它会 执行空行).

Just keep in mind that it won't output empty lines (though it will do lines with spaces on them).

这可能不是问题,具体取决于输入文件中允许的内容.如果有问题,我的建议是开始使用 CygWin

That may not be a problem depending on what's allowed in your input file. If it is a problem, my advice is to get your hands on either CygWin or GnuWin32 (or Powershell if you have it on your platform) and use some real scripting languages.

这篇关于批量删除字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆