0

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

It did nothing

Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45
  • I used a file path that contained about 6 test text files named test1-test6 in place of (*.txt) – StillLearning1987 Dec 22 '16 at 19:52
  • 1
    Thank-you for the suggested edits J.Baoby, – StillLearning1987 Dec 22 '16 at 20:41
  • 3
    `copy text1+%%a %%a.bak & del %%a & ren %%a.bak %%a` Use quotes on things that may contain spaces. –  Dec 22 '16 at 20:44
  • My pasted code looked exactly like yours when I copied it, but the way it pasted into the comment box made it look like that. I realized that I should not use a filepath and that I should place this batch file in the same folder as all of the .txt file I want to edit. **When I placed this over a network and tried to run it on a shared folder (even with administrative access) it would not run. I had to remote into the machine and place it in the folder to make it work. **The batch file will edit all .txt files in all subfolders. I was not aware of this so users should be careful. – StillLearning1987 Dec 22 '16 at 20:49
  • Thank-you Noodles! – StillLearning1987 Dec 22 '16 at 20:56
  • Remove the `/r` to only do current directory. See my CMD Cheat Sheet http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 –  Dec 22 '16 at 21:02
  • Most problems come from not using full paths. Try `for %%a in ("c:\windows\*.txt") do (`. The quotes handle paths with spaces. –  Dec 22 '16 at 21:04
  • @Noodles, in general I totally agree with you, but here, `for /R` already returns full paths in `%%a`... – aschipfl Dec 23 '16 at 11:33
  • Besides the unintended recursive operation, I cannot see any error in the code; are you aware that `for /R` opints to the current working directory in case no root directory is given after `/R`, and that the current working directory is not the same as the parent directory of the batch script? – aschipfl Dec 23 '16 at 11:35
  • Thank-you Everyone, This has allowed me to edit a large amount of files. I went with the original code as aschipfl mentioned and just made sure to temporarily move the one text file that I did not want to edit and the one subfolder containing additional text files I did not want to edit. It worked well. This is an old menu program and we needed to add a "write in" option to in order to avoid an error message that appeared from selecting a blank space instead of a space with text. – StillLearning1987 Dec 23 '16 at 14:49
  • I just updated this batch file using the advice from noodles successfully in my test environment. This will be helpful in the future when I edit this to make additional additions to the program. Thank-you aschipfl for explaining what /R will do! – StillLearning1987 Dec 23 '16 at 14:51

3 Answers3

1

I would most probably do it like this:

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"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

I found a way that is not very clean and will take a while for bigger files, but it works out for me:

@echo off
for /r "%~dp0" %%f in (*.txt) do (
echo Processing file %%f
>"%%~f.2" (
echo "Text to append here"
type "%%~f"
)
del "%%~f"
ren "%%~f.2" "%%~nxf"
)
pause

Loops recursively through all .txt -files in the batch-files directory.
Creates a new file with the name oldName.txt.2 and files it with the text to add and the rest of the oldfiles content.
Deletes the old file and renames the new file to the name of the old one.
The addition of .2 to the file ending is needed to make sure it does not get processed again by the loop.

Of course you can add multiple lines by multiplying the echo lines.
You can as well add text to the end of the file like this with adding the echo lines after the type line.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
0

You can simply use the Linux Sed command to insert a header into a file.

sed -i '1s/^/This is my header\n/' filename
e.g. sed -i '1s/^/Write In\n/' myfile.txt

This can be applied to multiple files under a directory:

For .txt files

for file in *.txt
do
  sed -i '1s/^/This is my header\n/' $file
done

For CSV files

for file in *.csv
do
  sed -i '1s/^/This is my header\n/' $file  
done
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45