Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add one row of text to top of 300 files 1

Status
Not open for further replies.

slint

Technical User
Joined
Jul 2, 2006
Messages
48
Location
SE
I have 300 files and i want to add strCompN = "intNumber" to the top in all the files how do i acomplish that?

I have manage to add strCompN in one file 300 times but not strCompN 1 time in 300 files.

This is the code i have been working with.

Code:
IntNumber = 10001

Const FOR_READING = 1
Const FOR_WRITING = 2

Do

strFileName = "C:\join.vbs"
strNewContent = ("strCompN = """&IntNumber&"""")

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName,FOR_READING)
strContents = objTS.ReadAll
objTS.Close
Set objTS = objFS.OpenTextFile(strFileName,FOR_WRITING)
objTS.WriteLine strNewContent
objTS.Write strContents

IntNumber = IntNumber + 1

Loop
 
where is your loop for the 300 files,,,where-ever they may be?
 

I must have missed placed them... :)

Code:
Do Until intNumber = 10301

Could this script be of any hand for me? I cant get it to *** ?!?!?

When i run this, it will open all the files.. not something i wont..

Code:
TargetFolder = "C:\test" 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder) 
Set colItems = objFolder.Items

For i = 0 to colItems.Count - 1
    colItems.Item(i).InvokeVerbEx
Next
 
Set objFolder = FSO.GetFolder("c:\test")
For Each aFile In objFolder.Files
'now do you opentextfile stuff for
Wscript.Echo aFile.Name

Next
 
all i get is Objekt required FSO 800A01A8???
 
It still only add all new lines to the first file in the dir. Not one line to all the files in the dir.

What is wrong....??

Code:
Dim objFolder, FSO, strpath, strfilename, intnumber, strContents, strFolder



strPath = "c:\test"

IntNumber = 10001

Const FOR_READING = 1
Const FOR_WRITING = 2

SET objShell = CreateObject("WScript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder(strPath)
For Each aFile In objFolder.Files
   'now do you opentextfile stuff for 

Do Until intnumber = 10300
 strNewContent = ("strCompN = """&IntNumber&"""")  

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(aFile,FOR_READING)
strContents = objTS.ReadAll
objTS.Close
Set objTS = objFS.OpenTextFile(aFile,FOR_WRITING)
objTS.WriteLine strNewContent
objTS.Write strContents

IntNumber = IntNumber + 1
Wscript.Echo aFile.Name

Loop
Next
 
[tt]
Dim objFolder, FSO, strpath, strfilename, intnumber, strContents, strFolder

strPath = "c:\test"

IntNumber = 10001

Const FOR_READING = 1
Const FOR_WRITING = 2

[red]'[/red]SET objShell = CreateObject("WScript.Shell") [green]'what for[/green]
Set Fso = CreateObject("Scripting.FileSystemObject")

Set objFolder = FSO.GetFolder(strPath)
For Each aFile In objFolder.Files
'now do you opentextfile stuff for

[red]'[/red]Do Until intnumber = 10300
strNewContent = ("strCompN = """&IntNumber&"""")

[red]'[/red]Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = [blue]Fso[/blue].OpenTextFile(aFile[blue].path[/blue],FOR_READING)
[blue]on error resume next
strContents=""[/blue]
strContents = objTS.ReadAll
[blue]on error goto 0[/blue]
objTS.Close
Set objTS = [blue]Fso[/blue].OpenTextFile(aFile[blue].path[/blue],FOR_WRITING)
objTS.WriteLine strNewContent
objTS.Write strContents
[red]objTS.close[/red]
IntNumber = IntNumber + 1
Wscript.Echo aFile.Name
[blue]if IntNumber>10300 then exit for[/blue]
[red]'[/red]Loop
Next
[/tt]
 
tsuji, U are the best, U deserve a Star!

U Rock!!

It works like a charm..

Thnx!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top