Hello,
I'd like to merge multiple variable text files from a directory in one text file.
Any idea if that can be accomplish with a script.
Thank you !
Patrice
Merging variable text file
Moderators: JRL, Dorian (MJT support)
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Yes. Take a look at the AppendFile command in the help file.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Marcus,
Thank you for your reply. this is greatly appreciated. I'll try to look more at the help file before posting.
When I use an application, a new sub-directory is created with a variable number of text file.
How could I use the AppendFile function in a script to:
1) Identify the last created sub-directory.
2) Run the AppendFile function with a conditionnal statement on the the n text files.
Thank you !
Patrice
Thank you for your reply. this is greatly appreciated. I'll try to look more at the help file before posting.
When I use an application, a new sub-directory is created with a variable number of text file.
How could I use the AppendFile function in a script to:
1) Identify the last created sub-directory.
2) Run the AppendFile function with a conditionnal statement on the the n text files.
Thank you !
Patrice
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
First you want to determine the path of the newest subfolder. Use this code to do that:
VBSTART
Function NewestSubFolder(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oSubFolder In oFolder.SubFolders
If DateDiff("s", dPrevDate, oSubFolder.DateLastModified) > 0 Then
sNewestSub = oSubFolder.Path
dPrevDate = oSubFolder.DateLastModified
End If
Next
NewestSubFolder = sNewestSub
End Function
VBEND
VBEval>NewestSubFolder("c:\temp"),folderpath
E.g. If your subfolders are created in c:\theapplication then use:
VBEval>NewestSubFolder("c:\theapplication"),subfolderpath
Now you have the path of the newest sub folder you want to enumerate all text files in there and append them together. Actually it's much easier just to use the DOS copy command to do this as it can handle wildcards and do it one go:
Let>RP_WAIT=1
Run>cmd.exe /c copy %subfolderpath%\*.txt %subfolderpath%\all.txt
This will append ALL txt files in that folder together into a file called all.txt.
However, you say something about a conditional statement on n files. Not sure what you mean by this. Feel free to clarify. An alternative if you just want to work on a few files is to use GetFileList, Separate and AppendFile in a loop. You may also need FileDate. See the help file for examples of each of these commands.
BTW the code used to determine the latest subfolder was a modification of this script in Scripts & Tips:
http://www.mjtnet.com/forum/viewtopic.php?p=8011
And I have posted a new Scripts & Tips entry here:
http://www.mjtnet.com/forum/viewtopic.php?p=11919#11919
VBSTART
Function NewestSubFolder(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oSubFolder In oFolder.SubFolders
If DateDiff("s", dPrevDate, oSubFolder.DateLastModified) > 0 Then
sNewestSub = oSubFolder.Path
dPrevDate = oSubFolder.DateLastModified
End If
Next
NewestSubFolder = sNewestSub
End Function
VBEND
VBEval>NewestSubFolder("c:\temp"),folderpath
E.g. If your subfolders are created in c:\theapplication then use:
VBEval>NewestSubFolder("c:\theapplication"),subfolderpath
Now you have the path of the newest sub folder you want to enumerate all text files in there and append them together. Actually it's much easier just to use the DOS copy command to do this as it can handle wildcards and do it one go:
Let>RP_WAIT=1
Run>cmd.exe /c copy %subfolderpath%\*.txt %subfolderpath%\all.txt
This will append ALL txt files in that folder together into a file called all.txt.
However, you say something about a conditional statement on n files. Not sure what you mean by this. Feel free to clarify. An alternative if you just want to work on a few files is to use GetFileList, Separate and AppendFile in a loop. You may also need FileDate. See the help file for examples of each of these commands.
BTW the code used to determine the latest subfolder was a modification of this script in Scripts & Tips:
http://www.mjtnet.com/forum/viewtopic.php?p=8011
And I have posted a new Scripts & Tips entry here:
http://www.mjtnet.com/forum/viewtopic.php?p=11919#11919
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Marcus,
Thank you for the sample code.
I have run the following script without success.
VBSTART
Function NewestSubFolder(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oSubFolder In oFolder.SubFolders
If DateDiff("s", dPrevDate, oSubFolder.DateLastModified) > 0 Then
sNewestSub = oSubFolder.Path
dPrevDate = oSubFolder.DateLastModified
End If
Next
NewestSubFolder = sNewestSub
End Function
VBEND
VBEval>NewestSubFolder("c:\LifeGuide"),subfolderpath
Let>RP_WAIT=1
Run>cmd.exe /c copy %subfolderpath%\*.txt %subfolderpath%\all.txt
I have a subfolder under c:\LifeGuide with is C:\LifeGuide\2006-05-15 22-57-24 and in that folder there is 12 .txt files. If I run the application (MQ) another subfolder with today's date will be generate and depending of the request, the application can generate x .txt files.
Patrice
Thank you for the sample code.
I have run the following script without success.
VBSTART
Function NewestSubFolder(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oSubFolder In oFolder.SubFolders
If DateDiff("s", dPrevDate, oSubFolder.DateLastModified) > 0 Then
sNewestSub = oSubFolder.Path
dPrevDate = oSubFolder.DateLastModified
End If
Next
NewestSubFolder = sNewestSub
End Function
VBEND
VBEval>NewestSubFolder("c:\LifeGuide"),subfolderpath
Let>RP_WAIT=1
Run>cmd.exe /c copy %subfolderpath%\*.txt %subfolderpath%\all.txt
I have a subfolder under c:\LifeGuide with is C:\LifeGuide\2006-05-15 22-57-24 and in that folder there is 12 .txt files. If I run the application (MQ) another subfolder with today's date will be generate and depending of the request, the application can generate x .txt files.
Patrice
The DOS component in this code doesn't like spaces in file or directory names. You can fix this problem by placing quotes around the names.
Try this:
VBSTART
Function NewestSubFolder(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oSubFolder In oFolder.SubFolders
If DateDiff("s", dPrevDate, oSubFolder.DateLastModified) > 0 Then
sNewestSub = oSubFolder.Path
dPrevDate = oSubFolder.DateLastModified
End If
Next
NewestSubFolder = sNewestSub
End Function
VBEND
VBEval>NewestSubFolder("c:\LifeGuide"),subfolderpath
Let>RP_WAIT=1
Run>cmd.exe /c copy "%subfolderpath%\*.txt" "%subfolderpath%\all.txt"
Try this:
VBSTART
Function NewestSubFolder(Folder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Folder)
dPrevDate = "0"
For Each oSubFolder In oFolder.SubFolders
If DateDiff("s", dPrevDate, oSubFolder.DateLastModified) > 0 Then
sNewestSub = oSubFolder.Path
dPrevDate = oSubFolder.DateLastModified
End If
Next
NewestSubFolder = sNewestSub
End Function
VBEND
VBEval>NewestSubFolder("c:\LifeGuide"),subfolderpath
Let>RP_WAIT=1
Run>cmd.exe /c copy "%subfolderpath%\*.txt" "%subfolderpath%\all.txt"