English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello,

I recieved a perfect answer from someone which was, for %f in (*.txt) do copy %f+mytext.txt

Now how can I change the above to only append mytext.txt to all .txt files that don't currently have mytext.txt in them?

mytext.txt contains "Written by John Smith"

2007-04-22 15:27:46 · 3 answers · asked by Netiad 2 in Computers & Internet Programming & Design

3 answers

This just takes one extra step from what you already have. You can search for the string "written by john smith" in all of the text files. If that "find" _fails_, then you can execute the addition of text to the file.

for %f in (*.txt) do find /i "Written by John Smith" "%f" || copy %f+mytext.txt %f /y

The double pipe symbol || (that's the other symbol on the backslash key) basically says "if the command before me fails, then do the command after me".

So, if the "find" command doesn't find your text in a text file, it fails, and the copy command takes place.

NOTE: If you need to do this for all text files throughout your subdirectories, then you need to get a little more advanced on the "for /f" options, like this, (all on one line)

for /f "tokens=*" %f in ('dir *.txt /b /s') do find /i "Written by John Smith" "%f" || copy "%f"+mytext.txt "%f" /y

This will search your entire subdirectory structure for *.txt files and try to find "Written by John Smith" in them. If it doesn't find that text in the file, then it will append your mytext.txt file to them.

Finally, you don't actually need to do a copy command to tag your files. You could just echo your text into the file, like this, (all on one line),

for /f "tokens=*" %f in ('dir *.txt /b /s') do find /i "Written by John Smith" "%f" || echo Written by John Smith>>"%f"

2007-04-23 14:18:20 · answer #1 · answered by Kevin 7 · 4 0

To keep it simple, i would suggest to locate the files that already contain the text in a given path, then move them to a temp directory and execute the command you already know on the remaining files (this will avoid having the text appended twice). Final step, return the files from the temp dir to the original dir.

2007-04-22 15:42:00 · answer #2 · answered by Cat 9 6 · 0 2

use grep to check if the text is already in the file (use tail first if you only want to look at the end), if grep returns unsuccessful, use echo or cat to add the text to the file

2007-04-22 17:00:28 · answer #3 · answered by undercoloteal 3 · 0 2

fedest.com, questions and answers