Link to home
Start Free TrialLog in
Avatar of getzjd
getzjdFlag for United States of America

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.Connection")

Set objRS = Server.CreateObject("ADODB.Recordset")

cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\ShopFloorweb\issue\Goodsissue.mdb;Persist 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("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.mapPath("/") & "\issue.txt")
set zfile=fs.CreateTextFile(Server.mapPath("/") & "\backup\issue.txt")
tfile.WriteLine(strReturn)
zfile.WriteLine(strReturn)
tfile.close
zfile.close
set zfile=nothing
set tfile=nothing
set fs=Nothing

%>
Avatar of Badotz
Badotz
Flag of United States of America image

Change

set tfile=fs.CreateTextFile(Server.mapPath("/") & "\issue.txt")

to

set tfile=fs.CreateTextFile(Server.mapPath("/") & "\issue.txt", TRUE) ' Force overwrite

Change this

set zfile=fs.CreateTextFile(Server.mapPath("/") & "\backup\issue.txt")

to (this bit is untested...)

set fldr=fs.GetFolder(Server.mapPath("/") & "\backup")
if fldr.Files.Count = 0 then
    set zfile=fs.CreateTextFile(Server.mapPath("/") & "\backup\issue.txt")
else
    set zfile=fs.CreateTextFile(Server.mapPath("/") & "\backup\issue" & fdlr.Files.Count + 1 & ".txt")

Info from http://www.w3schools.com/asp/met_createtextfile.asp
Avatar of getzjd

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(Server.mapPath("/") & "\backup\issue" & fdlr.Files.Count + 1 & ".txt")
ASKER CERTIFIED SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wingko
wingko

Just to complete badotz solution based on your question:

dim fldr,folderCount
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fldr=fs.GetFolder(Server.mapPath(".") & "\backup")
folderCount = fldr.Files.Count

'--------------------------------------------------
' overwrite existing issue.txt in root folder
'--------------------------------------------------
set tfile=fs.CreateTextFile(Server.mapPath(".") & "\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(Server.mapPath(".") & "\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
Avatar of getzjd

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.mapPath(".") & "\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)




Too many cooks...did my solution work, or did I miss something?
Avatar of getzjd

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)
Avatar of getzjd

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.Connection")

Set objRS = Server.CreateObject("ADODB.Recordset")

cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\ShopFloorweb\issue\Goodsissue.mdb;Persist 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("Scripting.FileSystemObject")
set fldr=fs.GetFolder(Server.mapPath("/") & "\backup")
folderCount = fldr.Files.Count
if folderCount = 0 then
    set zfile=fs.CreateTextFile(Server.mapPath("/") & "\backup\issue.txt")
else
    set zfile=fs.CreateTextFile(Server.mapPath("/") & "\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

%>
Post the entire code, and let's have a look.
Somehow, this line was left out:

set tfile=fs.CreateTextFile(Server.mapPath("/") & "\issue.txt", true)

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(Server.mapPath("/") & "\backup\issue.txt"), it won't work unless you have the backup folder first.
Avatar of getzjd

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 :-)
No worries - glad to help.