Recent

Author Topic: GetEnV Error  (Read 9287 times)

captian jaster

  • Guest
GetEnV Error
« on: April 02, 2010, 04:31:58 pm »
I Was Doing Some Coding and i made a Files And Folders Unit.
I was Testing My Program When It said All Directorys Found!
It went to make the file but it closed the window. I thought it was a file making error but it was because the folder was never made...
Code: [Select]
Procedure GetFolders;
BEGIN
  writeln('Getting Folders...');
  Delay(1000);
  Folders.System := Fsearch('System',GetEnV('Not')); //The Thing I dont understand..
  IF(Folders.System = 'Not')Then
  begin
    writeln('Missing Directory "System"');
    writeln('Making Directory...');
    Delay(200);
    CreateDir('System');
  end;
  Folders.Other := Fsearch('Other',GetEnV('Not'));
  IF(Folders.Other = 'Not')Then
  begin
    writeln('Missing Directory "Other"');
    writeln('Making Directory');
    Delay(200);
    CreateDIR('Other');
  end;
  writeln('All Directorys Found!');
END;

Ive used the GetEnV before But i dont know what i did wrong Now..

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: GetEnV Error
« Reply #1 on: April 02, 2010, 05:19:23 pm »
Code: [Select]
// ...
  Folders.System := Fsearch('System',GetEnV('Not')); //The Thing I dont understand..
//...

Is the environment string 'Not' defined?
Do you really want to search all paths that are in the env var 'Not'?

I'd recommend (CLICK) not to write repeat yourself in code. More code = more places where it can go wrong  :)
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

captian jaster

  • Guest
Re: GetEnV Error
« Reply #2 on: April 02, 2010, 08:01:18 pm »
I dont understand what you mean by defined..
The Not is used so if the folder doesnt exists it will make it.
I tryed adding backslashes that didnt work either..
This is were i got the code

http://pascalprogramming.byethost15.com/lesson9.php

dfeher

  • New Member
  • *
  • Posts: 19
Re: GetEnV Error
« Reply #3 on: April 02, 2010, 08:39:01 pm »
I looked where you said you got the code from. There is no 'Not' but '' used in those examples.  :)

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: GetEnV Error
« Reply #4 on: April 02, 2010, 08:50:08 pm »
Never copy & paste code and hope that it does what you want  :D
Why don't you use the more self-explanatory functions?
Like this...
Code: [Select]
procedure CheckAndCreateDir(const pName: string);
begin
  if not DirectoryExists(pName) then
  begin
    writeln(format('Missing directory: "%s"', [pName]));
    writeln('Making directory...');
    Delay(200);
    CreateDir(pName);
  end;
end;

procedure GetFolders;
begin
  writeln('Getting Folders...');
  Delay(1000);
  CheckAndCreateDir('System');
  CheckAndCreateDir('Others');
  writeln('All Directories Found!');
end;

All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

captian jaster

  • Guest
Re: GetEnV Error
« Reply #5 on: April 02, 2010, 10:05:02 pm »
Never copy & paste code and hope that it does what you want  :D
Why don't you use the more self-explanatory functions?
Like this...
Code: [Select]
procedure CheckAndCreateDir(const pName: string);
begin
  if not DirectoryExists(pName) then
  begin
    writeln(format('Missing directory: "%s"', [pName]));
    writeln('Making directory...');
    Delay(200);
    CreateDir(pName);
  end;
end;

procedure GetFolders;
begin
  writeln('Getting Folders...');
  Delay(1000);
  CheckAndCreateDir('System');
  CheckAndCreateDir('Others');
  writeln('All Directories Found!');
end;

Thats a cool code.... i can understand it easy to..
But ive never heard of DirectoryExist code.. isit in the Dos unit?
Edit:Never mind its in sysutils... What eles can GetEnV Do? And whats the point of format?
I was using GetEnV cuz i didnt know about DirectoryExists
I tryed it and it worked but arent you suppose to use Function instead of procedure
« Last Edit: April 02, 2010, 10:16:10 pm by captian jaster »

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: GetEnV Error
« Reply #6 on: April 02, 2010, 10:41:45 pm »
Thats a cool code.... i can understand it easy to..
Great; that was the intention  :D

Quote
But ive never heard of DirectoryExist code.. isit in the Dos unit?
Have a look at the docs; great reference material (DirectoryExists is in the rtl manual; same as GetEnv).

Quote
And whats the point of format?
The format function is great for building complex strings with minimal effort.

Quote
I tryed it and it worked but arent you suppose to use Function instead of procedure
If you want some sort of result back, then use a function.
As in this case a procedure gets the job done.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10485
  • FPC developer.
Re: GetEnV Error
« Reply #7 on: April 03, 2010, 01:37:47 pm »
One shouldn't use unit dos to begin with. Uses sysutils.

captian jaster

  • Guest
Re: GetEnV Error
« Reply #8 on: April 03, 2010, 05:43:11 pm »
Whats Wrong With the dos unit?................... wait i dont need it

 

TinyPortal © 2005-2018