assignging text file contents to variable - vb.net

Stack!
I'm currently trying to code a neat little function in Visual Basic that deletes my entire file system for my program depending on the contents of one text file.
If my text file contains the number 0 by itself, my file system will be deleted. If it contains a 1 by itself, it will not delete the file system and carry on with its pathway of execution.
the problem I'm having is reading the file, then assigning the text file's contents to a variable (string), which then is used in an if statement to decide whether it deletes (then immediately recreates) the file system, or keeps it. Ideally I'd be using this primarily for when the user installs. By default, it is set to 0 so that it clears any stored content, then after creating my directory, the number turns to 1 so that it does not reset my programs content directory.
Here's my code. The first line is giving me difficulties -
Could not find file 'C:\Users\110fa_000\documents\visual studio 2013\Projects\Novation_Launchpad_Emulator\Novation_Launchpad_Emulator\bin\Debug\0'.
fileReader = File.ReadAllText(My.Resources.FirstTimeUse)
If fileReader = "0" Then
FirstTimeUse = True
End If
If you can help me, that'd be absolutely brilliant. I don't think there's any more information I can give sadly, but everything regarding the function, purpose, and error is there.

Related

Creating a file with name given in code in LiveCode

I'm trying to build a program that gets users name at first. This user names are kept in a text file. After user logins, according to the user's name, I want user to be lead his/her specific informations. I figured out that I can only do it with a file that is created when he sign up for an account which I direct him with my sign up button in Livecode. While he/she create his account I want to create a specific file for his/her. Can you help me with it please?
PS: I don't want to do it with a database right now. I just want to learn how to create a file without a specific name like
put specialFolderPath("documents")&"/userLoginCridentials.txt" into tFile put URL("file:"&tFile) into myFile
Instead of this "userCridentials.txt" I want something user can create with his own name :)
Having a little problem to understand your question. Are you targeting mobile or desktop? Are you having problem saving or reading the data?
If saving is you problem
On desktop you can use:
ask file "Save file as:"
then you get the filename back in it so you can use:
if it is not empty then
# We have a complete file path in 'it'
put it into tFile
put tData into url ("file:" & tFile)
end if
If you targeting mobile and would like to save into the specialFolderPath("Documents")you can get the filename from a field and then save to that file. E.g. if you have a field named 'fileName' you can use something like:
put tData into url("file:" & specialFolderPath("Documents") & "/" & field "fileName"
Of course you should do some error checking to ensure that a user don't overwrite existing files without at least asking for permission, etc.
You can of course use a variable instead of a field...
If reading data is your problem
On desktop you can use:
answer file "Open File:"
Same as above but you now read data instead:
if it is not empty then
# We have a complete file path in 'it'
put it into tFile
put url ("file:" & tFile) into tData
end if
on mobile you probably would like to present a list with the user-created files. In LiveCode you can list all files in the defaultFolder with the files. But you must set the defaultFolder to the folder you want to list.
set the defaultFolder to specialFolderPath("Documents")
put the files into tFiles
Now tFiles contains every file in that folder and you can filter it, display it in a list etc. E.g:
filter tFiles with "*.txt"
put tFiles into
If your problem is how to remember the "current" file name
Whenever you restart your app every variable is reset. So everything you want to remember between runs needs to be saved before your app quits. And for that you need a predefined filename. SO then your procedure will be:
Read in the predefined file.
Grab the file name from within that file
Read the file
If your problem is something else
Sorry, then I misunderstood your question...

VBA Word original text on revised range on readonly files

I am creating a VBA tool to record (for QA purposes) revisions of translated files.
I can read both original and revised text, whitch I do paragraph by paragraph.
But I run into problems every time the file cannot be written to (open by someone else or wasn't closed correctly).
Since the macro will run for a long time, this is an issue. I could work around it, but my client probably will not be able to follow the procedure.
Is there no way to just read the original text without having to modify the file?
It really feels that there should be a way. I serache for a way to copy the range into a dummy and then rever revisions on that new one, but no such luck.
I'm really just doing
set wrdApp=CreateObject("Word.Application")
set wrdDoc=wrdApp.Documents.Open(filePath)
for each par in wrdDoc.Paragraphs
if par.Range.Revisions.Count <> 0 then
dim original, revised as String
revised=par.Range.Text
par.Range.Revisions.RejectAll
original=par.Range.Text
ProcessRevision original, revised
End if
next
Thanks!
Answering it myself, bottom line is it can't be done. I run into some other issues and had to undo changes after rejecting revisions, but I still can't do it unless I have write permissions.
My solution to make sure the files were not open somewhere was to copy the files prior to reading.
It's frustrating that a clearly read operation (getting the original text) need write permission.
If I ever find another solution, I'll be sure to post here.

Filecopy vs name function? Differences?

I have found two different ways to copy single files in Excel using VBA. One is file copy:
FileCopy (originalPath), (pathToCopyTo)
The other is name:
Name (originalPath) As (pathToCopyTo)
Any differences in performance/intended use/functionality? I haven't been able to notice anything, but interested to know!
They actually do very different things.
FileCopy will actually make a new copy of the file at the location of pathToCopyTo, leaving the original file intact.
Name renames the original file. The effectively moves the file if you supply a different file path. (A copy/paste action.) So, the original file won't be where you found it, it will be in the new location.
For more information see the following MSDN documentation:
Name Statement
FileCopy Statement
As for performance, Name will preform better because it's not necessary for the OS to physically copy the data on disk. It will just update the file's location in the lookup tables. Incidentally, this is also the reason Cut/Paste file operations are faster than Copy/Paste file operations.

Append multiple PDF files

I have about 600 PDF files that I want to add a single disclaimer page to the beginning of each of them. So, I need to find a way to merge two PDF documents where one file is always the same and and comes first and the second file is changing.
Please let me know how I can do this.
Thanks!
I found this:
http://gotofritz.net/blog/howto/joining-pdf-files-in-os-x-from-the-command-line/
So you could do something like this in a shell script:
#!/bin/bash
page1="disclaimer.pdf"
for f in {a.pdf b.pdf c.pdf}; do
"/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o "$f" "$page1" "$f"
done
You can wrap this in an Applescript or automator workflow it you like.
Combine an unknown PDF with a known PDF
Automator can do this. You would save it as an application, so that you can drop the second file onto it. Your steps would be something like:
Get Specified Finder Items: Add the known PDF document here, the one that is “always the same and comes first”.
Combine PDF Pages
Move Finder Items: To: Combined Files. This will create a randomly-named file in the folder “Combined Files” which you will need to create.
If this Automator workflow is saved as an application, you can drop your second, "changing" file onto the application icon. The workflow will combine the two, putting the named file first.
Loop this workflow for each dropped file
In order to do this on multiple files at once, you will need to create a second workflow that loops through each dropped file and calls the above workflow. This second workflow is also created as an application. It has only one step:
Run Workflow: choose the workflow created in the previous step, and process items in batches of 1 at a time using 1 workflow.
Give the files a more usable name
That’s the basics. The obvious drawback is that all of the files have random names. That can be fixed by grabbing the original filename into a variable, and saving the new, combined document using that name in your new folder.
First, add four new steps to the top of the first workflow, in front of “Get Specified Finder Items”:
Run AppleScript
Set Value of Variable: name the variable filepath.
Run AppleScript
Set Value of Variable: name the variable filename.
Get Value of Variable: filepath
The first two steps save the full path to the dropped PDF. The second two steps get just the filename portion. The fifth new step pops the full path back into the workflow so that it can be combined with the known disclaimer.
Set the AppleScript in step 1 to:
on run {input, parameters}
return input
end run
Set the AppleScript in step 3 to:
on run {input, parameters}
tell application "Finder"
set filename to name of file input
end tell
end run
Then, add a step at the end to rename the file. After Move Finder Items add *Rename Finder Items. Choose “Name Single Item”, “Full name to” and then drag the variable filename up to the text box.

vb.net writing files of specific size

I am creating an application whereby I need to write files (many, many of them). However, I don't want to write files that are too big because the program that consumes them will crash :)
Essentially, I would like to create files that are no larger than a specific size (possibly 5MB). My data is of CSV format and I will be writing line by line or into some stringbuilder to create the file I need.
Question: Is there a way to write data and create a set of files that are no larger than a set size?
I assume you are creating CSV rows 1 at a time.
If you are using a StreamWriter on the file (hopefully you are, no need to hold it all in memory):
Create the new record (a string of CSV items).
Look at the StreamWriter's BaseStream.Length property (Caveat - You need to use AutoFlush to make that property correct with the data that's been written previously).
Add your stream position, new record's length, and your line separator's length (usually 1 or 2, depending on Cr, Lf, or CrLf), and see if it exceeds your threshold.
If no, write the line feed, the record, and continue. If yes, close your StreamWriter and open a new one for a new file, write the record, and continue.