I am trying to write a BAT script to move files into subfolders depending on the filename - without going into too much detail about the explicit purpose of this, the files are named with 2 descriptive prefixes "PREFIX1_PREFIX2_descriptivefilename" - with an underscore delimiter.
I want to move the files into folders according to a few different rules which are dependent on both PREFIX1 and PREFIX2.
This is intended to be used for an in-house system for organising printed documents and images, which are printed on a range of types of paper. For example's sake, lets say we are working with just 2 PREFIX1 types - Class1 and Class2. PREFIX2 can take several forms denoting paper size, let's say for simplicity, "A6paper", "A5paper", "A4paper" and "Larger".
I need the folders to have slightly different filenames to the prefix as any one folder can contain a mix of both PREFIX1 and PREFIX2 document types, as there are a limited number of print types, let's say the folders we are working with are "A5", "A4", "Other", as files with the A6paper and A5paper prefix will both go into "A5".
Here is my example BAT script:
for /f "tokens=1,2* delims=_" %%a in ('dir /B *_*_*.pdf *_*_*.png') do (
if /I %%a == Class1 (
if /I %%b == A4paper (
if not exist "A4" md "A4"
move "%%a_%%b_%%c" "A4\%%a_%%b_%%c"
)
else if /I %%b == A5paper (
if not exist "A5" md "A5"
move "%%a_%%b_%%c" "A5\%%a_%%b_%%c"
)
else if /I %%b == A6paper (
if not exist "A5" md "A5"
move "%%a_%%b_%%c" "A5\%%a_%%b_%%c"
)
else if /I %%b == Larger (
if not exist "Other" md "Other"
move "%%a_%%b_%%c" "Other\%%a_%%b_%%c"
)
)
else if /I %%a == Class2 (
if /I %%b == A4paper (
if not exist "A4" md "A4"
move "%%a_%%b_%%c" "A4\%%a_%%b_%%c"
)
else if /I %%b == A5paper (
if not exist "A5" md "A5"
move "%%a_%%b_%%c" "A5\%%a_%%b_%%c"
)
else if /I %%b == A6paper (
if not exist "A5" md "A5"
move "%%a_%%b_%%c" "A5\%%a_%%b_%%c"
)
else if /I %%b == Larger (
if not exist "Other" md "Other"
move "%%a_%%b_%%c" "Other\%%a_%%b_%%c"
)
)
else if....
So as you can see this code is already a little unwieldy looking. I am really not sure about the syntax of BAT, whether "else" should be removed entirely as I get some errors using this. Regardless however, the output seems to be nothing like that expected, the code in the nested if clauses seems to be executed randomly regardless of the higher level rules.
I would also ideally like to be able to write something like:
if /I %%b in ("A5paper", "A6paper") (
if not exist "A5" md "A5"
move "%%a_%%b_%%c" "A5\%%a_%%b_%%c"
)
...but this does not seem to be allowed.
Any help would be much appreciated.