Truncated files when copying CSV files using FileSystemObject - vb6

I am helping my son write a program to format files to load into another system. I have done this before with no trouble. Now I get a 13 KB comma delimited text file and I am copying it use FSO to another file with a csv extension. For some reason the new file always stops at the same place, about6 records from the end of the file original file. I thought it might be something with the record after the line where it stopped so I move the record in the file. No change stopped at the same place. So moved the records above where it stopped. Still the same problem. It stops at 13 KB and leaves off about6 records. The only thing I can think of is file size, but is is below the limit of the VB CopyFile. I have imported the original file into Excel no problem. I have done a rename of the file and opened it in Excel no problem. Please give me and idea of where to go next.

I've heard of this happening before with fso, but I haven't heard of a solution (or a cause, for that matter). If you're using vb.net, you can use the my.computer.filesystem.filecopy function instead of fso. If you're using vb6, you can also copy a file this way, although it's not very elegant.
Dim s As String
Open sourcename For Binary As 1
s = String(LOF(1), " ")
Get 1, , s
Close 1
Open destname For Binary As 1
put 1, , s
close 1

Related

Applescript/automator : move all the files listed in a document

I'm completely rebuilding a shared iTunes library and this needs to be team work.
I found a way to work the XML database in Google Drive so that we can all edit the track list simultaneously (>7500 entries). The spreadsheet contains for every song the path to the corresponding file.
Now I need a script to move the tracks listed in that spreadsheet to a common folder, so I can separate the songs we decided to keep from the ones we don't want anymore.
The blueprint I imagined for the code is basically :
Get the paths list (txt, csv, etc. doesn't matter) and store it as
an array.
Rotate through that array and select+move to a common folder each file pointed by the paths.
I'm not expecting any ready-to-use solution, but I would really appreciate some tips or pieces of advice that could make me spare a lot of time.
I also have to admit I have limited knowledge in Mac OS X programming (more used to web and windows environments) and have no experience in Applescripts.
However, I feel that what I'm trying to achieve is pretty straightforward and could help other people as well.
there are just 2 items not clear in your request :
1) what is the file type in which file paths are stored ? I assumed it is a text file
2) which format the paths have ? is it Unix format (like HD/Users/My_User/Desktop/My_Song), or is is a Finder format (like HD:Users:My_User:Desktop:My_Song). I assume it is a finder format
then script bellow asks you to select the text file, read it, ask you to select destination folder and move every file described in text file to the destination folder.
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to choose folder with prompt "Select your destination folder"
set List_files to paragraphs of (read TextFile)
move List_files to My_Folder
end tell
the "move" can be changed to a "copy", if required
Thank you so much to both of you for your answers.
Regarding pbell's questions :
1. I did not specify the format of the file containing the paths because I wasn't sure which ones the script could handle. Now I will make sure it is in a txt format.
2. The paths are actually il a Unix format, but seeing your example it looks pretty easy to parse. Can I just replace every "/" with a ":" ..?
I will try your code this afternoon. Again thanks a lot for caring and for sharing your time and knowledge.
Terence
Works like a charm !
Thank you very much pbell, your code works perfectly. I just had to make a txt file with Finder-formatted paths like so :
Macintosh HD:Users:FirstnameSurname:Desktop:Music:November-7:Season 3:04 Parasite.mp3
Macintosh HD:Users:FirstnameSurname:Desktop:Music:November-7:Season 3:05 Nowhere.mp3
Macintosh HD:Users:FirstnameSurname:Desktop:Music:November-7:Season 3:06 Amber Light.mp3
We spared so much time thanks to you.
Have a great day,
Terence

Ruby - Delete the last character in a file?

Seems like it must be easy, but I just can't figure it out. How do you delete the very last character of a file using Ruby IO?
I took a look at the answer for deleting the last line of a file with Ruby but didn't fully understand it, and there must be a simpler way.
Any help?
There is File.truncate:
truncate(file_name, integer) → 0
Truncates the file file_name to be at most integer bytes long. Not available on all platforms.
So you can say things like:
File.truncate(file_name, File.size(file_name) - 1)
That should truncate the file with a single system call to adjust the file's size in the file system without copying anything.
Note that not available on all platforms caveat though. File.truncate should be available on anything unixy (such as Linux or OSX), I can't say anything useful about Windows support.
I assume you are referring to a text file. The usual way of changing such is to read it, make the changes, then write a new file:
text = File.read(in_fname)
File.write(out_fname, text[0..-2])
Insert the name of the file you are reading from for in_fname and the name of the file you are writing to for 'out_fname'. They can be the same file, but if that's the intent it's safer to write to a temporary file, copy the temporary file to the original file then delete the temporary file. That way, if something goes wrong before the operations are completed, you will probably still have either the original or temporary file. text[0..-2] is a string comprised of all characters read except for the last one. You could alternatively do this:
File.write(out_fname, File.read(in_fname, File.stat(in_fname).size-1))

vbscript to read text files and extract data

i am in need of script to extract number of note-ref_ and #ref_ presence in all html files
my folder structure will be
D:\Count_Test
wherein lot of folders and sub-folder will contain and in each sub-folder will have a ref.html, text.html file will contain note-ref_ and #ref_ text (apart these files, some other files such as xml, txt and imges and css sub-folder will contain)
I need to count for every single file how many times note-ref_ and #ref_ appeared and the results needs to capture in .csv file
can anybody help me by providing solution to extract text into csv file
Suggestions:
Use the Scripting.FileSystemObject (FSO) to walk through files and sub folders to identify the scope of your actions. Alternatively, you could capture the output of DIR /s /b D:\Count_Test*.html.
Once you know the list of files you'll need to open, you should read each of them using the OpenTextFile function of the FSO and loop through each row. When you find what you're looking for, increase some sort of counter - perhaps in an array.
Finally once you've finished collecting the data, you can output your results by once again doing OpenTextFile, but this time opening your CSV file location and writing the data you've collected in the appropriate format.

visual studio able to save state for multiple indivisual files not in solutions?

Often I use Visual studio to edit standalone files (not in a project). So usually I will have like say 6 files opened (3 html files, 1 css file, 2 js files etc)
And the problem is that whenever i close visual studio, I have to locate and re-open all 7 files again (which is a bother really).
Is there a way for us to like save all 7 files in a state file and simply when i open that state file it will open all the 7 files in whatever order I have last saved them as ?
If you have used Notepad++ you will know what I'm talking about, basically when we close Notepad++ and open it the files that were previously opened stay opened, some sort of functionality like this.
Here's two Visual Studio macro methods that should do exactly what you need.
SaveAll() saves all open files and writes a file named status.txt to C: in which all open files are written separated by semicolon (totally independent of project/solution):
Sub SaveAll()
Dim text As String
Dim i As Integer
For i = 1 To DTE.Documents.Count
text += DTE.Documents.Item(i).FullName + ";"
DTE.Documents.Item(i).Save()
Next i
My.Computer.FileSystem.WriteAllText("C:\status.txt", text, False)
End Sub
Next time, when you want to open those files, you simply call OpenAll(). It reads the status file, splits the semicolon separated text, and opens all files:
Sub OpenAll()
'Open status file
Dim text As String
text = My.Computer.FileSystem.ReadAllText("C:\status.txt")
Dim files As String()
files = text.Split(";")
Dim file As String
For Each file In files
If file.Length > 0 Then DTE.ItemOperations.OpenFile(file)
Next
End Sub
It's easy enough to modify this according to your needs (or even add a file dialogue). Thanks for asking this question, it made me focus on the VS macro IDE again and see how powerful it is.
You could just have a temporary or scratch solution for adding these files to, and when you've finished working on them (after days or weeks or whatever) just remove them. Solution files are probably a bit much for this, but you wouldn't have to do any extra work.
Otherwise look at making the recently opened files list a bit longer so they stay in there for longer.
Or use another editor for this purpose. I use Notepad++ for all my non-solution/non-project related files. If you close it without closing the individual files they remain in the editor next time you open it. It's faster and lighter weight than VS too.

XML File missing when we publish the vb.net application as click once

I am having an XML file in the application start up path. The program has to read the xml file. It works fine in the development.but when I publish the Application. The XML file is not copied to the out put directory
I have tried to set the copy to output directory always. but no luck
Have you set the xml file properties to be Content rather than None in visual studio?
It might be better to make it an embedded resource and write it do disk on the target machine if it does not exist, that way users can do a local restore but still edit a local version if required.
Dim read As Stream = Me.GetType().Assembly.GetManifestResourceStream("my-file.xml")
Dim buf() As Byte = New Byte((read.Length) - 1)
Dim file As New FileStream("C:\test.xml", FileMode.Create)
read.Read(buf, 0, buf.Length)
file.Write(buf, 0, buf.Length)
file.Close()
read.close()
disclaimer: It's been a long time since I did VB and I've not tried this code but th egist is there
Also you should consider the file size and maybe read in chunks rather than the whole lot depending on the file size. I've added no error handling and you'll obviously want to see if the file is there first

Resources