A Windows XP help forum. PCbanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » PCbanter forum » Windows 10 » Windows 10 Help Forum
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Adding one line to a directory full of text files



 
 
Thread Tools Rate Thread Display Modes
  #16  
Old June 9th 17, 12:25 AM posted to alt.comp.os.windows-10
Mayayana
external usenet poster
 
Posts: 6,438
Default Adding one line to a directory full of text files

"Chaya Eve" wrote

|I need to add a line to a directory full of text files with the same
| extension (which isn't .txt but I don't know if that matters).
|
| Is there an easy way in Windows 10 to run a command that does this?
|

It's never just simple. And there's no method
that can do it without some kind of code. What
you need is too specific for that. The following code
can be pasted into Notepad and saved as a file
with .vbs extension. Then drop the folder
onto that file. As written, it will add "okeydoke"
to any file that doesn't have it. Just change
"okeydoke" to your line.

If you often have projects like this you might
find it worthwhile to learn VBScript. Very handy.

Caveats: * You must have permission to run
the script. * Since files might have carriage returns
at the end, the script removes any it finds before
checking for the last line. It then adds one carriage
return plus the line, if necessary. (Without that
check the last line would be "" if there's a carriage
return.)

Watch out for line returns created by your
newsreader program.

'-- begin script --------------------------
Dim FSO, TS, s1, Arg, oFol, oFils, oFil, sPath, sLine

sLine = "okeydoke" 'enter line here.
Set FSO = CreateObject("Scripting.FileSystemObject")
Arg = WScript.Arguments(0)

Set oFol = FSO.GetFolder(Arg)
Set oFils = oFol.Files
For Each oFil in oFils
sPath = oFil.Path
Set TS = FSO.OpenTextFile(sPath, 1)
s1 = TS.ReadAll
TS.Close
Do
If Right(s1, 2) = vbCrLf Then
s1 = Left(s1, len(s1) - 2)
Else
Exit Do
End If
Loop
If Right(s1, Len(sLine)) sLine Then
s1 = s1 & vbCrLf & sLine
Set TS = FSO.OpenTextFile(sPath, 2)
TS.write s1
TS.Close
End If
Set TS = Nothing
Next
Set oFils = Nothing
Set oFol = Nothing
Set FSO = Nothing
MsgBox "Done."




Ads
  #17  
Old June 9th 17, 12:34 AM posted to alt.comp.os.windows-10
Mayayana
external usenet poster
 
Posts: 6,438
Default Adding one line to a directory full of text files

Note - the above is "air code", written
quickly and barely tested. Copy your folder
and operate on the copy, to be on the
safe side. That's always a good idea when
you're doing mass operations. Otherwise,
if it fails, then you need to write another
script to undo it.


  #18  
Old June 9th 17, 01:23 AM posted to alt.comp.os.windows-10
Paul[_32_]
external usenet poster
 
Posts: 11,873
Default Adding one line to a directory full of text files

Chaya Eve wrote:
On Thu, 8 Jun 2017 16:07:01 -0400, Big Al wrote:

One fly in the ointment is how you view/sort the directory as to what is
"last" line!


I must have confused everyone so I will clarify that I just one to add a
single line to each file in a directory of a given file type.

So if there are a hundred files in any given directory that are called
file1.abc
file2.abc
file3.abc
etc

I just want to add one line to each of those files.
For each file that ends with the extension abc
add this one command to that file if that command isn't already there
"do this"

What's the best way to do that on Windows 10?


https://stackoverflow.com/questions/...e-copy-command

COPY FILE1+FILE2=FILE1

FILE2 would contain the single line of text you want appended.

First you test that with test files, before doing anything more.

*******

If the folder contains a mix of files and folders, simply looping
over the directory may not be accurate enough.

You can use the "dir" command to make a file list.

You can use Microsoft Word to do "rectangular copy and paste".

Alternately, you can use "dir output.csv" then use Excel
to import that file, then duplicate the first colume, to make
two columns. You could crudely make a batch file format, using
Excel as your formatter.

If you don't have Office, you can use free LibreOffice.

Don't split this compute job between Linux and WIndows,
because of line endings. Stick to the target platform,
and then all the line endings will be consistent.

*******

You could write a one-liner in Command Prompt.

You need to find a looping construct.

https://stackoverflow.com/questions/...ing-a-for-loop

Notice how the experienced user/programmers in there,
use "echo %i" for their first loop structure test. This
avoids destroying the directory by making a mistake. That's
a way to see whether the kind of loop you made, is "bulletproof"
or not. If the filenames in the directory contain spaces in
the name, extra care is needed in constructing the looping
construct, so it doesn't "blow a fuse".

Once you've debugged the various components of your project,
you will eventually be able to emit a "one-liner" into Command Prompt,
or by using Excel as a cheep text editor, make a batch file with
no looping construct at all. By curating the file list, you
can prune out the files for which adding the one line, would
be inappropriate.

To read the command line options for "copy", try

copy /?

I know you can do this, VPNUser. We don't have to write
this one for you, cause it's simple. The journey of a thousand
miles, starts with a single step.

Now you know how I can do copy/paste programming. You don't
need to know a lot, except to Google, then copy, paste, compile,
test, and gradually build up a project until it meets your needs.
There is an amazing amount of worked examples out there, just
ready to be pasted.

HTH,
Paul
  #19  
Old June 9th 17, 09:18 AM posted to alt.comp.os.windows-10
Andy Burns[_6_]
external usenet poster
 
Posts: 1,318
Default Adding one line to a directory full of text files

Paul wrote:

COPY FILE1+FILE2=FILE1
FILE2 would contain the single line of text you want appended.


I don't think I've seen anyone address one part of the O/P, that this
"single line" should only be appended if it isn't already the *last*
line of each file, the uses of find.exe/findstr.exe will find it
anywhere within the files.
  #20  
Old June 9th 17, 10:08 AM posted to alt.comp.os.windows-10
Paul[_32_]
external usenet poster
 
Posts: 11,873
Default Adding one line to a directory full of text files

Andy Burns wrote:
Paul wrote:

COPY FILE1+FILE2=FILE1
FILE2 would contain the single line of text you want appended.


I don't think I've seen anyone address one part of the O/P, that this
"single line" should only be appended if it isn't already the *last*
line of each file, the uses of find.exe/findstr.exe will find it
anywhere within the files.


My "guess" as to the application, is these files arrive as a group
via download. They are missing the line. (I have no idea what is
in the line and don't want to know). Then, the magic program appends
the same line to the end of each file. The only thing you might
not want attacked in the folder, is some batch file. So some
selection might be required.

I tried to make the approach seem as little like a programming
project as possible. I wouldn't be doing it via "copy", and
would have some other approach. As I'd want a little more certainty
about what I was patching (like yourself). But I like programming
projects anyway, so salt to taste.

The OP really needs some sort of programming language,
as the OP has had multiple questions like this.

Even the "file" command in the bash shell, could be
used to sort out the file types (without relying on
a file extension). I'm only guessing that the files
in question, would be recognized by "file". But then
you'd be writing a Bash script of some sort. Since this
is the Win10 group, we have Command Prompt, PowerShell,
and Bash at our disposal. An embarrassment of riches.

Paul
  #21  
Old June 9th 17, 01:49 PM posted to alt.comp.os.windows-10
Mayayana
external usenet poster
 
Posts: 6,438
Default Adding one line to a directory full of text files

"Andy Burns" wrote

| I don't think I've seen anyone address one part of the O/P, that this
| "single line" should only be appended if it isn't already the *last*
| line of each file,

See my script below. This is really not a job
for DOS or inventive command lines to make
use of utilities.


  #22  
Old June 9th 17, 02:46 PM posted to alt.comp.os.windows-10
Jonathan N. Little[_2_]
external usenet poster
 
Posts: 1,133
Default Adding one line to a directory full of text files

Mayayana wrote:
"Chaya Eve" wrote

|I need to add a line to a directory full of text files with the same
| extension (which isn't .txt but I don't know if that matters).
|
| Is there an easy way in Windows 10 to run a command that does this?
|

It's never just simple. And there's no method
that can do it without some kind of code. What
you need is too specific for that. The following code
can be pasted into Notepad and saved as a file
with .vbs extension. Then drop the folder
onto that file. As written, it will add "okeydoke"
to any file that doesn't have it. Just change
"okeydoke" to your line.

If you often have projects like this you might
find it worthwhile to learn VBScript. Very handy.

Caveats: * You must have permission to run
the script. * Since files might have carriage returns
at the end, the script removes any it finds before
checking for the last line. It then adds one carriage
return plus the line, if necessary. (Without that
check the last line would be "" if there's a carriage
return.)

Watch out for line returns created by your
newsreader program.

'-- begin script --------------------------
Dim FSO, TS, s1, Arg, oFol, oFils, oFil, sPath, sLine

sLine = "okeydoke" 'enter line here.
Set FSO = CreateObject("Scripting.FileSystemObject")
Arg = WScript.Arguments(0)

Set oFol = FSO.GetFolder(Arg)
Set oFils = oFol.Files
For Each oFil in oFils
sPath = oFil.Path
Set TS = FSO.OpenTextFile(sPath, 1)
s1 = TS.ReadAll
TS.Close
Do
If Right(s1, 2) = vbCrLf Then
s1 = Left(s1, len(s1) - 2)
Else
Exit Do
End If
Loop
If Right(s1, Len(sLine)) sLine Then
s1 = s1 & vbCrLf & sLine
Set TS = FSO.OpenTextFile(sPath, 2)
TS.write s1
TS.Close
End If
Set TS = Nothing
Next
Set oFils = Nothing
Set oFol = Nothing
Set FSO = Nothing
MsgBox "Done."





You could do this simply with a one-liner in bash on Linux ;-)

for f in $(grep -L "do this" *.xxx); do echo "do this" $f; done


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
  #23  
Old June 9th 17, 03:27 PM posted to alt.comp.os.windows-10
Mayayana
external usenet poster
 
Posts: 6,438
Default Adding one line to a directory full of text files

"Jonathan N. Little" wrote

| You could do this simply with a one-liner in bash on Linux ;-)
|
| for f in $(grep -L "do this" *.xxx); do echo "do this" $f; done
|

I'm not a bash user, but I don't see
where you checked for trailing carriage
returns or added the line where it's missing.
It looks to me like your code will merely
list all files that contain "do this", which
has nothing to do with the request. (If
I'm mistaken then perhaps you could explain.)

That's the problem with command line.
It's limited. Well, that's the second problem.
The first is that it's tedious. The VBScript
(or software) takes more work to write, but
it can be reused and it's functionality is
endlessly adaptable. I keep dozens of VBScripts
on my desktop and use them daily. One fixes
line returns in files from *Nixers. One converts
DOCs to text. One cleans up obfuscated URLs.
One cleans out all my TEMP folders. And so on.
I wrote them once and don't have to ever do
it again.

Then of course there's the problem that
ugly Linux command lines can't edit files on
a Windows computer. And of course there's
the problem that life is just too short for
regular expressions. But your code certainly
does look clever. I'll grant you that.


  #24  
Old June 9th 17, 06:00 PM posted to alt.comp.os.windows-10
Gene Wirchenko[_2_]
external usenet poster
 
Posts: 496
Default Adding one line to a directory full of text files

On Fri, 9 Jun 2017 10:27:57 -0400, "Mayayana"
wrote:

"Jonathan N. Little" wrote

| You could do this simply with a one-liner in bash on Linux ;-)
|
| for f in $(grep -L "do this" *.xxx); do echo "do this" $f; done
|

I'm not a bash user, but I don't see
where you checked for trailing carriage
returns or added the line where it's missing.
It looks to me like your code will merely
list all files that contain "do this", which
has nothing to do with the request. (If
I'm mistaken then perhaps you could explain.)


For all files f in the list generated by a grep for all files
*.xxx which do NOT (the -L option) contain "do this", add a line "do
this" to the end of the file.

echo something somefile
adds the something to the end of somefile. If it were just instead
of , the file would be overwritten.

No, it does not check for a trailing newline. It might not be an
issue.

[snip]

Sincerely,

Gene Wirchenko
  #25  
Old June 9th 17, 08:11 PM posted to alt.comp.os.windows-10
Paul[_32_]
external usenet poster
 
Posts: 11,873
Default Adding one line to a directory full of text files

Mayayana wrote:
"Jonathan N. Little" wrote

| You could do this simply with a one-liner in bash on Linux ;-)
|
| for f in $(grep -L "do this" *.xxx); do echo "do this" $f; done
|

I'm not a bash user, but I don't see
where you checked for trailing carriage
returns or added the line where it's missing.
It looks to me like your code will merely
list all files that contain "do this", which
has nothing to do with the request. (If
I'm mistaken then perhaps you could explain.)

That's the problem with command line.
It's limited. Well, that's the second problem.
The first is that it's tedious. The VBScript
(or software) takes more work to write, but
it can be reused and it's functionality is
endlessly adaptable. I keep dozens of VBScripts
on my desktop and use them daily. One fixes
line returns in files from *Nixers. One converts
DOCs to text. One cleans up obfuscated URLs.
One cleans out all my TEMP folders. And so on.
I wrote them once and don't have to ever do
it again.

Then of course there's the problem that
ugly Linux command lines can't edit files on
a Windows computer. And of course there's
the problem that life is just too short for
regular expressions. But your code certainly
does look clever. I'll grant you that.


These files are mechanically generated by a server.
The format, line ending, white space, should all be the same.

If you're concerned about the last line not having
any line ending, your addon file can include
a blank line followed by the extra line.

There are plenty of problems that require "programming",
but just adding the same line to the end of each file,
isn't particularly demanding.

I would program it in the OPs case, because some of the
stuff he is doing, already uses scripts. And the additional
step might just be added to an existing script. Having
to run a separate step, is "clunky". That's the main
incentive for not using "copy".

Paul
  #26  
Old June 10th 17, 12:18 AM posted to alt.comp.os.windows-10
Mayayana
external usenet poster
 
Posts: 6,438
Default Adding one line to a directory full of text files

"Gene Wirchenko" wrote

| For all files f in the list generated by a grep for all files
| *.xxx which do NOT (the -L option) contain "do this", add a line "do
| this" to the end of the file.
|
| echo something somefile
| adds the something to the end of somefile. If it were just instead
| of , the file would be overwritten.
|
| No, it does not check for a trailing newline. It might not be an
| issue.
|
Nor does it check that it's in the last line,
apparently. But it is more efficient than I
would have guessed. Thanks.


  #27  
Old June 10th 17, 04:21 AM posted to alt.comp.os.windows-10
Jonathan N. Little[_2_]
external usenet poster
 
Posts: 1,133
Default Adding one line to a directory full of text files

Mayayana wrote:
"Jonathan N. Little" wrote

| You could do this simply with a one-liner in bash on Linux ;-)
|
| for f in $(grep -L "do this" *.xxx); do echo "do this" $f; done
|

I'm not a bash user, but I don't see
where you checked for trailing carriage
returns or added the line where it's missing.
It looks to me like your code will merely
list all files that contain "do this", which
has nothing to do with the request. (If
I'm mistaken then perhaps you could explain.)


-L, --files-without-match print only names of FILEs containing no match

So it just created a list of filename of files which do not have "do this".

If you want to check for ending return then

egrep -L "do this$" *.xxx

Or you could just inject a leading return if possible blank lines would
not matter. OP's parameters seemed a bit loose...

do echo -e "\ndo this"


That's the problem with command line.


Not really.

for f in $(grep -L "do this" *.xxx); do [ -n "$(egrep 'do this$') ] &&
echo -e "\ndo this" || echo "do this" $f; done

Still a one-liner.

It's limited. Well, that's the second problem.
The first is that it's tedious. The VBScript
(or software) takes more work to write, but
it can be reused and it's functionality is
endlessly adaptable.

Says you. You do know you can make modular shell scripts, and include
the modules? I do it all the time.

I keep dozens of VBScripts on my desktop and use them daily. One
fixes line returns in files from *Nixers. One converts DOCs to text.
One cleans up obfuscated URLs. One cleans out all my TEMP folders.
And so on. I wrote them once and don't have to ever do it again.

I admin a number of servers and workstations with my collection of shell
scipts. Your point?
Then of course there's the problem that ugly Linux command lines
can't edit files on a Windows computer.

Then of course there's the problem that ugly VBScripts can't edit files
on any other computers but Windows.

Actually not true. First thing I do is install grep, sed, awk, perl on
Windows.

And of course there's the problem that life is just too short for
regular expressions.

I clear indication that you do not understand regular expressions.

But your code certainly does look clever. I'll grant you that.

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
  #28  
Old June 10th 17, 04:34 AM posted to alt.comp.os.windows-10
Jonathan N. Little[_2_]
external usenet poster
 
Posts: 1,133
Default Adding one line to a directory full of text files

Mayayana wrote:
"Gene Wirchenko" wrote

| For all files f in the list generated by a grep for all files
| *.xxx which do NOT (the -L option) contain "do this", add a line "do
| this" to the end of the file.
|
| echo something somefile
| adds the something to the end of somefile. If it were just instead
| of , the file would be overwritten.
|
| No, it does not check for a trailing newline. It might not be an
| issue.
|
Nor does it check that it's in the last line,
apparently. But it is more efficient than I
would have guessed. Thanks.



Sure it could if that is the need just a modification to use:

tail -n1


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
  #29  
Old June 10th 17, 02:32 PM posted to alt.comp.os.windows-10
Mayayana
external usenet poster
 
Posts: 6,438
Default Adding one line to a directory full of text files

"Jonathan N. Little" wrote

| Then of course there's the problem that ugly Linux command lines
| can't edit files on a Windows computer.

| Then of course there's the problem that ugly VBScripts can't edit files
| on any other computers but Windows.
|

Actually I installed the WSH with WINE. Works
fine. But I've never gone further than to just test
things to see how well I might adapt to Linux. And
there's an issue of copyright. If enough people
wanted to use Windows libraries in Linux then
Microsoft might turn to legal enforcement to
stop it.

| Actually not true. First thing I do is install grep, sed, awk, perl on
| Windows.
|

Ah. To each their own, I guess. God created
Perl fans, as the saying goes, so they must
serve some purpose.



  #30  
Old June 10th 17, 02:44 PM posted to alt.comp.os.windows-10
Mr. Man-wai Chang
external usenet poster
 
Posts: 1,941
Default Adding one line to a directory full of text files

On 9/6/2017 4:28 AM, Chaya Eve wrote:
And let's say each of those files contains a script of the format:
(line 1) do one thing
(line 2) do another
(line 3) do something else
etc

All I want to do is *append* a new command to each of those files.
That way, each file has one more line.

For example, before I run the command, this is the content of file1.abc:
c:\ type file1.abc
do one thing
do another
do something else

This would be the desired result of all the files after I run the command:
c:\ type file1.abc
do one thing
do another
do something else
do this


For this part of your problem, you could do this:

echo "do this" file1.abc

--
@~@ Remain silent! Drink, Blink, Stretch! Live long and prosper!!
/ v \ Simplicity is Beauty!
/( _ )\ May the Force and farces be with you!
^ ^ (x86_64 Ubuntu 9.10) Linux 2.6.39.3
不借貸! 不詐騙! 不援交! 不打交! 不打劫! 不自殺! 請考慮綜援 (CSSA):
http://www.swd.gov.hk/tc/index/site_...sub_addressesa
 




Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off






All times are GMT +1. The time now is 02:17 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright 2004-2024 PCbanter.
The comments are property of their posters.