3

I have .pdf files with the date at the end of their name, e.g. "BINNAWAY - WERRIS CK - 483.94 - 120612".

I have folders named after the dates of each of these files e.g. "12-06-2012".

How can I move all the files into their corresponding folders based on the date in their name?

I am on Windows XP.

Would appreciate any advice.

Thanks

An Dorfer
  • 1,178
Michael
  • 31

3 Answers3

1

@and31415 Your code has worked on some of the files. But there are many others where it hasn't worked because all files are not in the format <name1> - <name2> - <some numbers> - <date> as assumed. Some may be in the format <name1> - <some numbers> - <date> or something else slightly different. However, all files will end with - <date>, in the following possible arrangements: "- 120612", "- 120612 (1)", "- 120612 (2)", or "- No Date". How can I make it so that only the text after the last hyphen is considered?

Appreciate all your help.

Michael

0

The following batch script assumes that:

  • All file names are in the format <name> - [...] - <some numbers> - <date>.
  • All years are in the range 2000-2099.

Just copy and paste the code in a new text file and save it as MoveFiles.cmd (or whatever as long as you keep the .cmd extension).

@echo off
setlocal
setlocal enabledelayedexpansion

for /r %%A in (*.pdf) do (
set date=
call :getLastToken "%%~nA"
for /f "tokens=1" %%B in ("!last!") do set date=%%~B
echo "!date!"|findstr "^\"[0-3][0-9][0-1][0-9]*\"$" >nul
if !errorlevel! == 0 (
set targetFolder=!date:~0,2!-!date:~2,2!-20!date:~4,2!
if not exist "!targetFolder!\" md "!targetFolder!"
move "%%~fA" "!targetFolder!" >nul
))

endlocal & exit /b

:getLastToken
set "str=%~1"
:loop
for /f "tokens=1* delims=-" %%A in ("%str%") do (
set "last=%%A"
set "str=%%B"
)
if defined str goto :loop
exit /b

How it works

  1. List all .pdf files in the current folder.

  2. Get each file name and tokenize it.

  3. Ensure the retrieved characters are actually numbers:

    The variable is echoed and then piped to the findstr command, which will use a regular expression to match alphabetical characters.

    • ^ matches beginning of line;
    • \" matches a straight quote character (");
    • [0-9] defines a character class which matches any character from 0 to 9;
    • * repeats zero or more occurrences of the previous class;
    • $ matches end of line.

    Additionally, the regular expression will try to match as much valid dates as possible. The !errorlevel! variable will be set to 0 if there's a match, or 1 otherwise. The findstr output will be then redirected to nul, thus ignored.

  4. Create the target folder in case it doesn't exist already.

  5. Move the current file to the related folder.

Further reading

and31415
  • 14,901
0

Hello just thought I'd update on the progress I've made and provide some feedback to my own questions. To account for files with the naming format <name1> - <some numbers> - <date> instead of <name1> - <name2> - <some numbers> - <date> as assumed by @and31415 in the script he provided, I have just changed tokens=4 to tokens=3 in a fresh .cmd file. This worked. But still, many files were not processed by these batch scripts. I figured out that the script was only processing some of the files in the folder (it only processed 50 to 100 pdf files and I have thousands of pdf files to go through), so I created a new folder and dumped the excess pdfs in there and ran the batch scripts in there. Still many pdf files not processed but Im going through them processing 50-100 pdfs each in a new folder and it should be done fairly quickly. Not ideal but still much better than doing it manually one by one! Thanks for your help @and31415 :)