[C005] VB-data file (2) random File

Source: Internet
Author: User
Tags file copy

Random File

Set the length of each record in a random file and set the record number. The record number starts from 1.

When accessing a record, the system calculates the location of the record as long as the number of the required record is described, and then writes or reads the record.

Preliminary understanding

Create a random file containing 10 records. Each record is composed of three values: Square (1 to 10), cubic (3), and open-ended (0 to 10). The number is used as the record number.

Private Type Numval Square As String Cube As String SqrRoot As StringEnd TypePublic nv As NumvalSub dksjflsd () Open ThisWorkbook. path & "\ data5.txt" For Random As #1 Len = Len (nv) For I = 1 To 9 nv. square = CStr (I * I) nv. cube = CStr (I * I) nv. sqrRoot = CStr (Sqr (I) Put #1, I, nv Next For I = 1 To 9 Get #1, I, nv Debug. print "no."; I; "No. Record:", nv. square, nv. cube, nv. sqrRoot Next Close # 1End Sub

Perform random file access, including the following:

(1) Before opening a file for random access, use Type... The End Type statement defines a record Type (such as Numval). This Type includes multiple data items and is consistent with the fields included in the records in the file.

When Dim is used to define a variable (such as nv) as a record type Numval, this variable also contains multiple data items of this type. You can use nv later. squre, nv. cube, nv. reference sqroot


(2) Specify the Random type to open the file and set the record length. After opening the file, you can save or take any record.

 

(3) read a record or store a record by using the Get and Put statements and specifying the record number respectively.

The relationship between the record and the record number must be established.

1. Read statement
Format: Get # file number [, record number], variable
Function: reads a specified record from a random file to a variable.


2. Write statements

Format: Put # file number [, record number], variable
Function: Write the value of a variable to the record of a random file.

By default, the record number is the record number used last time plus 1

 

Basic File Operations

Files and folders can be conveniently operated in applications

(1) create a folder Statement (MrDir): MkDir [path] folder name
Example: MkDir "D: \ VB \ Temp"


(2) Change the current folder Statement (ChDir): ChDir path
Example: ChDir "D: \ VB \ Dat"

(3) Delete folder Statement (RmDir): RmDir [path] folder name
Example: RmDir "D: \ VB \ Temp"


(4) Delete A file Statement (Kill): Kill [path] File Name
Example: Kill "D: \ VB \ datal. dat"
Kill "D: \ VB \ dat \*.*"

(5) file copy statement (FileCopy ):

FileCopy [Path 1] source file [, [Path 2] target file]
Example: FileCopy "C: \ aaa.txt", "D: \ Temp \ bbb.txt"

(6) renaming and moving of files:

Name (formerly known)
Renaming example: Name "C: \ aaa.txt" As "C: \ ccc.txt"
Mobile example: Name "C: \ Aaa.txt" As "C: \ Tmp \ Aaa.txt"

(7) Call the application

Format: Shell (command string [, window type])

Function: Call a specified application.

Example:

X = Shell ("C: \ Windows \ assumer.exe", 1)

Execute the explorer.exe application under "C: \ windows.pdf" and display the program window (example 8.12)

MkDir-create a folder

Sub dfjkld () For I = 1 To 20 MkDir "C: \ Users \ McDelfino \ Desktop \ exercises \ Folder-" & Format (I, "00 ") 'note that Format is changed to NextEnd Sub
 

RmDir-delete a folder

 

Sub dfksdlf () For I = 1 To 20 Step 2 RmDir "C: \ Users \ McDelfino \ Desktop \ exercises \ Folder-" & Format (I, "00") NextEnd Sub

 

 

Kill-delete an object

 

Sub dfksdlf () Kill "C: \ Users \ McDelfino \ Desktop \ exercises \ 1.txt" End Sub
Delete the 1.txt file!

 

 

Sub dfksdlf () Kill "C: \ Users \ McDelfino \ Desktop \ exercises \ *. *" End Sub
Delete all files with extension!

 

FileCopy-copy an object

Copy a file and change the file name ~

 

Sub djklklj () FileCopy "C: \ Users \ mcelfino \ Desktop \ exercises \ Folder-01 \ source file .xlsx", "C: \ Users \ mcelfino \ Desktop \ exercises \ Folder-02 \ target file .xlsx "End Sub

 

Name (formerly known As)-file rename & file Movement

File rename

 

Sub dkjflsdjfl () For I = 1 To 10 Name "C: \ Users \ McDelfino \ Desktop \ exercises \ workbook-" & I &". xlsx "As" C: \ Users \ McDelfino \ Desktop \ exercises \ workbook-"& Format (I," 00 ")&". xlsx "NextEnd Sub


 

 

File Movement

 

Sub dkjflsdjfl () For I = 1 To 10 Name "C: \ Users \ McDelfino \ Desktop \ exercises \ workbook-" & Format (I, "00 ")&". xlsx "As" C: \ Users \ McDelfino \ Desktop \ workbook-"& Format (I," 00 ")&". xlsx "NextEnd Sub

 

 

Dir-Traverse files

See: http://blog.csdn.net/alexbnlee/article/details/6932339

Modify file names in batches

 

Sub jdslfjl () Dim MyFile As String Dim count As Integer count = 1 MyFile = Dir ("C: \ Users \ mcelfino \ Desktop \ exercises \"&"*. xlsx ") Name" C: \ Users \ mcelfino \ Desktop \ exercises \ "& MyFile As" C: \ Users \ mcelfino \ Desktop \ exercises \ "& Format (1, "00 ")&". xlsx "Do While MyFile <>" "If MyFile =" "Then Exit Do count = count + 1 MyFile = Dir Name" C: \ Users \ McDelfino \ Desktop \ exercise \ "& MyFile As" C: \ Users \ McDelfino \ Desktop \ exercise \ "& Format (count," 00 ")&". xlsx "LoopEnd Sub

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.