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