In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how VBS implements text file manipulation. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
For file operations, for example: generate or open or delete a .txt format file.
Although the official FILE plug-in provides some basic functionality, it doesn't do much.
You don't need to understand what a fso template is and what a vbs statement is, just apply the following sentence!
The red word is a self-defined part, and the blue word is an arbitrary part.
If you do not understand, please refer to the following example!
The operation of the red part of the same file must be the same
Please read the explanation in parentheses carefully
Define the object of a relational file problem named fso. The operation of a file only needs to be written once. This sentence must be written.
VBS Set fso = CreateObject ("Scripting.FileSystemObject")
0. Determine whether a file or folder exists (yn is the return value, file existence returns 1, non-existence returns 0)
VBS yn=fso.FileExists (determine the directory of the file)
1. Create a file (the blue word defines whether the text file can be overwritten by the next write, omitting defaults to ture)
VBS set ttfile=fso.createtextfile (created file directory, ture | false)
two。 Open an existing file (blue word defines how the file is written, which is a. Read only b. Can read and write, but every time the file is opened to rewrite c. Write at the end of the file)
VBS const forreading=1
VBS const forwriting=2
VBS const forappending=8
Note: if you want to change the opening method, you must close the file and reopen it.
VBS set ttfile=fso.opentextfile (directory where files are opened, forreading | forwriting | forappending)
3. Close an open file (the red part is the same as the red part of the open file)
VBS ttfile.close
4. Read the line of the open file and enter (the red part ttfile is the same as the red part of the open file)
VBS read=ttfile.ReadLine
5. Read all the contents of the file (the red part of the ttfile should be the same as the red part of the open file)
VBS read=ttfile.ReadAll
6. Write a line and enter (the red part is the same as the red part of the file that has been opened)
VBS ttfile.writeline (what you want to write)
7. Delete the specified file (the first sentence is not required if ttfile has been defined)
VBS set ttfile=fso.GetFile (the file directory to be deleted)
VBS ttfile.delete
8. Determine whether the input tag is at the end (return-1, otherwise return 0)
VBS yn=ttfile.atendofstream
Here are some folder operations
8. Determine whether it is the root directory (yn is the return value, file existence returns 1, non-existence returns 0)
VBS yn=fso.IsRootFolder
9. Read folder
VBS set ttfile=fso.GetFolder (folder directory)
10. Create a folder
VBS set ttfile=fso.creaFolder (folder directory created)
11. Delete the specified folder (the first sentence is not required if ttfile has been defined)
VBS set ttfile=fso.GetFolder (the file directory to be deleted)
VBS ttfile.deletefolder
Here are some other frequently used file operations (note: can be used for files in all formats. The scarlet letter is already defined in your above script)
VBS ttfile.size returns the file size
VBS ttfile.type returns the file type
VBS ttfile.DateCreated returns the file creation time
VBS ttfile.DatelastAccessed returns the last access time of the file
VBS ttfile.DateLastModified returns the last modification time of the file
VBS ttfile.name returns the file name
VBS ttfile.ShortPath returns the file short path name
VBS ttfile.path returns the physical address of the file
VBS Set fso = CreateObject ("Scripting.FileSystemObject") / / determine whether there is a file 1.txt VBS pd1=fso.FileExists (d:\ 1.txt) if 0=pd1 / / if there is no file on the d disk Create an unoverridable file 1.txt VBS set txtfile=fso.createtextfile ("d:\ 1.txt", false) / / open 1.txt VBS set txtfile=fso.opentextfile ("d:\ 1.txt", forappending) / / write a line of "1234567890" VBS txtfile.writeline ("1234567890") / / close 1.txt VBS txtfile.close endif / / open 1.txt VBS set txtfile=fso.opentextfile ("d:\ 1.txt") as read-only under the d disk. Forreading) / / read the first line And assign it to the variable read VBS read=txtfile.ReadLine / / to close 1.txt VBS txtfile.close
All the functions are not listed completely for a while. I hope you can fill in the deficiency with the post.
Creat by Xi Feijian (small teacher)
'manipulate text files, manipulate fso objects (file object manipulation)
Create a file
Dim fso, fset fso = server.CreateObject ("Scripting.FileSystemObject") set f = fso.CreateTextFile ("C:\ test.txt", true) 'the second parameter indicates whether to overwrite f.Write ("write") f.WriteLine ("write and wrap") f.WriteBlankLines (3)' write three blank lines (equivalent to pressing enter three times in a text editor) f.Close () set f = nothingset fso = nothing
Open and read the file
Dim fso, fset fso = server.CreateObject ("Scripting.FileSystemObject") set f = fso.OpenTextFile ("C:\ test.txt", 1, false) 'the second parameter 1 indicates read-only opening, and the third parameter indicates whether to create f.Skip (3)' move the current position back three characters f.SkipLine ()'to the first character of the next line Note: no parameter response.Write f.Read (3) 'reads three characters back from the current position and moves the current position back three characters response.Write f.ReadLine ()' reads back from the current position until a newline character is encountered (no newline character is read), and moves the current position to the first character of the next line, note: no parameter response.Write f.ReadAll () 'reads backwards from the current position until the end of the file And move the current location to the last if f.atEndOfLine thenresponse.Write of the file ("end of line!") End ifif f.atEndOfStream thenresponse.Write ("end of file!") End iff.Close () set f = nothingset fso = nothing
Open and write the file
Dim fso, fset fso = server.CreateObject ("Scripting.FileSystemObject") set f = fso.OpenTextFile ("C:\ test.txt", 2, false) 'second parameter 2 indicates rewriting, if 8 means append f.Write ("write content") f.WriteLine ("write content and wrap") f.WriteBlankLines (3)' write three blank lines (equivalent to pressing enter three times in a text editor) f.Close () set f = nothingset fso = nothing
Determine whether the file exists or not
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") if fso.FileExists ("C:\ test.txt") thenresponse.Write ("object file exists") elseresponse.Write ("object file does not exist") end ifset fso = nothing
Move Fil
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") call fso.MoveFile ("C:\ test.txt", "D:\ test111.txt") 'the file name part of the two parameters can be different set fso = nothing
Copy a file
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") call fso.CopyFile ("C:\ test.txt", "D:\ test111.txt") 'the file name part of the two parameters can be different set fso = nothing
Delete a file
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") fso.DeleteFile ("C:\ test.txt") set fso = nothing
Create a folder
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") fso.CreateFolder ("C:\ test") 'the parent folder of the destination folder must exist set fso = nothing
Determine whether the folder exists
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") if fso.FolderExists ("C:\ Windows") thenresponse.Write ("destination folder exists") elseresponse.Write ("destination folder does not exist") end ifset fso = nothing
Delete folder
Dim fsoset fso = server.CreateObject ("Scripting.FileSystemObject") fso.DeleteFolder ("C:\ test") 'folder need not be empty set fso = nothing Thank you for reading! On "VBS how to achieve text file operation" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.