Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a Read/Write Test File in VB6

Status
Not open for further replies.

Dydom

Programmer
Apr 30, 2002
10
0
0
FR
Hi,
I would like to make a test which permit to transforme the file property. If my file property is in read only, I want to put it on write, and, if we can't put it on write ( if the file is on a cdrom), I want to put the file in a temp path on my harddrive.
So, I need help to change the file property. I don't find this in the filename propety function, so, if anyone have an idea.
Thanks
Dom
 
This info is from VB Help: You would use the following to get the file attribute:

-------------------------------
-------------------------------

GetAttr Function


Returns an Integer representing the attributes of a file, directory, or folder.

Syntax

GetAttr(pathname)

The required pathnameargument is astring expression that specifies a file name. The pathname may include the directory or folder, and the drive.

Return Values

The value returned by GetAttr is the sum of the following attribute values:

Constant Value Description
vbNormal 0 Normal.
vbReadOnly 1 Read-only.
vbHidden 2 Hidden.
vbSystem 4 System file.
vbDirectory 16 Directory or folder.
vbArchive 32 File has changed since last backup.


Note Theseconstants are specified by Visual Basic for Applications. The names can be used anywhere in your code in place of the actual values.

Remarks

To determine which attributes are set, use the And operator to perform abitwise comparison of the value returned by the GetAttr function and the value of the individual file attribute you want. If the result is not zero, that attribute is set for the named file. For example, the return value of the following And expression is zero if the Archive attribute is not set:

Result = GetAttr(FName) And vbArchive

-------------------------------
-------------------------------

And the following to set the attribute:

Sets attribute information for a file.

Syntax

SetAttr pathname, attributes

The SetAttr statement syntax has thesenamed arguments:

Part Description
pathname Required.String expression that specifies a file name — may include directory or folder, and drive.
attributes Required.Constant ornumeric expression, whose sum specifies file attributes.


Settings

The attributesargument settings are:

Constant Value Description
vbNormal 0 Normal (default).
vbReadOnly 1 Read-only.
vbHidden 2 Hidden.
vbSystem 4 System file.
vbArchive 32 File has changed since last backup.


Note These constants are specified by Visual Basic for Applications. The names can be used anywhere in your code in place of the actual values.

Jim [pc2]
Web: Email: jim@jimnull.com
 
Here is an quick example I tossed together. You will need to reference the Microsoft Scripting Runtime library.

Private Sub Command1_Click()
Dim fso As New FileSystemObject
Dim sPath As String
Dim sTempPath As String
Dim sFileName As String
Dim bRO As Boolean
Dim sFile As File
Dim sAttrib As FileAttribute

On Error GoTo ErrHnd

sTempPath = "C:\" 'Path of temp dir
sFileName = "Voting.gif" 'File to Check
sPath = "D:\Downloads\" 'Path of File to Check

'Get the file Attributes
Set sFile = fso.GetFile(sPath & sFileName)
sAttrib = sFile.Attributes

'Check the Read Only Property of the File
If BitOn(sAttrib, 1) Then
'Attempt to Change Read Only Property Error Trap in case of CD
sFile.Attributes = sAttrib - ReadOnly
End If

'bRO is Set to True in Error Trap If File is Read Only
If bRO Then
'Copy the File to a temp dir and Change the Read Only Property
sFile.Copy sTempPath & sFileName, True
Set sFile = fso.GetFile(sTempPath & sFileName)
sFile.Attributes = sAttrib - ReadOnly
End If

Set fso = Nothing
Set sFile = Nothing

Exit Sub

ErrHnd:

Select Case Err.Number
Case 70 'Permission Not Permitted for Read Only CD
bRO = True
Resume Next
Case Else
MsgBox Err.Number & " " & Err.Description & " Error Generated By " & Err.Source, vbCritical, "System Error Trap !"
End Select
End Sub


Private Function BitOn(Number As Long, Bit As Long) As Boolean
Dim iX As Long
Dim iY As Long

iY = 1
For iX = 1 To Bit - 1
iY = iY * 2
Next
If Number And iY Then BitOn = True Else BitOn = False
End Function


Hope this Helps. Good LuckPrivate Sub Command1_Click()
Dim fso As New FileSystemObject
Dim sPath As String
Dim sTempPath As String
Dim sFileName As String
Dim bRO As Boolean
Dim sFile As File
Dim sAttrib As FileAttribute

On Error GoTo ErrHnd

sTempPath = "C:\" 'Path of temp dir
sFileName = "Voting.gif" 'File to Check
sPath = "D:\Downloads\" 'Path of File to Check

'Get the file Attributes
Set sFile = fso.GetFile(sPath & sFileName)
sAttrib = sFile.Attributes

'Check the Read Only Property of the File
If BitOn(sAttrib, 1) Then
'Attempt to Change Read Only Property Error Trap in case of CD
sFile.Attributes = sAttrib - ReadOnly
End If

'bRO is Set to True in Error Trap If File is Read Only
If bRO Then
'Copy the File to a temp dir and Change the Read Only Property
sFile.Copy sTempPath & sFileName, True
Set sFile = fso.GetFile(sTempPath & sFileName)
sFile.Attributes = sAttrib - ReadOnly
End If

Set fso = Nothing
Set sFile = Nothing

Exit Sub

ErrHnd:

Select Case Err.Number
Case 70 'Permission Not Permitted for Read Only CD
bRO = True
Resume Next
Case Else
MsgBox Err.Number & " " & Err.Description & " Error Generated By " & Err.Source, vbCritical, "System Error Trap !"
End Select
End Sub


Private Function BitOn(Number As Long, Bit As Long) As Boolean
Dim iX As Long
Dim iY As Long

iY = 1
For iX = 1 To Bit - 1
iY = iY * 2
Next
If Number And iY Then BitOn = True Else BitOn = False
End Function Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Sorry about the second paste effect. There is only 2 procedures not 4, I guess I was in a hurry. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Thanks for your help
setattr is running, now I'm studying the Foada's answer to create my temp file !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top