Writing to several files in multiple folders

December 9, 2009 at 21:20:28
Specs: Windows XP
Hiya. I've been pulling my hear for several hours now trying to figure out how to get my thing working. This site's been to great help; unforunately I still haven't figured it out.

So here goes. I've got several folders located on C:\texts. There are hundreds of subfolders with text files. What I want to do is make a batch that will open up these files in order to paste additional text.

I played around for some time now and came up with this approach.

FOR /R C:\texts\ %%G IN (*.txt) DO dir /b /s %%G >>text.txt

This would list all my texts into one file. Next what I did was playing around with the FOR command, hoping I would make up something that would read the lines from text.txt, opening up every text file and ultimately pasting the additional text.

I was unsuccessful, of course. I hope you guys can be of some help :-)

Thanks in advance.


See More: Writing to several files in multiple folders

Report •


#1
December 10, 2009 at 00:39:21
opening up every text file and ultimately pasting the additional text

You need to be much more specific. Where does the new text come from(is it a file, is it fixed...) and where should it go? Maybe the end of the file? Does it replace other text?

Do the files have empty lines? If so is it important they are retained? Do any of the following characters appear in the files: "!%&^)|;*=?<>?

I can't guarantee that it's possible in batch, but it may not be practical depending on the circumstances.


Batch Variable how to


Report •

#2
December 10, 2009 at 01:31:41
Hi. Thanks for quick reply. I see that I worded myself a little strangely there.

I simply(difficultly?) want a batch that at the end of every text file in every subfolder, adds a line saying "made by abikan"

I had some luck with this code:

dir /b *.txt > text.txt
for /f "tokens=*" %%H in (text.txt) do call :sub1 %%H
::cls
goto :eof

:sub1

ren "%1" "workFILE"
echo testing >> "workFILE"
ren "workFILE" "%1"
goto :eof

This worked nicely when it was just one folder, but as soon as text.txt got full paths, it wouldn't work anymore. So that's it; I wish you could help me with a code like that, but that it worked working with full paths. That is, I want something similar as the code above except that it works from the rootfolder and down.

Many thanks again for hopefully great help :)!


Report •

#3
December 10, 2009 at 01:56:29
Actually that is a pretty easy prospect:

Simply change the text "x:\" to the directory you want to start
in and it will process every file that ends in .txt in the
directory and all of it's sub directories. I took the liberty
of adding a blank line before adding "made by abikan"
I also capitilised to "Made by Abikan".


Make sure when you replace "x:\" with your directory
that is ends in a back slash.

Before you run it make sure you want to do it because it's
a lot harder to undo than it is do.


I haven't tested this because I don't want all of my text
files messed with, but I'm fairly sure it will be fine.

::change your start dir here
::make sure yoy add the trailing backslash!
::it can be a path like "x:\somedir\" (don't include the quotes)
set startdir=x:\



for /f "delims=" %%a in (' 2^>nul dir /b/s "%startdir%*.txt"') do (
    >> "%~fa" echo.
    >> "%~fa" echo Made by Abikan
)


Batch Variable how to


Report •

Related Solutions

#4
December 10, 2009 at 01:59:19
Why are you renaming the file, then appending to it, then renaming it back to its original name, instead of simply appending to the file?

By the way, the reason your script failed is that

ren "%1" "workFILE"

keeps "workFILE" in the same directory as the original folder, and the next line
echo testing >> "workFILE"

works on a file in the current folder.

You can fix that by using MOVE instead of REN, then the file will both move directory and be renamed. But why bother, just append straight to the file.


Report •

#5
December 10, 2009 at 11:49:17
Thanks the both of you! My problem is solved. :-)!!

edit:
for /f "delims=" %%a in (' 2^>nul dir /b/s "%startdir%*.txt"') do (
>> "%~fa" echo.
>> "%~fa" echo Made by Abikan
)

I had to change:
>> "%~fa" echo.
>> "%~fa" echo Made by Abikan

to
>> "%%~fa" echo.
>> "%%~fa" echo Made by Abikan

for it to work properly.

Thanks again the both of you!


Report •

#6
December 10, 2009 at 12:38:23
Oops!

At least you figured it out; have a good one.


Batch Variable how to


Report •


Ask Question