I'm creating a text editor for one application, using richtextbox to change the text. I have to add a text file with openfiledialog, then save that file to an output file.
I'm using this code to save my file
SaveFileDialog^ saveFile1 = gcnew SaveFileDialog;
if (saveFile1->ShowDialog() ==
System::Windows::Forms::DialogResult::OK && saveFile1->FileName->Length > 0)
{
// Save the contents of the RichTextBox1 into the file.
richTextBox1->SaveFile(saveFile1->FileName);
}
but the following string is added to my output file
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\lang1036\fs17
I'd like to remove this from my file, does anyone have a solution?
It looks like the RichTextBox.SaveFile function has a second argument which can be used to specify the format of the file. So instead of calling:
richTextBox1->SaveFile(saveFile1->FileName);
Try calling it like so:
richTextBox1->SaveFile(saveFile1->FileName, RichTextBoxStreamType.PlainText);
And this should save the contents as plain text instead of rich text.
Related
Is there a way to copy (or cut) a file to the Windows clipboard from the command line?
In particular with a batch script. I know how to copy the contents to the clipboard (type file | clip), but this is not the case. I want to have the whole file as I would press Ctrl + C in Windows Explorer.
OK, it seems the easiest way was to create a small C# tool that takes arguments and stores them in the clipboard:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace File2Clip
{
public class App
{
[STAThread]
static void Main(string[] args)
{
List<string> list = new List<string>();
string line;
while(!string.IsNullOrEmpty(line = Console.ReadLine())) list.Add(line);
foreach (string s in args) list.Add(s);
StringCollection paths = new StringCollection();
foreach (string s in list) {
Console.Write(s);
paths.Add(
System.IO.Path.IsPathRooted(s) ?
s :
System.IO.Directory.GetCurrentDirectory() +
#"\" + s);
}
Clipboard.SetFileDropList(paths);
}
}
}
2017 edit: Here's a github repo with both source and binary.
This would place the contents of the file into the clipboard (accomplished by clip.exe).
type \path\to\file|clip
To get the actual file, you'll probably have to resort to some other programming language, like VBScript or PowerShell to access Windows API's. I'm not entirely certain what Explorer puts into the clipboard when you CTRL+C a file. I suspect it uses the notification system to do something more intelligent than put the path to the file there. Depending on the context of the CTRL+V, you'll get something (Explorer, Word) or nothing (Notepad).
I've forever wanted this to use in Emacs, so, inspired by this question, an answer here, and a goodly amount of NIH syndrome, I've written a C version available at
https://github.com/roryyorke/picellif
picellif also handles wildcards (it's not clear to me if rostok's C# version does or not).
copy and move are (some of) the batch commands that copy/paste and cut/paste files, respectively. We don't use the terms paste or cut when dealing with files but if I understand you there is a need to copy a file to another location and to move files to another location.
You can try Swiss File Knife (SFK):
sfk toclip
Copy stdin to clipboard as plain text.
type test.txt | sfk toclip
Copies the content of ASCII file test.txt into the clipboard.
sfk list | sfk toclip
Copies a file listing of the current dir into the clipboard.
sfk fromclip [-wait] [-clear]
Dump plain text content from the clipboard to the terminal.
-wait : block until plain text is available.
-clear: empty the clipboard after reading it.
Example: turn backslashes into forward slashes. Imagine you have the following text open within Notepad:
foo/bar/systems/alpha1.cpp
foo/bar/systems/alpha2.cpp
foo/bar/systems/beta1.cpp
And for some reason you need the first line in a format like this:
foo\bar\systems\alpha1.cpp
Then you may do it this way:
Mark the first line using SHIFT + CURSOR keys.
Press Ctrl + C or Ctrl + Insert to copy it into clipboard
On the Windows command line, run this command (for example, from a batch file):
sfk fromclip +filter -rep x/x\x +toclip
Back in the editor, press Ctrl + V or Shift + Insert, pasting the result from the clipboard.
As you see, the line changed into "foo\bar\systems\alpha1.cpp".
You can use Command Line Copy and Paste Files utilities on Sid's Bytestream.
Hi I'm trying to read a pdf in Ruby, first of all I want to convert it into a txt. path is the path to the PDF, The point is that I get a .txt file empty, and as someone told me is a pdftotext problem, but I don't know how to fix it.
spec = path.sub(/\.pdf$/, '')
`pdftotext #{spec}.pdf`
file = File.new("#{spec}.txt", "w+")
text = []
file.readlines.each do |l|
if l.length > 0
text << l
Rails.logger.info l
end
end
file.close
What's wrong with my code? Thanks!
It's not possible to extract text from every PDF. Some PDF files use a font encoding that makes it impossible to extract text with simple tools such as pdftotext (and some PDF files are even completely immune to direct text extraction with any tool known to me -- in these cases you'll have to apply OCR first to have a chance to extract text...).
So if you test your code with the same "weird" PDF file all the time, it may well happen that you're getting frustrated over your code while in reality the fault lies with the PDF.
First make sure that the commandline usage of pdftotxt works well with a given PDF, then test (and develop further) your code with that PDF.
The problem is you are opening the file in write ("w") mode, whuch truncates the file. You can see a table of file modes and what they mean at http://ruby-doc.org/core-1.9.3/IO.html.
Try something like this, it uses a pdftotext option to send the text to stdout to avoid creating a temporary file and uses blocks for more idiomatic ruby.
text = `pdftotext #{path} -`
text.split.select { |line|
line.length > 0
}.each { |line|
Rails.logger.info(line)
}
You would need to open the txt file with write permission.
file = File.new("#{spec}.txt", "w")
You could consult How to create a file in Ruby
Update: your code is not complete and looks buggy.
Cant say what is path
Looks like you are trying to read the text file to which you intend to write file.readlines.each
spell check length you have it l.lenght
You may want to paste the actual code.
Check this gist https://gist.github.com/4160587
As mentioned, your code is not working because you are reading and writing to the same file.
Example
Ruby code file_write.rb to do the file write operation
pdf_file = File.open("in.txt")
output_file = File.open("out.txt", "w") # file to which you want to write
#iterate over input file and write the content to output file
pdf_file.readlines.each do |l|
output_file.puts(l)
end
output_file.close
pdf_file.close
Sample txt file in.txt
Some text in file
Another line of text
1. Line 1
2. Not really line 2
Once your run file_write.rb you should see new file called out.txt with same content as in.txt You could change the content of input file if you want. In your case you would use pdf reader to get the content and write it to the text file. Basically first line of the code will change.
i am stumbling over one thing:
i am sorting a bunch of files in awk and saving the sorted particles as txt. but now i need to save them as .doc and especially in landscape format. i googled a lot and found out that the only way of doing this is save the file as doc but during creating the file, write these rtf code into file and then write the real content into file.
rtf start-tag code:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
\landscape
\paperw15840\paperh12240\margl720\margr720\margt720\margb720
and rtf close-tag:
}
the close tag will be written after the last line of the file as the last line into the new created file.
my problem is, how can i find the last line of the file inside the awk before coming to END.
this is my code. http://pastebin.com/mfjH4NYY
it is huge code to know what is happenning, but the point is: the fnnID is not available in the END tag, thru this, a new file will be created if i try to append the } char to close the rtf format. can someone help me figure out the clue?
thanks a lot
Let's say you'll have a function write_header(filepath) that will write the RTF header into a file. Make this function record in some global variable all the filepaths it was passed. Then, in your END, loop over these filepaths and write the RTF footer into them.
As for your new "ls -l" question: I don't see why you need to use it.
Here's what I suggested:
function write_header(filepath) {
print "{\\rtf1\\ans .... " >> filepath
tracked[max_header++] = filepath
}
BEGIN {
# You don't have to write the headers in BEGIN. Just make sure it's the
# first thing you write to the files.
write_header("file1.doc")
write_header("file2.doc")
write_header("another_file.doc")
}
END {
# Write the footers.
for(i in tracked) {
print "}" >> tracked[i]
}
}
Is there a way to set a default document type when saving a NEW FILE?
I created several new files and I want to have a default value of .txt when saving a NEW FILE.
Create a new plugin Tools > Developer > New Plugin...
Paste this in:
import sublime, sublime_plugin
class EverythingIsPowerShell(sublime_plugin.EventListener):
def on_new(self, view):
view.set_syntax_file('Packages/PowerShell/Support/PowershellSyntax.tmLanguage')
Save and call it NewTabSyntax.py. New tabs will now default to Powershell.
You can change the syntax to whatever you prefer. To find out the "path" of a particular syntax, simply open a file of that syntax, open the console (View > Show Console) and type:
view.settings().get('syntax')
This plugin does it:
https://github.com/spadgos/sublime-DefaultFileType
seems pretty great.
Edit:
Ok, two things, there currently seems to be a small bug so the text file syntax is not being correctly picked up due to the whitespace in the filename. In addition you need to set the "use_current_file_syntax" to false, (otherwise the new file will default to whatever filetype you have open already when you hit Ctrl-N)... So the fix/workaround is this:
Put the following code in: Packages/User/default_file_type.sublime-settings
{ "default_new_file_syntax": "Packages/Text/Plain_text.tmLanguage",
"use_current_file_syntax": false }
NOTE THE UNDERSCORE.
Next, find the "Plain text.tmLanguage" file and copy and rename it (in the same folder) as "Plain_text.tmLanguage". [be sure to copy/duplicate it, do not just rename it, as it may have dependancies]
Restart, just to be sure, and this should do the trick.
Also note this plugin only works for new files created with Ctrl-N.
Working after these steps:
1.Uninstalled
2.Installed using Package Control
3.Test using default install (type Jave) <-- worked
4.Copy and Renamed file Sublime Text 2\Packages\Text\Plain text.tmLanguage > Sublime Text 2\Packages\Text\Plain_text.tmLanguage
5.Changed file Sublime Text 2\Packages\Default File Type\default_file_type.sublime-settings >
`{ "default_new_file_syntax": "Packages/Text/Plain_text.tmLanguage", "use_current_file_syntax": true }`
-- All working.
I did not need to copy any files into the 'Packages/User' folder
#fraxel _ Thanks for all the help and quick response.
In Cocoa, how can I write a string to a text file without replacing the contents of the file, like writing at the end of the file?
For example the following code:
BOOL written = [data writeToFile:[path stringByAppendingPathComponent:#"conf.txt"] options:NSAtomicWrite error:&error];
The data (string) was written to the text file however it replaced the original contents of the file.
Any suggestions?
Use an NSFileHandle.
First call -[NSFileHandle seekToEndOfFile] to seek to the end of the file.
Then use -[NSFileHandle writeData:] (instead of -[NSData writeToFile:]) to append your data to the end of the file.