gpt4 book ai didi

linux - 将 header 添加到多个 .txt 文件中

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:40 26 4
gpt4 key购买 nike

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

这是我正在使用的代码:

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"

关于linux - 将 header 添加到多个 .txt 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41290680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com