@ECHO OFF SETLOCAL enabledelayedexpansion :: THIS SCRIPT FILE NEEDS TO BE IN ASCII FOR THE CHARACTER REPLACEMENTS TO WORK :: If working with utf8 chars to be replaced, set this script file to be in utf-8 and change the codepage, see :: https://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file ::chcp 65001 set origdir=%CD% set odir=..\import if exist %odir% rmdir /S /Q "%odir%" if not exist %odir% mkdir %odir% pushd %odir% set odir=%CD% popd echo ORIG DIR: !origdir! echo OUTPUT DIR: !odir! call :procDir :procDir if not [%1]==[] ( set dirname=%1 rem echo Dirname: !dirname! set dirname=!dirname:"=! rem echo Dirname 2: !dirname! ) else ( set dirname= rem echo empty dir ) ::if not [%dirname%]==[] (set dirname=!dirname:"=!) else (set dirname=) ::echo Dirname is %dirname% :: Recurse through current directory, writing out the metadata.xml file :: https://ss64.com/nt/for_r.html - Recurse through subfolders :: https://ss64.com/nt/syntax-args.html use of parameters :: https://ss64.com/nt/syntax-substring.html :: https://stackoverflow.com/questions/2772456/string-replacement-in-batch-file/2772498 for /r . %%G in (.,*.pdf,*.jpg) do ( set fullpath=%%~fG rem Remove the prefix path of the starting directory denoted by var origdir rem to get the relative path set relpath=%%~fG set foldername=%%~nG rem If dealing with directory, create a folder by the same name inside the output directory rem and write out a metadata.xml file there IF EXIST %%G\NUL ( if "!relpath!" == "!origdir!" ( set relpath= set metapath="!odir!\metadata.xml" ) else ( CALL SET relpath=!!relpath:%origdir%\=!! set metapath="!odir!\!relpath!\metadata.xml" ) echo RELPATH: !relpath! rem echo OUTPUT DIR: "!odir!\!relpath!" echo METAPATH: !metapath! if not exist "!odir!\!relpath!" mkdir "!odir!\!relpath!" echo ^ > !metapath! echo ^<^^!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd"^> >> !metapath! echo ^ >> !metapath! echo ^ >> !metapath! echo ^.*^ >> !metapath! echo ^ >> !metapath! echo ^!foldername!^ >> !metapath! echo ^!foldername!^ >> !metapath! echo ^ >> !metapath! echo ^ >> !metapath! echo ^ >> !metapath! ) else ( rem Dealing with (PDF) files: rem Create a subfolder in import by the same name as the PDF rem then copy the PDF into it rem And create a metadata.xml file in here for it if "!relpath!" == "!origdir!" ( set relpath= set metapath="!odir!\metadata.xml" ) else ( CALL SET relpath=!!relpath:%origdir%\=!! rem Remove .pdf extension from relative path CALL SET relpath=!!relpath:~0,-4!! set metapath="!odir!\!relpath!\metadata.xml" ) echo RELPATH: !relpath! rem echo OUTPUT DIR: "!odir!\!relpath!" echo METAPATH: !metapath! rem foldername is PDF's filename without .pdf extension echo @@@ GOT FILE: !foldername! if not exist "!odir!\!relpath!" mkdir "!odir!\!relpath!" rem if not exist "!odir!\!relpath!\!foldername!\!foldername!.pdf" copy "!fullpath!" "!odir!\!relpath!\!foldername!\." copy "!fullpath!" "!odir!\!relpath!\." rem Replace relative path to file with vertical bars to create the hierarchy classifier meta value needed set line=!relpath:\=^|! echo LINE: !line! to be written out to !metapath! echo ^ > !metapath! echo ^<^^!DOCTYPE DirectoryMetadata SYSTEM "http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd"^> >> !metapath! echo ^ >> !metapath! echo ^ >> !metapath! echo ^.*^ >> !metapath! echo ^ >> !metapath! echo ^!line!^ >> !metapath! echo ^!foldername!^ >> !metapath! echo ^!foldername!^ >> !metapath! echo ^ >> !metapath! echo ^ >> !metapath! echo ^ >> !metapath! ) ) rem exit