Merging variable text file

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
stinger
Newbie
Posts: 11
Joined: Thu May 11, 2006 2:50 pm

Merging variable text file

Post by stinger » Tue May 16, 2006 11:03 am

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

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 16, 2006 11:14 am

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?

stinger
Newbie
Posts: 11
Joined: Thu May 11, 2006 2:50 pm

Post by stinger » Tue May 16, 2006 11:51 am

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

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 16, 2006 12:15 pm

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
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

stinger
Newbie
Posts: 11
Joined: Thu May 11, 2006 2:50 pm

Post by stinger » Tue May 16, 2006 3:00 pm

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

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue May 16, 2006 3:31 pm

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"

stinger
Newbie
Posts: 11
Joined: Thu May 11, 2006 2:50 pm

Post by stinger » Tue May 16, 2006 3:43 pm

Marcus,

Tested with success.

Your help was greatly appreciated !

Patrice

:wink:

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts