Click to See Complete Forum and Search --> : Saving Problem


NeOmega
October 22nd, 2001, 02:09 PM
When I run the following code, it tries to write to files which aren't folders. I only want it to save the file in the folders under that directory. Help would be appreciated.
_____________________

option Explicit
private Sub Command1_Click()
Dim strFolder as string
Dim strtPoint as string
strtPoint = "C:\My Documents"
strFolder = Dir(strtPoint & "\*.*", vbDirectory)
Do
If strFolder <> "." And strFolder <> ".." then ' bypass system folders
If GetAttr(strtPoint & "\" & strFolder) And vbDirectory = vbDirectory then ' This a Directory
Open strtPoint & "\" & strFolder & "\title.txt" for Output as #1
print #1, "Message"
Close #1
End If
End If
strFolder = Dir ' get next in chain
Loop Until strFolder = "" ' do it again
End Sub

TB_Guy_2000
October 22nd, 2001, 02:42 PM
Here is the problem

If (GetAttr(strtPoint & "\" & strFolder) And vbDirectory) = vbDirectory then ' This a Directory


Hope this helps

TB_Guy_2000

John G Duffy
October 22nd, 2001, 06:26 PM
What I see is that any folder that is a subFolder of "My Documents" will end up with a one line file called "Title.txt"

Where do you want "Title.txt" to end up at and how many copies do you want?
On my System the "My DOcuments" has three subfolders called "My Picturees", "My eBooks" and "My Music". All tree ended up with that file in it.

John G

NeOmega
October 22nd, 2001, 08:46 PM
That's exactly what I wanted except now I have a new problem. I need to be able to have more than one directory (ex. My Documents and Misc Folder). Here's my code again.
__________

option Explicit
private Sub Command1_Click()
Dim strFolder as string
Dim strtPoint as string
strtPoint = "C:\Directory"
strFolder = Dir(strtPoint & "\*.*", vbDirectory)
Do
If strFolder <> "." And strFolder <> ".." then ' bypass system folders
If (GetAttr(strtPoint & "\" & strFolder) And vbDirectory) = vbDirectory then ' This a Directory
Open strtPoint & "\" & strFolder & "\file.txt" for Output as #1
print #1, "Text"
Close #1
End If
End If
strFolder = Dir ' get next in chain
Loop Until strFolder = "" ' do it again
End Sub

John G Duffy
October 23rd, 2001, 07:26 AM
Simply put the code into a subroutine then call the subroutine from Command1_Click passing the name of the starting folder like so.

option Explicit
'
private Sub Command1_Click()
SaveToFolder "C:\My Documents"
SaveToFolder "C:\Misc Folder"
End Sub
'
public Sub SaveToFolder(strtPoint as string)
'
Dim strFolder as string
strFolder = Dir(strtPoint & "\*.*", vbDirectory)
Do
If strFolder <> "." And strFolder <> ".." then ' bypass system folders
If GetAttr(strtPoint & "\" & strFolder) And vbDirectory = vbDirectory then ' This a Directory
Open strtPoint & "\" & strFolder & "\title.txt" for Output as #1
print #1, "Message"
Close #1
End If
End If
strFolder = Dir ' get next in chain
Loop Until strFolder = "" ' do it again
End Sub




John G

Boumxyz2
October 23rd, 2001, 09:50 AM
If GetAttr(strtPoint & "\" & strFolder) And
vbDirectory = vbDirectory then ' This a Directory




The second part of this condition seems useless.
VbDirectory always equals vbDirectory

NeOmega
October 24th, 2001, 07:46 PM
How can I make it so that the program will write to system folders?

John G Duffy
October 25th, 2001, 02:49 PM
Slight error in my sample. Relplace similiar line with this one. See positioning of right parenthesis

If GetAttr(strtPoint & "\" & strFolder And vbDirectory) = vbDirectory then ' This a Directory




John G

John G Duffy
October 25th, 2001, 02:50 PM
Misplaced Right Parenthesis. It should read

If GetAttr(strtPoint & "\" & strFolder) And vbDirectory = vbDirectory then ' This a Directory



THanks for pointing that out

John G

John G Duffy
October 25th, 2001, 02:51 PM
A correction to my correction.

If GetAttr(strtPoint & "\" & strFolder And vbDirectory) = vbDirectory then ' This a Directory




John G

John G Duffy
October 25th, 2001, 03:02 PM
There seems to be some confusion regarding positioning of parenthesis in my original post so here is is again with some clarification (Additional left - right parenthesis). The original post is correct. It takes the output of the GetAttr function "ands" the result with vbDirectory then compares the results with vbDirectory.
Disregard my corrections and corrections to the corrections.

option Explicit
'
private Sub Command1_Click()
SaveToFolder "C:\My Documents"
End Sub
'
public Sub SaveToFolder(strtPoint as string)
'
Dim strFolder as string
strFolder = Dir(strtPoint & "\*.*", vbDirectory)
Do
If strFolder <> "." And strFolder <> ".." then ' bypass system folders
If (GetAttr(strtPoint & "\" & strFolder) And vbDirectory) = vbDirectory then ' This a Directory
Open strtPoint & "\" & strFolder & "\title.txt" for Output as #1
print #1, "Message"
Close #1
End If
End If
strFolder = Dir ' get next in chain
Loop Until strFolder = "" ' do it again
End Sub





John G

John G Duffy
October 25th, 2001, 03:04 PM
I know of no reason why you can't read to a system folder unless they are password protected or are made readonly for some reason.
I am not familiar with Windows NT or Windows 2000 so if you are using those I can not help.

John G