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
Find text, copy and create new file
Re: Find text, copy and create new file
Hello Matze,
here is a macro that will do this kind of thing:
There are a few things I am unsure of.
here is a macro that will do this kind of thing:
Code: Select all
# 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
- 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 929 times
philip
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.
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.
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?
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?
Re: Find text, copy and create new file
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".Matze wrote:When I want to name the bits after the first number I just have to replace the name-part, right?
So then to grab a bit and put it into a variable you use a parenthesis like this:
Code: Select all
… (?<variable_name> … ) …
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: Select all
[[:digit:]]{1,5}\n[[:digit:]]\n.+\n(.+\n)?\n?A\:.+\n\n
Code: Select all
(?<name>[[:digit:]]{1,5})\n[[:digit:]]\n.+\n(.+\n)?\n?A\:.+\n\n
Code: Select all
(?<first_num>[[:digit:]]{1,5})\n(?<second_num>[[:digit:]])\n(?<first_line>.+)\n(.+\n)?\n?(?<A_code>A\:.+)\n\n
Hope that is clear
philip
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
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