I’ve created a program with a login system where the user enters his information and the program checks the database the program is connected to to see if the results match and then logs the user in. I want to make a log file for each time a user logs in. The log file’s name should contain the user’s username and the date and time the user logged in. I’m using the following code to check for the user’s credentials and write his details to a log file. Also I want the date in the file name to be something like 23 January 2013.. So the coding for that is before the "with dmPredictGame do..."
sDate := DateToStr(Date());
sTime := TimeToStr(Time());
iYear := StrToInt(Copy(sDate,1,4));
iDay := StrToInt(Copy(sDate,9,2));
K := StrToInt(Copy(sDate,6,2));
Case K of
1 : sMonth := 'January';
2 : sMonth := 'February';
3 : sMonth := 'March';
4 : sMonth := 'April';
5 : sMonth := 'May';
6 : sMonth := 'June';
7 : sMonth := 'July'; //Check for the current month
8 : sMonth := 'August';
9 : sMonth := 'September';
10 : sMonth := 'Oktober';
11 : sMonth := 'November';
12 : sMonth := 'December';
end;
sTime1 := copy(sTime,1,2);
sTime2 := copy(sTime,4,2);
sLoginTime := sTime1 + ';' + sTime2; //Use ; because windows does not allow : in file names
sLoginDate := IntToStr(iDay) + ' ' + sMonth + ' ' + IntToStr(iYear);
with dmPredictGame do
begin
if tblUserInfo.Locate('Username', edtUsername.Text, []) AND ((edtPassword.Text) = tblUserInfo['Password']) then //Check if the username and password is correct.
begin
MessageDlg('Login was successful! Welcome back ' + edtUsername.Text, mtInformation, [mbOK],0);
edtUsername.Text := tblUserInfo['Username'];
begin
sUsername := edtUsername.Text;
sPassword := tblUserInfo['Password'];
sName := tblUserInfo['Name'];
sSurname := tblUserInfo['Surname'];
assignFile(UserLogFile, 'Log ' + sUsername + ' (' + sLoginDate + ') ' + sLoginTime + '.txt');
Rewrite(UserLogFile);
writeln(UserLogFile, 'Username: ' + sUsername);
writeln(UserLogFile, 'Password: ' + sPassword);
writeln(UserLogFile, 'Name: ' + sName);
writeln(UserLogFile, 'Surname: ' + sSurname);
writeln(UserLogFile, 'Date Logged In: ' + sDate);
writeln(UserLogFile, 'Time Logged In: ' + sTime);
closeFile(UserLogFile);
end;
Now my question: How can I create the text file in a different directory than the current directory that the program is in? I have a folder named “Logs” in the same folder that the program itself is in. I want the logs to be saved to that folder when they are created.
Any advice?