Edit the path to the file or folder: каталог программы\save

How to make it so that if the path каталог программы\save in Edit, then when you press the button, a message is displayed - "Обрабатывать файлы в указанной папке запрещено" . "Обрабатывать файлы в указанной папке запрещено" The save folder is always located near the exe program itself.

I assume that you need to prescribe something like this or maybe wrong?: ExtractFilePath(Application.ExeName)

How to do it with the word I already figured out:

 const NoValid = 'save'; if Pos(Edit1.Text, NoValid) <> 0 then begin ShowMessage('Обрабатывать в файлы в указанной папке запрещено'); Exit; end; 

    2 answers 2

    For the sake of interest, I tried to do this and this is my version (if, of course, I am satisfied):

     procedure TForm1.Button1Click(Sender: TObject); var s, s1, s2: string; p, p2: integer; begin // Строка где ищем s := Edit1.Text; // Слово 1 по которому ищем (с большой буквы) s1 := 'Save'; // Слово 1 по которому ищем (с маленькой буквы) s2 := 'save'; // Осуществляем поиск в Edit используя функцию Pos p := pos(s1, s); p2 := pos(s2, s); { Проверка: Если в строке (Edit) существует слово - Save (с большой буквы) } if p > 0 then // если слово есть begin // если слово первое if ((p = 1) and (s[p + length(s1)] = '\')) // или в середине or ((s[p - 1] = '\') and ((s[p + length(s1)] = '\') // или в конце or (p + length(s1) - 1 = length(s)))) // Тогда очищаем Edit then Edit1.clear; // И выводим сообщение ShowMessage('Обрабатывать в файлы в указанной папке запрещено'); // Выходим Exit end; { Проверка: Если в строке (Edit) существует слово - Save (с маленькой буквы) } if p2 > 0 then // если слово есть begin // если слово первое if ((p2 = 1) and (s[p2 + length(s2)] = '\')) // или в середине or ((s[p2 - 1] = '\') and ((s[p2 + length(s2)] = '\') // или в конце or (p2 + length(s2) - 1 = length(s)))) // Тогда очищаем Edit then Edit1.clear; // И выводим сообщение ShowMessage('Обрабатывать в файлы в указанной папке запрещено'); // Выходим Exit end; end; 
    • one
      You are the second time helping me out. Your code is fine. It also suits me that it prohibits any path with the word Save. Respect YOU Tatiana. - Denis
    • one
      @Denis, I, in my time, was also rescued right there and now they are helping out. By the way, if you do not want to prescribe this code for each button (well, if you have a lot of buttons), then write it in Edit and it will always work both when inserted and when printed in Edit. - Tatiana

    Code with comments:

     // Получаем путь к запрещенной папке, без \ на конце restrictedPath := ExtractFilePath(Application.ExeName) + 'save'; // Проверяемый путь приводим к стандартному виду, // чтобы нам не подсунули ".\save" или "каталог программы\save\..\save\" testPath := ExcludeTrailingPathDelimiter(ExpandFileName(Edit1.Text)); if SysUtils.SameText(testPath, restrictedPath) then begin ShowMessage('Обрабатывать в файлы в указанной папке запрещено'); Exit; end; 
    • And the variables are restrictedPath, testPath: string; Right ? - Denis
    • It turns out if the path is like this: C: \ Users \ adm \ Desktop \ save That does not work. And if just the word in Edit works. - Denis