nisus.com
https://www.nisus.com/forum/

Find text, copy and create new file
https://www.nisus.com/forum/viewtopic.php?f=17&t=6281
Page 1 of 1

Author:  Matze [ 2016-01-31 23:01:36 ]
Post subject:  Find text, copy and create new file

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

Author:  phspaelti [ 2016-02-01 00:19:37 ]
Post subject:  Re: Find text, copy and create new file

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 58 times

Author:  Matze [ 2016-02-01 02:01:48 ]
Post subject:  Re: Find text, copy and create new file

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.

Author:  Matze [ 2016-02-01 02:08:54 ]
Post subject:  Re: Find text, copy and create new file

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?

Author:  phspaelti [ 2016-02-01 02:46:38 ]
Post subject:  Re: Find text, copy and create new file

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

Author:  Matze [ 2016-02-05 00:37:00 ]
Post subject:  Re: Find text, copy and create new file

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

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/