I have a lot of folders and they have the following naming convention:

  • folder1(1924)
  • folder2(2014)
  • folder3(1999)
  • folder4(2001)
  • folder5(1891)

Now I would love to move/find in explorer all the folders where the year (between the ()) is below the year 2000 and above the year 1900

So in the output would be folder1 and folder3.

Is there any script that can do this?

  • Is it year part of the directory name? – Romeo Ninov Jun 8 '15 at 8:31
  • Yea every folder has the year in the name – Kiwi Jun 8 '15 at 8:35

If all those folders are under C:\Source folder then run the following batch file from C:\Source folder itself:

@echo off
set dest=D:\Target folder
for /f "tokens=1,2 delims=()" %%d in ('dir /ad /b') do (
    if %%e gtr 1900 (
        if %%e lss 2000 (
            if not exist "%dest%\%%d(%%e)\" md "%dest%\%%d(%%e)"
            robocopy "%%d(%%e)" "%dest%\%%d(%%e)" /e /is /move
  • It gives the follwoing error ` ERROR 2 (0x00000002) Accessing Source Directory ` – Kiwi Jun 8 '15 at 10:24
  • Found out why, there are some things behind the year aswell ;) fixing that – Kiwi Jun 8 '15 at 10:53

Here is one sample way to do this (assuming all directories are on one level)

find . -type d -name "fold*" -maxdepth 1|awk -F'[()]'  '{if ($2 > 1900 && $2 < 2000) print $0}'

If you want to execute it under Windows OS the samples way is to use some cygwin environment (also mobaxterm work fine) and to run it in terminal. Unfortunately my Windows skills are very modest and I can't rewrite it for native cmd script

  • You might want to include instructions on how the OP can run this in Windows. – Karan Jun 8 '15 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.