Results 1 to 25 of 25

Thread: Open and Clear a text file.

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Open and Clear a text file.

    How can I open C:\Folder1\Text1.txt and erase all it's contents and then save it. It does not need to visually open, but I just want to erase all of it's contents. ~Bryan
    ~ Animelion

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Open and Clear a text file.

    Quote Originally Posted by Animelion
    How can I open C:\Folder1\Text1.txt and erase all it's contents and then save it. It does not need to visually open, but I just want to erase all of it's contents. ~Bryan
    What are you trying to do? Just Delete the File.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Open and Clear a text file.

    I have a text file that I am recording information into, however I want the user to be able to click a button to wipe the text file clean. So I am not deleating the text file, but just trying to clear all the text in it and then have it save. ~Thanks Bryan
    ~ Animelion

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Open and Clear a text file.

    if you want to clear a file's contents:
    VB Code:
    1. Open "C:\Folder\Text1.txt" For Output As #1: Close #1

  5. #5
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Open and Clear a text file.

    Quote Originally Posted by bushmobile
    if you want to clear a file's contents:
    VB Code:
    1. Open "C:\Folder\Text1.txt" For Output As #1: Close #1
    Hey bushmobile, could you walk me through this single line of code please? I'm amazed at how simplified this is I'd like to understand it more

    Thanks




    If you find any of my posts of good help, please rate it

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open and Clear a text file.

    Its actually 2 lines of code. The colon is a line break/separater so to speak.

    The first part
    "Open "C:\Folder\Text1.txt" For Output As #1"

    Opens the file in Output mode which will clear the file contents if it already exists.


    "Close #1"
    Closes the file and releases.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Open and Clear a text file.

    Thanks RobDog

    Well....it's 2 instructions on a single line, right?

    When does it save the contents of the empty text file? At the close portion? Is that standard with the Close statement?

    And I was doing some testing with this....and reading the help file about this...and it said:
    If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs.
    And I'm not sure I understand this correctly. I had the text file open with text in it (saved, by the way) and I ran the code to try to get an error, but couldn't. So I assume that it is because of the type of access that it has makes the error not occur, correct? And what type of access is this exactly? What kind of access would cause an error?

    And I also noticed that if I ran the code with the text file already opened I can open the text file again and a new instance of the text file opens with blank text. And if I save the text file instance that has text in it and close both instances and re-open, the text is (as expected) still there....but why didn't it ask me to replace the file? And why am I aloud to open multiple instances of notepad?


    ....maybe I just don't understand notepad correctly




    If you find any of my posts of good help, please rate it

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open and Clear a text file.

    Yes, 2 lines of code (instructions) on a single line. There are several Modes for files to be opened with. You can specify Shared or Exclusive etc. If you dont specify it then its shared and opening the file in Notepad and via code will not generate an error. Yes, when you close the file its an implied save operation.

    Here is one tutorial on File I/O - http://vbforums.com/showthread.php?t=405051
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Open and Clear a text file.

    Quote Originally Posted by RobDog888
    Yes, 2 lines of code (instructions) on a single line. There are several Modes for files to be opened with. You can specify Shared or Exclusive etc. If you dont specify it then its shared and opening the file in Notepad and via code will not generate an error. Yes, when you close the file its an implied save operation.

    Here is one tutorial on File I/O - http://vbforums.com/showthread.php?t=405051
    Thanks for the info and the link Rob. Really usefull info...and relatively easy to understand




    If you find any of my posts of good help, please rate it

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Open and Clear a text file.

    notepad doesn't open files directly - it opens copies of them - that's why you can open a file in VB while it's open in notepad (and why you can open multiple copies of the same file in notepad - they're all actually separate files) - at least i think that's what it does.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open and Clear a text file.

    I would think it opens it in the default, shared file mode.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Open and Clear a text file.

    i just assumed it opened the file read into memory (text box) and closed it, reopen to save etc. unlike word that holds the file open until it is the document is closed.

    pete

  14. #14

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Open and Clear a text file.

    How could I have it automatically copy the contents of the text file into the clipboard ?
    ~ Animelion

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Open and Clear a text file.

    VB Code:
    1. Open "C:\file.txt" For Input As #1
    2.         Clipboard.Clear
    3.         Clipboard.SetText Input(LOF(1), #1)
    4.     Close #1

  16. #16

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Open and Clear a text file.

    Thanks bushmobile. Can I use the semicolon multiple times to make that all in one line of text ?
    ~ Animelion

  17. #17
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Open and Clear a text file.

    you can, but don't - it'll make your code virtually impossible to read / debug easily.

    the only reason I did it like that in post #3 is because the Close #1 bit could almost be considered a byproduct of the Opening (and it didn't effect the readability). If you're opening a file and then doing stuff with it then you should put everything on separate lines - it doesn't change the number of lines of code that are actually executed.

  18. #18

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Open and Clear a text file.

    When I user the Clipboard.clear command I get a "Object Required" error. Any more quidance would be helpful.
    ~ Animelion

  19. #19
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Open and Clear a text file.

    Quote Originally Posted by Animelion
    When I user the Clipboard.clear command I get a "Object Required" error. Any more quidance would be helpful.
    You will get that error when trying to use Clipboard.Clear in VBA, but it will work just fine in VB6.

    Which are you using?

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open and Clear a text file.

    Clipboard is for VB 6. Which Office app are you using?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Open and Clear a text file.

    I am running office 2003 on the computer, but it's not an office app, it's a macro recorder in IBM's WRQ reflections. The macro runs on VBA. Hope that helps you.
    ~ Animelion

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open and Clear a text file.

    Then you will probably need to use some Window APIs to do the trick.
    VB Code:
    1. Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
    2. Private Declare Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
    3. Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Open and Clear a text file.

    When I user the Clipboard.clear command I get a "Object Required" error. Any more guidance would be helpful.
    ~ Animelion

  24. #24
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Open and Clear a text file.

    whoops sorry, that's VB6 yes

    As RobDog888 said, you'll need API, something like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GlobalLock Lib "kernel32" ( _
    4.     ByVal hMem As Long) As Long
    5.  
    6. Private Declare Function GlobalUnlock Lib "kernel32" ( _
    7.     ByVal hMem As Long) As Long
    8.    
    9. Private Declare Function GlobalAlloc Lib "kernel32" ( _
    10.     ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    11.    
    12. Private Declare Function OpenClipboard Lib "User32" ( _
    13.     ByVal hwnd As Long) As Long
    14.  
    15. Private Declare Function SetClipboardData Lib "User32" ( _
    16.     ByVal wFormat As Long, ByVal hMem As Long) As Long
    17.    
    18. Private Declare Function CloseClipboard Lib "User32" () As Long
    19.  
    20. Private Declare Function EmptyClipboard Lib "User32" () As Long
    21.  
    22. Private Declare Function lstrcpy Lib "kernel32" ( _
    23.     ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
    24.    
    25. Private Const GHND = &H42
    26. Private Const CF_TEXT = 1
    27.  
    28. Private Function CopyFileToClipboard(ByVal sPath As String) As Boolean
    29.     Dim hGlobalMemory As Long, lpGlobalMemory As Long
    30.     Dim sText As String
    31.    
    32.     Open sPath For Input As #1
    33.         sText = Input(LOF(1), #1)
    34.     Close #1
    35.        
    36.     hGlobalMemory = GlobalAlloc(GHND, Len(sText) + 1)
    37.     lpGlobalMemory = GlobalLock(hGlobalMemory)
    38.     lpGlobalMemory = lstrcpy(lpGlobalMemory, sText)
    39.    
    40.     If GlobalUnlock(hGlobalMemory) = 0 Then
    41.         If OpenClipboard(0&) Then
    42.             EmptyClipboard
    43.             SetClipboardData CF_TEXT, hGlobalMemory
    44.             If CloseClipboard Then CopyFileToClipboard = True
    45.         End If
    46.     End If
    47. End Function
    to use:
    VB Code:
    1. If CopyFileToClipboard("C:\file.txt") Then
    2.     ' Copy successful
    3. Else
    4.     ' Copy failed
    5. End If

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open and Clear a text file.

    Yes, use the APIs I posted as the Clipboard obbject is not supported in VBA.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width