DIR /B /AD "!sPDFName:~0,4!"
As written in the box above, this
DIR command lists the directory ( /AD ) in bare format ( /B ) that is named after
the first four characters ( :~0,4 ) of the variable sPDFName whose
expansion is delayed ( !
<VariableName>! ).
The variable sPDFName should contain a
String value of the
PDF file
Name. To see what it should be we'll need to trace the code:
FOR /F "delims=" %%G IN ('DIR /B /A-D "*.pdf" 2^>NUL') DO (
For each file ending in ".pdf" that is not a directory, do the following.
SET "sPDFName=%%~nxG"
The
name and e
xtension ( nx ) of the current file ( %%G ) without surrounding quotes ( ~ ) is set to the sPDFName variable.
IF "!sPDFName:~0,1!"=="_" (SET "sPDFName=!sPDFName:~1!")
If the first character ( :~0,1 ) of the variable sPDFName is an underscore ( _ ) then set the value of the variable to itself ( sPDFName ) but skip the first character ( :~1 ). For example, if the file name is "_EKAH-ADC_en.pdf" then this line will set the variable equal to "EKAH-ADC_en.pdf".
To make it easier for me to explain, let's split the next line and explain the parts:
DIR /B /AD "!sPDFName:~0,4!"
If it exists, list the directory that is named for the first four characters ( :~0,4 ) of the variable sPDFName.
2^>^&1 1^>NUL
Let's make this easier to read by removing the escape characters ( ^ ) so we can see the
output redirections at work:
2>&1 1>NUL
Send the error text output ( 2 ) as the new text output ( >&1). And ignore the original text output ( 1>NUL ).
Now let's combine the previous two segments:
DIR /B /AD "!sPDFName:~0,4!" 2>&1 1>NUL
If the directory named for the first four characters of the variable sPDFName does not exist then give us the error message.
Now let's try interpreting the entire FOR...DO expression:
FOR /F "delims=" %%H IN ('DIR /B /AD "!sPDFName:~0,4!" 2^>^&1 1^>NUL') DO (
If we receive an error message from the enclosed DIR command, do the following:
MD "!sPDFName:~0,4!"
Make a directory named after the first four characters ( :~0,4 ) of the variable sPDFName.
To summarize let's say the current file we're working on is "_EKAH-ADC_en.pdf":
- First, sPDFName is set to the current file name ending in ".pdf": "_EKAH-ADC_en.pdf"
- Next we skip the first character so the variable sPDFName is now "EKAH-ADC_en.pdf". This is the final change we make to the actual variable.
- Finally, we use the following substring of the variable sPDFName several times. We skip zero characters and use the next four with the expression "!sPDFName:~0,4!". ("EKAH-ADC_en.pdf" becomes "EKAH", but the contents of the variable never change.)
EDIT: I suppose this line:
FOR /F "delims=" %%H IN ('DIR /B /AD "!sPDFName:~0,4!" 2^>^&1 1^>NUL') DO (ECHO=MD "!sPDFName:~0,4!")
...could be simplified to this:
DIR /B /AD "!sPDFName:~0,4!" 2>NUL || ECHO=MD "!sPDFName:~0,4!"
Here's the final code:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%G IN ('DIR /B /A-D "*.pdf" 2^>NUL') DO (
SET "sPDFName=%%~nxG"
IF "!sPDFName:~0,1!"=="_" (SET "sPDFName=!sPDFName:~1!")
DIR /B /AD "!sPDFName:~0,4!" 2>NUL || MD "!sPDFName:~0,4!"
MOVE "%%~nxG" "!sPDFName:~0,4!\"
)
PAUSE
The "ECHO=" have already been removed.
This post has been edited by 5eraph: 17 February 2012 - 10:05 AM