-
Notifications
You must be signed in to change notification settings - Fork 3
/
REPLACE.PAS
185 lines (175 loc) · 4.22 KB
/
REPLACE.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2022
@website(https://www.gladir.com/msdos0)
@abstract(Target: Turbo Pascal, Free Pascal)
}
Program Replace;
Uses Crt,DOS;
Var
Language:(_French,_English,_Germany,_Italian,_Spain);
TmpLanguage:String;
Option:Set Of (_P);
YesReplace:Boolean;
I:Integer;
Source,Target:String;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
Function FileExist(Name:String):Boolean;
Var
Rec:SearchRec;
Begin
FindFirst(Name,AnyFile,Rec);
FileExist:=DosError=0;
End;
Function DeleteFile(Name:String):Boolean;
Var
F:File;
Begin
{$I-}
Assign(F,Name);
Erase(F);
{$I+}
DeleteFile:=IoResult=0;
End;
Function SplitFileName(s:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
FSplit(S,D,N,E);
Splitfilename:=N+E;
End;
Function CopyFile(Source,Target:String):Boolean;
Var
SourceFile,TargetFile:File;
RecordsRead:Integer;
Buffer:Array[1..1000]of Byte;
Begin
CopyFile:=False;
Assign(SourceFile,Source);
{$I-}Reset(SourceFile,1);{$I+}
If IOResult<>0Then Begin
WriteLn('Fichier source introuvable ',Source);
Exit;
End;
Assign(TargetFile,Target);
{$I-}Rewrite(TargetFile,1);
BlockRead(SourceFile,Buffer,SizeOf(Buffer),RecordsRead);
While RecordsRead>0 do Begin
BlockWrite(TargetFile,Buffer,RecordsRead);
BlockRead(SourceFile,Buffer,SizeOf(Buffer),RecordsRead);
End;
Close(SourceFile);
Close(TargetFile);
{$I+}
CopyFile:=True;
End;
Function MoveFile(Source,Target:String):Boolean;
Var
F:File;
Begin
If(Source='')or(Target='')Then Begin
MoveFile:=False;
Exit;
End;
Source:=FExpand(Source);
Target:=FExpand(Target);
If(Source[1]<>Target[1])and(Source[2]=':')Then Begin { Unite de disque different ?}
{ Copie le fichier }
MoveFile:=CopyFile(Source,Target);
{ Supprime le fichier }
{$I-}Assign(F,Source);
Erase(F);
{$I+}
End
Else
Begin
{$I-}
Assign(F,Source);
Rename(F,Target);
MoveFile:=IOResult=0;
{$I+}
End;
End;
BEGIN
Language:=_French;
TmpLanguage:=GetEnv('LANGUAGE');
If TmpLanguage<>''Then Begin
If TmpLanguage[1]='"'Then TmpLanguage:=Copy(TmpLanguage,2,255);
If StrToUpper(Copy(TmpLanguage,1,2))='EN'Then Language:=_English Else
If StrToUpper(Copy(TmpLanguage,1,2))='GR'Then Language:=_Germany Else
If StrToUpper(Copy(TmpLanguage,1,2))='IT'Then Language:=_Italian Else
If StrToUpper(Copy(TmpLanguage,1,2))='SP'Then Language:=_Spain;
End;
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
Case Language of
_Germany:Begin
WriteLn('Ersetzt Dateien');
WriteLn;
WriteLn('REPLACE [Laufwerk1:][Pfad1]Datname [Laufwerk2:][Pfad2] [/P]');
WriteLn;
WriteLn('/P Fordert vor dem Ersetzen einer Datei oder Hinzufgen ');
WriteLn(' einer neuen Datei zur Best„tigung auf.');
End;
_English:Begin
WriteLn('Replaces files.');
WriteLn;
WriteLn('REPLACE [drive1:][[path1]filename [drive2:][path2] [/P]');
WriteLn;
WriteLn('/P Prompt for confirmation before replacing a file or adding');
WriteLn(' a source files.');
End;
Else Begin
WriteLn('REPLACE : Cette commande permet de remplacer un fichier.');
WriteLn;
WriteLn('Syntaxe : REPLACE source destination [/P]');
WriteLn;
WriteLn(' /P Demande une confirmation avant de remplacer');
End;
End;
End
Else
If ParamCount>=2Then Begin
Option:=[];
Source:='';
Target:='';
For I:=1 to ParamCount do Begin
If StrToUpper(ParamStr(I))='/P'Then Include(Option,_P) Else
If Source<>''Then Target:=FExpand(ParamStr(I))
Else Source:=FExpand(ParamStr(I));
End;
YesReplace:=True;
If FileExist(Target)Then Begin
If(_P in Option)Then Begin
Write('Remplacer ',Target,' (O/N) ? ');
If ReadKey in['Y','y','O','o']Then Begin
WriteLn('Oui');
If Not DeleteFile(Target)Then Begin
WriteLn('Impossible de remplacer le fichier cible : ',Target);
Halt;
End;
End
Else
Begin
YesReplace:=False;
WriteLn('Non');
End;
End;
End;
If(YesReplace)Then Begin
If Not MoveFile(Source,Target)Then Begin
WriteLn('Erreur de remplacement du fichier.');
End;
End;
End
Else
WriteLn('Nombre de parametre invalide');
END.