将标头添加到多个.txt文件中 [英] Adding a header into multiple .txt files

查看:108
本文介绍了将标头添加到多个.txt文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个批处理文件,该文件将在数百个.txt文件内容的开头的新行中添加文本"Write In",而不会删除任何现有文本.我在这里发现了一些对我不起作用的东西.有人有建议吗?

I need a batch file that will add the text "Write In" in a new line at the beginning of the content of hundreds of .txt files without removing any existing text. I found something on here that did not work for me. Anyone have suggestions?

这是我正在使用的代码:

This is the code I was working with:

for /r %%a in (*.txt) do (
    echo ---- %%a before ----
    type "%%a"
    echo --------------------

    echo Write In > "%%a.tmp"
    type "%%a" >> "%%a.tmp"
    del "%%a"
    move "%%a.tmp" "%%a"

    echo ---- %%a after ----
    type "%%a"
    echo --------------------
)
pause

什么也没做

推荐答案

我很可能会这样:

rem // Create temporary header file:
> "head.txt" echo Write In
rem // Iterate all text files in current directory:
for %%F in ("*.txt") do (
    rem /* Combine header and currently iterated text file into a temporary file;
    rem    there cannot arise any file name conflicts (like temporary files becoming
    rem    iterated also unintendedly, or temporary files overwriting files to handle),
    rem    because the extension of the temporary files differ from the text files: */
    copy /B "head.txt"+"%%~F" "%%~F.tmp"
    rem // Overwrite original text file by temporary file, erase the latter:
    move /Y "%%~F.tmp" "%%~F"
)
rem // Erase the temporary header file:
del "head.txt"

这篇关于将标头添加到多个.txt文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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