getzjd
asked on
ASP - If text File exists increment +1
I need a simple ASP function that when executed will write a file to a directory. If it exists, then increment it +1 . Right now as you see below I am placing the txt file in two locations. I would like ONLY the file in the \backup folder to be incremented if it exists. The file in \ should just be overwrtiten as it is.
<%@LANGUAGE="VBSCRIPT"%>
<%
Dim cnn
Dim SQL
Dim strReturn
Dim objRS
Set cnn = Server.CreateObject("ADODB .Connectio n")
Set objRS = Server.CreateObject("ADODB .Recordset ")
cnn.ConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=d:\ShopFloorweb\iss ue\Goodsis sue.mdb;Pe rsist Security Info=False"
cnn.Open
SQL= "SELECT datestamp, timestamp, po, material, batch, quantity, uom, movtype, employee FROM tbl1"
'Execute select statement
SET objRS = cnn.Execute(SQL)
strReturn= objRS.GetString (2, , vbTab,vbCrLf, "Null")
objRS.Close
set objRS = Nothing
dim fs,tfile, zfile
set fs=Server.CreateObject("Sc ripting.Fi leSystemOb ject")
set tfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\issue.txt")
set zfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\backup\issue.txt")
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing
%>
<%@LANGUAGE="VBSCRIPT"%>
<%
Dim cnn
Dim SQL
Dim strReturn
Dim objRS
Set cnn = Server.CreateObject("ADODB
Set objRS = Server.CreateObject("ADODB
cnn.ConnectionString = "Provider=Microsoft.Jet.OL
cnn.Open
SQL= "SELECT datestamp, timestamp, po, material, batch, quantity, uom, movtype, employee FROM tbl1"
'Execute select statement
SET objRS = cnn.Execute(SQL)
strReturn= objRS.GetString (2, , vbTab,vbCrLf, "Null")
objRS.Close
set objRS = Nothing
dim fs,tfile, zfile
set fs=Server.CreateObject("Sc
set tfile=fs.CreateTextFile(Se
set zfile=fs.CreateTextFile(Se
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing
%>
ASKER
True worked..
but received this error in relation to above code.
Microsoft VBScript runtime error '800a01a8'
Object required: 'fdlr'
/issue/csv.asp, line 43
line 43 is
set zfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\backup\issue" & fdlr.Files.Count + 1 & ".txt")
but received this error in relation to above code.
Microsoft VBScript runtime error '800a01a8'
Object required: 'fdlr'
/issue/csv.asp, line 43
line 43 is
set zfile=fs.CreateTextFile(Se
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just to complete badotz solution based on your question:
dim fldr,folderCount
set fs=Server.CreateObject("Sc ripting.Fi leSystemOb ject")
set fldr=fs.GetFolder(Server.m apPath("." ) & "\backup")
folderCount = fldr.Files.Count
'------------------------- ---------- ---------- -----
' overwrite existing issue.txt in root folder
'------------------------- ---------- ---------- -----
set tfile=fs.CreateTextFile(Se rver.mapPa th(".") & "\issue.txt", TRUE)
'------------------------- ---------- ---------- -----
' increment issue file in backup folder from issue1.txt to issuexxxx.txt where xxxx is integer
'------------------------- ---------- ---------- -----
if folderCount = 0 then
foldercount=1
else
folderCount=folderCount+1
end if
set zfile=fs.CreateTextFile(Se rver.mapPa th(".") & "\backup\issue" & (folderCount) & ".txt")
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing
Hope it helps.
Regards,
Willy
dim fldr,folderCount
set fs=Server.CreateObject("Sc
set fldr=fs.GetFolder(Server.m
folderCount = fldr.Files.Count
'-------------------------
' overwrite existing issue.txt in root folder
'-------------------------
set tfile=fs.CreateTextFile(Se
'-------------------------
' increment issue file in backup folder from issue1.txt to issuexxxx.txt where xxxx is integer
'-------------------------
if folderCount = 0 then
foldercount=1
else
folderCount=folderCount+1
end if
set zfile=fs.CreateTextFile(Se
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing
Hope it helps.
Regards,
Willy
ASKER
Wingko... here is the error I get with your code...
Microsoft VBScript runtime error '800a004c'
Path not found
/issue/csv.asp, line 38
Line 38 is set fldr=fs.GetFolder(Server.m apPath("." ) & "\Backup")
badotz when i try your code i get
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/issue/csv.asp, line 46
Line 46 is tfile.WriteLine(strReturn)
Microsoft VBScript runtime error '800a004c'
Path not found
/issue/csv.asp, line 38
Line 38 is set fldr=fs.GetFolder(Server.m
badotz when i try your code i get
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/issue/csv.asp, line 46
Line 46 is tfile.WriteLine(strReturn)
Too many cooks...did my solution work, or did I miss something?
ASKER
badotz when i try your code i get
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/issue/csv.asp, line 46
Line 46 is tfile.WriteLine(strReturn)
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/issue/csv.asp, line 46
Line 46 is tfile.WriteLine(strReturn)
ASKER
Badotz here is what i tried with you
<%
Dim cnn
Dim SQL
Dim strReturn
Dim objRS
Dim fs,tfile, zfile, fldr, folderCount
Set cnn = Server.CreateObject("ADODB .Connectio n")
Set objRS = Server.CreateObject("ADODB .Recordset ")
cnn.ConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=d:\ShopFloorweb\iss ue\Goodsis sue.mdb;Pe rsist Security Info=False"
cnn.Open
SQL= "SELECT datestamp, timestamp, po, material, batch, quantity, uom, movtype, employee FROM tbl1"
'Execute select statement
SET objRS = cnn.Execute(SQL)
strReturn= objRS.GetString (2, , vbTab,vbCrLf, "Null")
objRS.Close
set objRS = Nothing
set fs=Server.CreateObject("Sc ripting.Fi leSystemOb ject")
set fldr=fs.GetFolder(Server.m apPath("/" ) & "\backup")
folderCount = fldr.Files.Count
if folderCount = 0 then
set zfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\backup\issue.txt")
else
set zfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\backup\issue" & (folderCount) & ".txt")
end if
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing
%>
<%
Dim cnn
Dim SQL
Dim strReturn
Dim objRS
Dim fs,tfile, zfile, fldr, folderCount
Set cnn = Server.CreateObject("ADODB
Set objRS = Server.CreateObject("ADODB
cnn.ConnectionString = "Provider=Microsoft.Jet.OL
cnn.Open
SQL= "SELECT datestamp, timestamp, po, material, batch, quantity, uom, movtype, employee FROM tbl1"
'Execute select statement
SET objRS = cnn.Execute(SQL)
strReturn= objRS.GetString (2, , vbTab,vbCrLf, "Null")
objRS.Close
set objRS = Nothing
set fs=Server.CreateObject("Sc
set fldr=fs.GetFolder(Server.m
folderCount = fldr.Files.Count
if folderCount = 0 then
set zfile=fs.CreateTextFile(Se
else
set zfile=fs.CreateTextFile(Se
end if
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing
%>
Post the entire code, and let's have a look.
Somehow, this line was left out:
set tfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\issue.txt", true)
Insert it above this line:
tfile.WriteLine(strReturn)
set tfile=fs.CreateTextFile(Se
Insert it above this line:
tfile.WriteLine(strReturn)
Ups sorry...I forgot to tell you...
My solution is working (I've test it), but you must create folder called backup inside the root where your script running. ex: csv.asp is in root issue/csv.asp then you must create backup under issue. /issue/backup.
And one more thing, you must give the newly created backup folder write access otherwise you'll get permission denied.
I guess badotz solution will get the same error as mine, unless you'll do the above step first.
Silly me, I thought you already have the backup folder, because from the sample you give me before, you do this : set zfile=fs.CreateTextFile(Se rver.mapPa th("/") & "\backup\issue.txt"), it won't work unless you have the backup folder first.
My solution is working (I've test it), but you must create folder called backup inside the root where your script running. ex: csv.asp is in root issue/csv.asp then you must create backup under issue. /issue/backup.
And one more thing, you must give the newly created backup folder write access otherwise you'll get permission denied.
I guess badotz solution will get the same error as mine, unless you'll do the above step first.
Silly me, I thought you already have the backup folder, because from the sample you give me before, you do this : set zfile=fs.CreateTextFile(Se
ASKER
the backup folder was already in place and has been in place. My original code was working by placing a file in each location both \ and \backup
I was a doink and left out a line of badotz code, he nailed it first.
Thanks for all your help. As I work on this project, I am sure I will post more in the future :-)
I was a doink and left out a line of badotz code, he nailed it first.
Thanks for all your help. As I work on this project, I am sure I will post more in the future :-)
No worries - glad to help.
set tfile=fs.CreateTextFile(Se
to
set tfile=fs.CreateTextFile(Se
Change this
set zfile=fs.CreateTextFile(Se
to (this bit is untested...)
set fldr=fs.GetFolder(Server.m
if fldr.Files.Count = 0 then
set zfile=fs.CreateTextFile(Se
else
set zfile=fs.CreateTextFile(Se
Info from http://www.w3schools.com/asp/met_createtextfile.asp