This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.bat
452 lines (379 loc) · 16.3 KB
/
process.bat
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
:: ===========================================================================
:: NAME: commands.bat
:: AUTHOR: Manuel Gil.
:: DATE: 11/26/2017.
:: VERSION: 1.0.3.0
:: ===========================================================================
:: This script contains commands for handling folders and files.
:: @args - %* = All arguments.
:: %~1 = Function name.
:: %~2 = Second argument.
:: ...
:: %~9 = Last argument.
:: /source: = Source folder.
:: /destination: = Destination folder.
:: /custom: = Modification folder.
:: /version: = System version.
:: Each function has a separate parameter handling.
:: void main(args[]);
:main
echo off
title Process Manager
cls
:: Gets all arguments
set args=%*
:: Remove first argument
call set args=%%args:%~1=%%
:: Replace '{' to '"'
set args=%args: {="%
:: Replace '}' to '"'
set args=%args:}="%
:: Add '"' before/after to ','
set args=%args:,=","%
:: Sets the Variables
for %%a in (%args%) do (
call :initValues %%a
)
:: Gets date
for /f "tokens=1-5 delims=/., " %%a in ("%date%") do (
set CONST_DATE=%%d%%c%%b%%a
)
:: Sets the Log File
set log=%~dp0log%CONST_DATE%.log
:: Sets the Catalog File
set catalog=%~dp0catalog.ini
:: If the Source Folder not exist
if not exist "%CONST_SOURCE%" (
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. Tool for efficient file copying >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. ERROR: The folder %CONST_SOURCE% does not exist. >>"%log%"
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo.ERROR: The folder %CONST_SOURCE% does not exist.
pause
goto :eof
)
:: Select the Function
if %~1 EQU createCopy: (
goto createCopy
) else if %~1 EQU patchFiles: (
goto patchFiles
) else if %~1 EQU extractCustomization: (
goto extractCustomization
) else if %~1 EQU createList: (
goto createList
) else (
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. Tool for efficient file copying >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. ERROR: Could not access function "%~1". >>"%log%"
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo.ERROR: Could not access function "%~1".
pause
)
goto :eof
:: This method starts the necessary variables for the use of the script.
:: @param - arguments = Arguments to evaluate.
:: void initValues(arguments);
:initValues
set arg=%~1
:: Sets the Source Folder
set arg=%arg:source:=%
if "%~1" NEQ "%arg%" (
set CONST_SOURCE=%arg%
)
:: Sets the Destination Folder
set arg=%arg:destination:=%
if "%~1" NEQ "%arg%" (
set CONST_DESTINATION=%arg%
)
:: Sets the Custom Folder
set arg=%arg:custom:=%
if "%~1" NEQ "%arg%" (
set CONST_CUSTOM=%arg%
)
:: Sets the OS Version
set arg=%arg:version:=%
if "%~1" NEQ "%arg%" (
set CONST_VERSION=%arg%
)
goto :eof
:: This method copies the relative path of a file or folder.
:: @param - source = Source folder.
:: @param - value = path to validate.
:: void pathRelativeFolder(source, value);
:pathRelativeFolder
set source=%~1
set value=%~2
call set dir=%%value:%source%=%%
set dir=.%dir%
echo.dir=%dir%>>"%catalog%"
goto :eof
:: This method copies the relative path of a file or folder.
:: @param - source = Source folder.
:: @param - value = path to validate.
:: void pathRelativeFile(source, value);
:pathRelativeFile
set source=%~1
set value=%~2
call set file=%%value:%source%=%%
set file=.%file%
echo.file=%file%>>"%catalog%"
goto :eof
:: This method creates a catalog preserving the structure of the directories.
:: @param - source = Source folder.
:: void createCatalog(source);
:createCatalog
set source=%~1
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. A catalog is created preserving the structure of the directories. >>"%log%"
echo. >>"%log%"
echo.A catalog is created preserving the structure of the directories.
:: Creates the Catalog File
echo.# A catalog of files is created>"%catalog%"
echo.# Source: "%source%\">>"%catalog%"
echo.# >>"%catalog%"
echo.>>"%catalog%"
echo.[path]>>"%catalog%"
echo.dir=.\>>"%catalog%"
:: Gets folders
for /f "tokens=*" %%a in ('dir /b /s /a:d "%source%"') do (
call :pathRelativeFolder "%source%" "%%a"
)
:: Gets files
for /f "tokens=*" %%a in ('dir /b /s /a:-d "%source%"') do (
call :pathRelativeFile "%source%" "%%a"
)
goto :eof
:: This method creates folders in a target location.
:: @param - destination = Destination folder.
:: @param - name = Folder name.
:: void createFolder(destination, name);
:createFolder
set destination=%~1
set name=%~2
set name=%name:.\=%
set dir=%destination%\%name%
echo. Start: %date% %time%, >>"%log%"
echo. Action: Creating the folder %dir%. >>"%log%"
echo.Creating the folder %dir%.
mkdir "%dir%"
goto :eof
:: This method copies the files preserving the structure.
:: @param - source = Source folder.
:: @param - destination = Destination folder.
:: @param - file = File name.
:: void copyFile(source, destination, file);
:copyFile
:: Sets the filename
set name=%~nx3
:: Sets folder
set dir=%~3
set dir=%dir:.\=%
:: Sets Source Folder
set source=%~1\%dir%
call set source=%%source:%name%=%%\
set source=%source:\\=%
:: Sets Destination Folder
set destination=%~2\%dir%
call set destination=%%destination:%name%=%%\
set destination=%destination:\\=%
:: Count the processed files
set /a process+=1
echo. Start: %date% %time%, >>"%log%"
echo. Action: Copying the file %name%. >>"%log%"
echo.Copying the file %name%.
:: If Windows Server 2003
if "%version%" EQU "5.2.3790" (
:: Checks the ROBOCOPY Commands
robocopy /?>nul
if %errorlevel% EQU 9009 (
echo.Make sure you install Windows Server 2003 Resource Kit Tools and try again.
pause
goto :eof
)
)
if exist "%source%\%name%" (
set /a found+=1
cd \
cd /d "%source%"
:: If Windows Server 2008 R2 SP1
if "%version%" EQU "6.1.7601" (
:: The /MT parameter applies to Windows Server 2008 R2 and Windows 7
robocopy "%source%" "%destination%" "%name%" /mt:128 /njh /ns /np /ts /log+:"%log%"
) else (
robocopy "%source%" "%destination%" "%name%" /njh /ns /np /ts /log+:"%log%"
)
) else (
echo. Start: %date% %time%, >>"%log%"
echo. ERROR: The file %name% does not exist in source folder. >>"%log%"
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo.ERROR: The file %name% does not exist in source folder.
echo.
)
goto :eof
:: This method creates a backup of the files and folders.
:: void createCopy();
:createCopy
:: Sets Backup Folder
set backup=%CONST_DESTINATION%\Backups\%CONST_DATE%
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. Tool for efficient file copying >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. Backup files and folders. >>"%log%"
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. Action: Creating folder "Backup". >>"%log%"
:: Creates the Backup Folder
mkdir "%backup%"
echo. Start: %date% %time%, >>"%log%"
echo. Action: Copying files starts. >>"%log%"
echo.Copying files starts.
:: If Windows Server 2003
if "%version%" EQU "5.2.3790" (
:: Checks the ROBOCOPY Commands
robocopy /?>nul
if %errorlevel% EQU 9009 (
echo.Make sure you install Windows Server 2003 Resource Kit Tools and try again.
pause
goto :eof
)
)
cd \
cd /d "%CONST_SOURCE%"
:: If Windows Server 2008 R2 SP1
if "%version%" EQU "6.1.7601" (
:: The /MT parameter applies to Windows Server 2008 R2 and Windows 7
robocopy "%CONST_SOURCE%" "%backup%" /e /mt:128 /tee /njh /ns /np /ts /log+:"%log%"
) else (
robocopy "%CONST_SOURCE%" "%backup%" /e /tee /njh /ns /np /ts /log+:"%log%"
)
if %ERRORLEVEL% EQU 0 (
echo. Enf: %date% %time%, >>"%log%"
echo. Action: The process has ended correctly. >>"%log%"
echo.The process has ended correctly.
) else if %ERRORLEVEL% EQU 1 (
echo. Enf: %date% %time%, >>"%log%"
echo. Action: The process has ended correctly. >>"%log%"
echo.The process has ended correctly.
) else (
echo. Enf: %date% %time%, >>"%log%"
echo. ERROR: An error %ERRORLEVEL% has occurred in the command. >>"%log%"
echo.ERROR: An error %ERRORLEVEL% has occurred in the command.
)
pause
echo.------------------------------------------------------------------------------->>"%log%"
goto :eof
:: This method copies the update files to the destination folder.
:: void patchFiles();
:patchFiles
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. Tool for efficient file copying >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. The update files are copied to the destination folder . >>"%log%"
echo. >>"%log%"
call :createCatalog "%CONST_SOURCE%"
echo. Start: %date% %time%, >>"%log%"
echo. Action: Folder creation starts. >>"%log%"
echo.Folder creation starts.
:: If Windows Server 2003
if "%version%" EQU "5.2.3790" (
:: Checks the ROBOCOPY Commands
robocopy /?>nul
if %errorlevel% EQU 9009 (
echo.Make sure you install Windows Server 2003 Resource Kit Tools and try again.
pause
goto :eof
)
)
cd \
cd /d "%CONST_SOURCE%"
:: If Windows Server 2008 R2 SP1
if "%version%" EQU "6.1.7601" (
:: The /MT parameter applies to Windows Server 2008 R2 and Windows 7
robocopy "%CONST_SOURCE%" "%CONST_DESTINATION%" /e /mt:128 /tee /njh /ns /np /ts /log+:"%log%"
) else (
robocopy "%CONST_SOURCE%" "%CONST_DESTINATION%" /e /tee /njh /ns /np /ts /log+:"%log%"
)
if %ERRORLEVEL% EQU 0 (
echo. Enf: %date% %time%, >>"%log%"
echo. Action: The process has ended correctly. >>"%log%"
echo.The process has ended correctly.
) else if %ERRORLEVEL% EQU 1 (
echo. Enf: %date% %time%, >>"%log%"
echo. Action: The process has ended correctly. >>"%log%"
echo.The process has ended correctly.
) else (
echo. Enf: %date% %time%, >>"%log%"
echo. ERROR: An error %ERRORLEVEL% has occurred in the command. >>"%log%"
echo.ERROR: An error %ERRORLEVEL% has occurred in the command.
)
pause
echo.------------------------------------------------------------------------------->>"%log%"
goto :eof
:: This method extracts custom files.
:: void extractCustomization();
:extractCustomization
echo. >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. Tool for efficient file copying >>"%log%"
echo.------------------------------------------------------------------------------->>"%log%"
echo. >>"%log%"
echo. Start: %date% %time%, >>"%log%"
echo. Action: Folder creation starts. >>"%log%"
echo. >>"%log%"
:: Creates the Catalog file
call :createCatalog "%CONST_CUSTOM%"
echo. Start: %date% %time%, >>"%log%"
echo. Action: Folder creation starts. >>"%log%"
echo.Folder creation starts.
:: Creates the Destination Folder
if not exist "%CONST_DESTINATION%" (
mkdir "%CONST_DESTINATION%"
)
:: Creates the Destination Subfolders
for /f "tokens=2 delims==" %%a in ('findstr "dir" "%catalog%"') do (
call :createFolder "%CONST_DESTINATION%" "%%a"
)
echo. Start: %date% %time%, >>"%log%"
echo. Action: Copying files starts. >>"%log%"
echo.Copying files starts.
set process=0
set found=0
:: Copy Files
for /f "tokens=2 delims==" %%a in ('findstr "file" "%catalog%"') do (
call :copyFile "%CONST_SOURCE%" "%CONST_DESTINATION%" "%%a"
)
echo.Were processed %process% file(s).
echo.Were found %found% coincidences.
pause
echo.------------------------------------------------------------------------------->>"%log%"
goto :eof
:: This method creates a list of files and folders.
:: void createList();
:createList
set list=%~dp0list.txt
echo.Start: %date% %time%,>>"%list%"
echo.Source: "%CONST_SOURCE%\">>"%list%"
echo.>>"%list%"
dir /s /a:-d "%CONST_SOURCE%">>"%list%"
echo.
echo.A list of files and folders has been created in %list%.
pause
goto :eof