-
Hi,
I really need help with this one! There are at least 2 or more batch files but with unknown names. Now, what I need to do is copy all the texts from each and every one of them and save them in one file.
Any help or suggestion would be very much appreciated.
-
You could do this, its a bit messy and probaly could be done in less code...
How about
opening each file and writing it to the one then open the next and writing that at the endo fht ethe one etc...
open outfile for output as #1 (open for sequential input)
'open first file
open file1 for input as #2 (open for sequential input)
do
if not eof(2) then
Line Input #2, buffer
print #1,buffer
endif
loop
close #2
'open next file
open file2 for input as #2 (open for sequential input)
do
if not eof(2) then
Line Input #2, buffer
print #1,buffer
endif
loop
close #2
etc etc
ending with ...close #1
DocZaf
{;->
-
Hi, thanks for the reply.
But, I do not have the file name/s. That is my problem. All I know is there are 2 or more files with the same "known" extention but "unknown" file names. The file names will be created by the user but the extentions are the same.
I really like to know if that is possible at all with VB.
Thanks again for your time.
-
This code will take all the .bat files in the current directory and make one .bat file called "myNewFile.Bat". Now even i am impressed that this works.;)
Code:
Dim fs, f, f1, fc
Dim myReadFile, myNewFile
Dim strFileRead As String
Set fs = CreateObject("Scripting.FileSystemObject")
'the new file made up of all the others
Set myNewFile = fs.CreateTextFile("myNewFile.Bat", True)
Set f = fs.GetFolder(CurDir)
'get a list of files
Set fc = f.Files
For Each f1 In fc
'if this is a .bat file
If Right$(f1.Name, 3) = "bat" Then
'get the file
Set myReadFile = fs.OpenTextFile(f1.Name, 1)
'loop to write each line
While Not myReadFile.AtEndOfStream
strFileRead = myReadFile.ReadLine
myNewFile.WriteLine (strFileRead)
Wend
'close file
myReadFile.Close
End If
Next
myNewFile.Close
Set f = Nothing
Set f1 = Nothing
Set fc = Nothing
Set fs = Nothing
-
Its refreshing to see someone use the FSO! Nice one!
-
Thank You! Thank You!
I really appreciate all for your time on this question specially "lain17" with his code.
I just came back from work and I have not had a chance to try it but I am positive that this is it.
Thank You again.
-
Simpler way??
What about old plain DOS command copy?
--------
Copy *.bat YourFileName
--------
Not so elegant but simple, not?
-
While your in dos why not just pipe it
print *.bat >> MYFile.Bat
I take it that your not planning on running the batch file steel?
Zaf Khan
-
When I saw the last one, I just sat back for a few minutes! I couldn't say anything! Oh, brother!!!!! I think I need a vacation!
Well, of course it works!
Thank you all, one more time.
-
Hi da_silvy,
Your suggestion would work if I knew the filenames but I only know the file extentions. So, I cannot use "file1 = "c:\file1.bat" and etc., unfortunately. That is the problem. I am going to try Zaf Khan's VB code suggestion as well and will update you guys.
Thanks for your time.
-
sorry, here is a better way :)
but you need a Common Dialog COntrol called cdlg1
Code:
Dim file1, file2, TargetFile, myString, myFile1, myFile2 As String
On Error GoTo ending:
cdlg1.CancelError = True
cdlg1.DialogTitle = "Open File 1"
cdlg1.Filter = "Batch Files|*.bat"
cdlg1.FilterIndex = 1
cdlg1.ShowOpen
file1 = cdlg1.FileName
cdlg1.DialogTitle = "Open File 2"
cdlg1.ShowOpen
file2 = cdlg1.FileName
cdlg1.DialogTitle = "Save As"
cdlg1.ShowSave
TargetFile = cdlg1.FileName
Open file2 For Input As #1 'open 2nd source file
Do Until EOF(1) 'Repeat until all the file has been loaded
Input #1, myString 'get the current line
myFile2 = myFile2 & vbCrLf & myString 'add it to the "file" string
Loop
Close #1
Open file1 For Input As #1 'see above etc. etc.
Do Until EOF(1)
Input #1, myString
myFile1 = myFile1 & vbCrLf & myString
Loop
Close #1
Open TargetFile For Output As #1
Print #1, myFile1 & vbCrLf
Print #1, myFile2
Close #1
Exit Sub
ending:
MsgBox "Unfortunately there was an error!", vbCritical, "Error"
does this one work better? it lets the user choose the source 1, source 2 and the target :)