source: main/trunk/model-sites-dev/waikato-edu/TC4.12_Math Resources_Activities/maths-activities/DATA-PREPARATION/procMath.bat@ 33691

Last change on this file since 33691 was 33691, checked in by ak19, 5 years ago

Math collection renames and moving things about

File size: 4.5 KB
Line 
1@ECHO OFF
2SETLOCAL enabledelayedexpansion
3
4:: THIS SCRIPT FILE NEEDS TO BE IN ASCII FOR THE CHARACTER REPLACEMENTS TO WORK
5:: If working with utf8 chars to be replaced, set this script file to be in utf-8 and change the codepage, see
6:: https://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file
7::chcp 65001
8
9set origdir=%CD%
10
11set odir=..\import
12if exist %odir% rmdir /S /Q "%odir%"
13
14if not exist %odir% mkdir %odir%
15pushd %odir%
16set odir=%CD%
17popd
18
19echo ORIG DIR: !origdir!
20echo OUTPUT DIR: !odir!
21call :procDir
22
23:procDir
24if not [%1]==[] (
25 set dirname=%1
26 rem echo Dirname: !dirname!
27 set dirname=!dirname:"=!
28 rem echo Dirname 2: !dirname!
29) else (
30 set dirname=
31 rem echo empty dir
32)
33::if not [%dirname%]==[] (set dirname=!dirname:"=!) else (set dirname=)
34
35::echo Dirname is %dirname%
36
37
38:: Recurse through current directory, writing out the metadata.xml file
39:: https://ss64.com/nt/for_r.html - Recurse through subfolders
40:: https://ss64.com/nt/syntax-args.html use of parameters
41:: https://ss64.com/nt/syntax-substring.html
42:: https://stackoverflow.com/questions/2772456/string-replacement-in-batch-file/2772498
43for /r . %%G in (.,*.pdf,*.jpg) do (
44 set fullpath=%%~fG
45
46 rem Remove the prefix path of the starting directory denoted by var origdir
47 rem to get the relative path
48 set relpath=%%~fG
49 set foldername=%%~nG
50
51
52 rem If dealing with directory, create a folder by the same name inside the output directory
53 rem and write out a metadata.xml file there
54 IF EXIST %%G\NUL (
55
56 if "!relpath!" == "!origdir!" (
57 set relpath=
58 set metapath="!odir!\metadata.xml"
59 ) else (
60 CALL SET relpath=!!relpath:%origdir%\=!!
61 set metapath="!odir!\!relpath!\metadata.xml"
62 )
63 echo RELPATH: !relpath!
64 rem echo OUTPUT DIR: "!odir!\!relpath!"
65 echo METAPATH: !metapath!
66
67 if not exist "!odir!\!relpath!" mkdir "!odir!\!relpath!"
68
69 echo ^<?xml version="1.0" encoding="UTF-8"?^> > !metapath!
70 echo ^<^^!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd"^> >> !metapath!
71 echo ^<DirectoryMetadata^> >> !metapath!
72 echo ^<FileSet^> >> !metapath!
73 echo ^<FileName^>.*^</FileName^> >> !metapath!
74 echo ^<Description^> >> !metapath!
75 echo ^<Metadata mode="accumulate" name="er.Crate"^>!foldername!^</Metadata^> >> !metapath!
76 echo ^<Metadata mode="accumulate" name="er.CrateName"^>!foldername!^</Metadata^> >> !metapath!
77 echo ^</Description^> >> !metapath!
78 echo ^</FileSet^> >> !metapath!
79 echo ^</DirectoryMetadata^> >> !metapath!
80 ) else (
81 rem Dealing with (PDF) files:
82 rem Create a subfolder in import by the same name as the PDF
83 rem then copy the PDF into it
84 rem And create a metadata.xml file in here for it
85
86 if "!relpath!" == "!origdir!" (
87 set relpath=
88 set metapath="!odir!\metadata.xml"
89 ) else (
90 CALL SET relpath=!!relpath:%origdir%\=!!
91 rem Remove .pdf extension from relative path
92 CALL SET relpath=!!relpath:~0,-4!!
93 set metapath="!odir!\!relpath!\metadata.xml"
94 )
95 echo RELPATH: !relpath!
96 rem echo OUTPUT DIR: "!odir!\!relpath!"
97 echo METAPATH: !metapath!
98
99 rem foldername is PDF's filename without .pdf extension
100 echo @@@ GOT FILE: !foldername!
101 if not exist "!odir!\!relpath!" mkdir "!odir!\!relpath!"
102 rem if not exist "!odir!\!relpath!\!foldername!\!foldername!.pdf" copy "!fullpath!" "!odir!\!relpath!\!foldername!\."
103 copy "!fullpath!" "!odir!\!relpath!\."
104
105 rem Replace relative path to file with vertical bars to create the hierarchy classifier meta value needed
106 set line=!relpath:\=^|!
107 echo LINE: !line! to be written out to !metapath!
108
109 echo ^<?xml version="1.0" encoding="UTF-8"?^> > !metapath!
110 echo ^<^^!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd"^> >> !metapath!
111 echo ^<DirectoryMetadata^> >> !metapath!
112 echo ^<FileSet^> >> !metapath!
113 echo ^<FileName^>.*^</FileName^> >> !metapath!
114 echo ^<Description^> >> !metapath!
115 echo ^<Metadata mode="accumulate" name="er.CrateHierarchy"^>!line!^</Metadata^> >> !metapath!
116 echo ^<Metadata mode="accumulate" name="er.Activity"^>!foldername!^</Metadata^> >> !metapath!
117 echo ^<Metadata mode="accumulate" name="er.ActivityName"^>!foldername!^</Metadata^> >> !metapath!
118 echo ^</Description^> >> !metapath!
119 echo ^</FileSet^> >> !metapath!
120 echo ^</DirectoryMetadata^> >> !metapath!
121
122 )
123
124)
125
126
127rem exit
Note: See TracBrowser for help on using the repository browser.