Reply to topic  [ 6 posts ] 
Find text, copy and create new file 
Author Message
User avatar

Joined: 2004-12-14 07:54:49
Posts: 156
Location: Düsseldorf Germany
Hi there,

I've got a document with thousands of textblocks as follows:

([[:digit:]]{1,5}\n[[:digit:]]\n)(.+\n)(.+\n)?(\n?)(A\:.+\n)\n

I'd like NW to find every textblock, copy it, open a new file, paste the copied text into that new file, name the new file (filename is first line of text), save it and close it.
So that in the end I have got thousands of documents.

Can someone please help me with this?

Thanks and best regards, Matze


2016-01-31 23:01:36
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Hello Matze,

here is a macro that will do this kind of thing:

Code:
# Useful file saving info
$encoding = 'UTF-8'
$ext = 'txt'

# Locate a Folder to save the bits
$savePath = '~/Desktop'
$savePath = Choose Folder $savePath, 'Make Save Location'
prompt 'The files will be saved here', $savePath

# Find all bits to save. Quit if nothing found
$sourceDoc = Document.active
$bits = $sourceDoc.text.findAll '([[:digit:]]{1,5}\n[[:digit:]]\n)(.+\n)(.+\n)?(\n?)(A\:.+\n)\n', 'Ea'
if ! $bits.count
Exit 'No appropriate text found in this file'
end

# Sandbox access. Sufficient? Necessary?
File.requireAccessAtPath $savePath

foreach $bit in $bits
# Create a file name for saving
$bit.substring.find '([[:digit:]]{1,5}\n[[:digit:]]\n)(?<name>.+)\n(.+\n)?(\n?)(A\:.+\n)\n', 'E$'
$fileName = $savePath.filePathByAppendingComponent $name
$fileName = $fileName.filePathByAppendingExtension $ext
# Save the data to the file
File.writeStringToPath $bit.substring, $fileName, $encoding
end


There are a few things I am unsure of.
  • Note how I am taking the file name from your find expression. Is that the correct bit that you want? If those bits are not unique, this macro will overwrite earlier files with the same name.
  • I added a line for the Sandbox access. I hope this works, and that it only asks for access at most once. You may want to test on a small sample first.
  • At the beginning I added a place where you can choose the extension for the saved files, and for the encoding
  • This is only saving the content. The formatting will be dumped. If you want to save the formatting, this would have to be changed slightly. I picked this method, since I think it will be fastest. Let me know if it is not what you want.


Attachments:
Matze_Save_Bits.nwm [4.46 KiB]
Downloaded 59 times

_________________
philip
2016-02-01 00:19:37
Profile
User avatar

Joined: 2004-12-14 07:54:49
Posts: 156
Location: Düsseldorf Germany
Hi Philip,

thanks a lot for your quick help.
The Macro finds the first bit and creates one file with this bit in it.
But it doesn't do so with all the following bits.


2016-02-01 02:01:48
Profile WWW
User avatar

Joined: 2004-12-14 07:54:49
Posts: 156
Location: Düsseldorf Germany
Sorry, it has been my fault: searchpattern only matches one of six test-bits.

So IT WORKS, GREAT! Thank you!

PS When I want to name the bits after the first number I just have to replace the name-part, right?


2016-02-01 02:08:54
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Matze wrote:
When I want to name the bits after the first number I just have to replace the name-part, right?


To get bits out of a find expression into a variable, you can use the '$' option. So you should see that I added a '$' to the options on the second find expression. Note that this only works for "Find" not for "Find All".

So then to grab a bit and put it into a variable you use a parenthesis like this:
Code:
… (?<variable_name> … ) …

The variable name can be any single word you want. The variable will have the same name with a '$' at the front.

Then you make sure that only the part you want in the variable is inside the parentheses. By the way your original find expression was fine, but it has a lot of parentheses, and most of those are not needed in the macro. So here is the expression with all the unnecessary parentheses removed:
Code:
[[:digit:]]{1,5}\n[[:digit:]]\n.+\n(.+\n)?\n?A\:.+\n\n


To get the first 1–5 digits into a variable with name '$name', you add parentheses like this:
Code:
(?<name>[[:digit:]]{1,5})\n[[:digit:]]\n.+\n(.+\n)?\n?A\:.+\n\n


You can use multiple parentheses like this at the same time, with different names, and put the different bits into different variables.
Code:
(?<first_num>[[:digit:]]{1,5})\n(?<second_num>[[:digit:]])\n(?<first_line>.+)\n(.+\n)?\n?(?<A_code>A\:.+)\n\n

Which will end up putting the four different bits into the variables '$first_num', '$second_num', '$first_line', and '$A_code'.

Hope that is clear

_________________
philip


2016-02-01 02:46:38
Profile
User avatar

Joined: 2004-12-14 07:54:49
Posts: 156
Location: Düsseldorf Germany
Dear Philip,

sorry for the late reply and thanks again for your help and your explanations.
I have saved them in my database for further use.
What would I be without your generous macros over all these years?
If you ever need some help in german (translations, lecture or alike), please let me know. It will be a pleasure for me to pay back that way.

Best, Matze


2016-02-05 00:37:00
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software