View Single Post
  #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