Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Existing File Problem.....

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,63818
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Existing File Problem.....
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Existing File Problem..... - 8/27/2008 12:19:36 AM   
  dtscape

 

Posts: 22
Score: 0
Joined: 8/27/2008
Status: offline
Hi,
Hoping someone can help out on here. I am currently setting up a web page for work and have come across a problem.
This is the script so far (writen on Notepad).

Sub B2_onClick
on error resume next

dim date1
dim option1
dim completelogon
dim pagenumber
dim username


Set objNet = CreateObject("WScript.NetWork")
username = objNet.Username

pagenumber="Department ID"
date1 = FormatDateTime(Now(), vbGeneralDate)
option1 = document.form_Staffsurveylogon.menu.value
completelogon = pagenumber & " ; " & option1 & " ; " & date1
strFileName = "K:\Network Enterprise\Planning & Resource\Project Management\Staff Survey
2009\" & username & ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFileName)
Set f = fso.OpenTextFile("K:\Network Enterprise\Planning & Resource\Project
Management\Staff Survey 2009\" & username & ".txt")
If not (f.AtEndOfStream) then
ReadAllTextFile = f.ReadAll
End if
f.Close

Set f = fso.OpenTextFile("K:\Network Enterprise\Planning & Resource\Project
Management\Staff Survey 2009\" & username & ".txt", 2, True)
f.Write(ReadAllTextFile)
f.WriteLine completelogon

window.location.href  = "Page_1.htm"
End sub

What I am after is when running this script I need the code to see if a txt file (by this user, as this creates a text folder for each user as its run) already exists and if so a messagebox would appear saying this and then stops them proceeding any further through the web pages. This would mean only one run through by each person. Thats the idea...... Any help would be greatly appreciated as I've been trying so many different things and nothing seems to work!!!!!

 
 
Post #: 1
 
 RE: Existing File Problem..... - 8/27/2008 1:15:33 AM   
  dm_4ever


Posts: 2670
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
The problem with a web page would be permissions....comment or remove on error resume next and you may see what the problem is.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dtscape)
 
 
Post #: 2
 
 RE: Existing File Problem..... - 8/27/2008 1:52:37 AM   
  dtscape

 

Posts: 22
Score: 0
Joined: 8/27/2008
Status: offline
Thank you...
That did not seem to work though... It somehow stopped the script from doing anything ( I have no idea why!!!)
I must confess to being a complete novice on vbs so please excuse my ignorance.

(in reply to dtscape)
 
 
Post #: 3
 
 RE: Existing File Problem..... - 8/27/2008 1:55:46 AM   
  dtscape

 

Posts: 22
Score: 0
Joined: 8/27/2008
Status: offline
Could you please suggest a code that will search for the txt file (titled by the username) and if it exists to then state a message which when clicked would close the window (without requesting permission from the user). This would stop them from proceeding any further( which is my aim).

Thank you in advance.

(in reply to dtscape)
 
 
Post #: 4
 
 RE: Existing File Problem..... - 8/27/2008 11:07:10 AM   
  fosterr_2000

 

Posts: 115
Score: 0
Joined: 12/18/2004
From:
Status: offline
I am not sure if I fully understand what you are needing but maybe this will help.  I cleaned yours up a bit.

Hope this helps get you started.



Sub B2_onClick
on error resume next
dim date1
dim option1
dim completelogon
dim pagenumber
dim username
dim fso, f
dim ReadAllTextFile
dim strFileName
dim objNet
const ForWriting = 2
const ForReadOnly = 1

Set objNet = CreateObject("WScript.NetWork")
username = objNet.Username
pagenumber="Department ID"
date1 = FormatDateTime(Now(), vbGeneralDate)
option1 = document.form_Staffsurveylogon.menu.value
completelogon = pagenumber & " ; " & option1 & " ; " & date1
strFileName = "K:\Network Enterprise\Planning & Resource\Project Management\Staff Survey " & _
             "2009\" & username & ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")
if not fso.fileExists(strFileName) then
  Set f = fso.OpenTextFile(strFileName, ForWriting, true)
  f.WriteLine completelogon
else
  Set f = fso.openTextFile(strFileName, ForReadOnly, true)
  ReadAllTextFile = f.ReadAll
  msgbox(ReadAllTextFile)
end if
f.Close
window.location.href  = "Page_1.htm"
End sub

(in reply to dtscape)
 
 
Post #: 5
 
 RE: Existing File Problem..... - 8/27/2008 8:05:50 PM   
  dtscape

 

Posts: 22
Score: 0
Joined: 8/27/2008
Status: offline
Thank you.

Not really what I am after....I simply need a code that will see if a text file exists (under the current user) and if so to state a message  ("Thank you. You have already completed the Staff Survey") and by clicking ok to close the window(without requesting permission from the user to do so). If the text file does not exist then to carry on...

Hope someone can help as I've tried several options and nothing seems to work.


_____________________________

Cheers
PAUL B.

(in reply to dtscape)
 
 
Post #: 6
 
 RE: Existing File Problem..... - 8/28/2008 12:33:41 AM   
  chiltz

 

Posts: 76
Score: 0
Joined: 6/13/2007
Status: offline
The below should work.  It will check for the existance of the file, if it exists, it will give you a message box and then exit If statement without processing.  If the file does not exist, then it continues on.



      

Not sure why you are reading all the info from the doc into a variable, closing the file, reopening the file, rewriting the data into the same file along with one new line.  Well...I should say I understand why you did it....ForWriting(2) would overwrite all info in the doc.  You should just use "ForAppending(8)" and that would just write a new line on the first blank line in the doc.  Save you some time and code.

< Message edited by chiltz -- 8/28/2008 12:40:08 AM >

(in reply to dtscape)
 
 
Post #: 7
 
 RE: Existing File Problem..... - 8/29/2008 1:37:02 AM   
  dtscape

 

Posts: 22
Score: 0
Joined: 8/27/2008
Status: offline
Thank you... you've cracked it!!!!
Strange but I tried this form of coding before but it just did nothing.... I think it was the placement of the "End If" as i placed it too early before.

I have removed the reading section on this part(as you said. It seems pointless!!!). I copied this part of coding from a colleagues work (who has now left) and am learning as I go. Having great fun... with a bit of frustration thrown in for good measure!!!!

Thank you again....

_____________________________

Cheers
PAUL B.

(in reply to dtscape)
 
 
Post #: 8
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Existing File Problem..... Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts