Consulting

Results 1 to 19 of 19

Thread: create a .txt file

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Posts
    61
    Location

    create a .txt file

    Simple process I guess. All i want is to create a text file, name it, then save it.


    I can open notepad using the shell command but i cant save the little fella afterwards.

    Many thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not just create a simple Excel file and save it as a text file?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Nov 2007
    Posts
    61
    Location
    I want to keep the size and contents of the .txt to a minimum ie. 0 or 1 bytes . I know this seems picky but I am restricted with the amount of space available.

  4. #4
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    what exactly do you wish to do from VBA that the file is created with not more than 0 or 1 bytes?

    Is it just creating a txt file from Excel without data in it, or is it a text file with data not more than 0 or 1 bytes?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, I don't understand how that impacts my proposed solution.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Nov 2007
    Posts
    61
    Location
    sry my explanation is a bit pants. If you re-name a excel spreadsheet to .txt the size of the .txt is above 1 byte. if you create a a .txt file from fresh and name it test.txt its size is 0 byte.

  7. #7
    VBAX Regular
    Joined
    Nov 2007
    Posts
    61
    Location
    I would (using VBA) like to create a .txt called test.txt from fresh.

  8. #8
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Is there a particular file name you want, based on the data available in your Excel file or is it a random file?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As I said, do it in Excel, save it a s text file. Do all this with the macro recorder on, and you have the code.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    One way -- Help has good writeup on the VBA cmds

    [VBA]
    Sub MakeTXT()
    Const sFilename As String = "C:\SmallFile.txt"

    Dim sLine As String
    Dim iFile As Long

    On Error Resume Next
    Call Kill(sFilename)
    On Error GoTo 0

    iFile = FreeFile
    Open sFilename For Output As iFile

    sLine = "ABC"

    Print #1, sLine

    Close #1

    End Sub
    [/VBA]

    Paul

  11. #11
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    This doesn't make much sense. If you cannot exceed 1 byte, then you can ONLY enter ONE character per text file. Is that what you really want?

    That's just a bit strange.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  12. #12
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    sry my explanation is a bit pants. If you re-name a excel spreadsheet to .txt the size of the .txt is above 1 byte. if you create a a .txt file from fresh and name it test.txt its size is 0 byte.
    Tezzies, the size of a txt file is 0 as long it's empty.
    ALe
    Help indigent families: www.bancomadreteresa.org

  13. #13
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    in Paul's example just change sLine = "ABC" to sLine = ""
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

  14. #14
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by ProteanBeing
    in Paul's example just change sLine = "ABC" to sLine = ""
    Almost, but that just makes it 2 bytes instead of 5 bytes. This is because 'Print #1, sLine' will always add a carriage return (or line feed, not sure which) and that takes 2 bytes. Write #1 will also add a line feed / carriage return.

    I'm not sure how you would write to a text file to have ONLY one character without inherently adding a line feed to it...




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  15. #15
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Ok. To write to a text file so you do not have a carriage return at the end, use the FileSystemObject and TextStream objects (set a reference to Microsoft Scripting Runtime).

    Here's the code:
    [VBA]' SET REFERENCE TO 'Microsoft Scripting Runtime'
    Sub MakeTXT()
    Dim fso As New FileSystemObject
    Dim myFile As TextStream

    Set myFile = fso.CreateTextFile("C:\MyTextFile.txt", Overwrite:=True)

    myFile.Write "1"

    myFile.Close

    End Sub[/VBA]




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  16. #16
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    Open "U:\textfile.txt" For Output As #1
    Close #1

    0 bytes!

    too easy to see
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

  17. #17
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by ProteanBeing
    Open "U:\textfile.txt" For Output As #1
    Close #1

    0 bytes!

    too easy to see
    Right. But try writing 1 byte of data with that method.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  18. #18
    I'm curious. Why do you want to create a 0-1 byte file?

    [VBA]Public Sub EmptyTxtFile()
    Const Outfile = "c:\test.txt"

    Open Outfile For Output Access Write As #1 ' Open text file for write
    Close #1 ' Close file.
    End Sub[/VBA]

  19. #19
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    Tezzies,
    you do know that even if you create a 0-1 byte file the computer will still reserve a sector for that file (4 kb on my computer). If you create a lot of these files there will be a HUGE difference between the electronic used space and the physical used space.
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •