0

I have a directory structure that looks like this:

Folder A
 A.PDF
 A_1.TIF
 A_2.TIF
 A_3.TIF

Folder B
 B.PDF
 B_1.TIF
 B_2.TIF
 B_3.TIF

Folder C
 C.PDF
 C_1.TIF
 C_2.TIF
 C_3.TIF

What I want to be able to do is have it so a script sorts the PDFs and TIFFs into separate folders with a specific title, for example:

Folder A
 Representation_1
  A.PDF
 Preservation_1
  A_1.TIF
  A_2.TIF
  A_3.TIF

I've used the following script to read the names of the files and sort them into new folders based off a string in their filename. I was hoping it could be retrofitted to do the same, but using file extensions:

@echo off
setlocal EnableDelayedExpansion

REM Put here the path where the files are:
set "Source=%~dp0"

for %%a in ("%Source%\*") do (
set "File=%%~na"
for /d %%b in ("%Source%\*") do (
set "Folder=%%~nb"
If "!File:~0,18!"=="!Folder:~0,18!" Move "%%a" "%%b"
)
if Exist "%%a" md "!Source!\!File:~0,18!"& move /y "%%a" "!Source!\!File:~0,18!"
)
NoorSafi
  • 3
  • 2
  • I didn't get the Representation, Reservation part – Ricardo Bohner Nov 22 '22 at 14:46
  • Those are the the specific subfolders I want the TIF and PDF files moved into, that said, they can just be titled after the extension and I can bulk rename them after the fact. What's important is that the script generates the folders and moves the files into them. – NoorSafi Nov 22 '22 at 14:53

1 Answers1

0

If I got you right it would be something like this. Go through the subfolders of source, inside the subfolder if there are pdf files create a folder called Representation_1 and move all pdf files to this folder. If there are tiff or tif files move these to a folder called Preservation_1 inside the subfolder.

@echo off

set Source=%userprofile%\desktop\test

pushd "%Source%"

for /f "delims=" %%a in ('dir /b /ad *') do (
                                             dir "%%a\*.pdf">nul && if not exist "%%a\Representation_1" md "%%a\Representation_1"
                                             dir "%%a\*.tif*">nul && if not exist "%%a\Preservation_1" md "%%a\Preservation_1"
                                             for /f "delims=" %%b in ('dir /b /a-d %%a\*') do (
                                                                                               if /i "%%~xb"==".pdf" move "%%a\%%~b" "%%a\Representation_1"
                                                                                               if /i "%%~xb"==".tiff" move "%%a\%%~b" "%%a\Preservation_1"
                                                                                               if /i "%%~xb"==".tif" move "%%a\%%~b" "%%a\Preservation_1"
                                                                                              ) 
                                            )
popd
exit

enter image description here

Ricardo Bohner
  • 3,903
  • 2
  • 18
  • 12