简体   繁体   English

命令行:从批处理文件中的回显数据中删除文本

[英]Command Line: removing text from echo data in a Batch File

In a batch file, I am trying to figure out how to remove characters at front/back of an command echo'd data. 在一个批处理文件中,我试图找出如何删除命令回显数据的前面/后面的字符。 Not specifically with a directory, but for sake of example. 不是专门与目录有关,而是为了举例。 Is there a way to strip characters at front or back of data as it is written to a file? 在将数据写入文件时,是否有办法去除数据前面或后面的字符?

EXAMPLE: dir /b *.txt >dir.txt 示例: dir /b *.txt >dir.txt

This result would save the filenames of a directory: filename.txt thisistest.txt hardquestion.txt 此结果将保存目录的文件名:filename.txt thisistest.txt hardquestion.txt

but I would like to strip out last 4 characters of the filenames so it would echo to dir.txt only the filename: filename thisistest hardquestion 但我想删除文件名的最后4个字符,以便仅将文件名回显到dir.txt :filename thisistest hardquestion

>dir.txt (for %%a in (*.txt) do echo %%~na)

for command will iterate over the indicated set of files and for each one, a reference to the file is stored in the replaceable parameter ( %%a in this case) and the code after the do clause executed. for命令将遍历指示的文件集,对于每个文件集,对该文件的引用存储在可替换参数(在本例中为%%a )中,并在do子句执行后存储代码。

In this case, the code executed for each of the files is echo %%~na , echo the name of the file referenced by %%a 在这种情况下,对每个文件执行的代码为echo %%~na ,echo %%a引用的文件名

All the for command output is redirected to the final file. 所有的for命令输出都重定向到最终文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理-如何将带有set / P命令的行回显到另一个文件 - BATCH - How to echo a line with a set /P command to another file 如何在批处理命令中包括一行文本文件? - How to include a line of a text file in a batch command? 在从批处理文件到另一个批处理文件的特定行中插入命令 - Insert a command into a specific line from batch file to another batch file 批处理:回显一些文本并阅读其余部分 - Batch: echo some text and read the rest of the line 我需要使用批处理文件在回显关闭的情况下向文本文档回显十行。 文件已保存,但每一行都显示ECHO已关闭。 我的批处理文件是- - I need to echo ten lines to a text document using a batch file with echo off. The file gets saved, but each line says ECHO is off. my batch file is- 为批处理文件中的单个命令打开回显 - Turn on echo for a single command in a batch file Windows批处理编程:从文件中仅删除完全匹配的行 - windows batch programming: Removing only exactly matching line from the file 如何使用批处理命令从文本文件中删除 CRLF 和新行 - How to remove CRLF and a new line from text file using batch command 批处理回显命令无法转义 - Batch echo command to file doesn't escape 批处理文件ECHO在PUSHD命令后无法正常工作 - Batch file ECHO not working after PUSHD command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM