Move groups of folders to new folders with script

justearth
justearth used Ask the Experts™
on
Hello,
My sub directories have the following naming scheme and I looking for a script to help me organize them.

aaa_xxx
aaa_xxxc_x or aaa_xxxc_xx

Where aaa = dif, dur, dir or glo
Where xxx = 001 --> ~450 not continuous
Where c = c
and where x or xx after c = 0 - 11

These are sub directory names, I'd like them place into folders based on

aaa with no c
and aaa with c

e.g.

dif_001 - dif_450 in the same new sub directory

dif_001c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 - dif_450c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 into the same new sub directory

and the same thing for dur, dir and glo.

I'd appreciate annotate code / script.

Thanks,
JE
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
@echo off
setlocal enabledelayedexpansion

if not exist "aaa with no c\" (
   md "aaa with no c"
)

if not exist "and aaa with c\" (
   md "and aaa with c"
)

for /d %%a in (*) do (
   set folder=%%a
   if /i "!folder:~7,1!" equ "c" (
      move "%%a" "and aaa with c\"
   ) else (
      move "%%a" "aaa with no c\"
   )
)

Author

Commented:
t0t0:
Where do I add the input directory?

Thanks,
JE

Commented:
okay, back i a mo
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
t0t0:

Thanks for the follow-up. However, I am unsure what it means.

Would:

set root=C:\folder1

work for choosing the main directory?

Thanks again,
JE

Commented:
Please try this
place this batch file in the folder which contains your AAA_XXX and AAA_XXXc_X? folders and run it. You will be asked to enter the name where you want to move your folders to. You should type in a new or existing foldername or path (without quotes and no trailing '\' character)


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

CLS
ECHO First we will move C_X and C_XX type folders to a new destination
ECHO Then we will move AAA_XXX type folders
ECHO.
SET /P destination=Enter foldername where you want to move C_X and C_XX type folders to:

IF NOT EXIST "%destination%\" MD "%destination%"
FOR /D %%a IN (???_???c_??) DO (
   SET folder=%%a
      IF /I "!folder:~7,1!" EQU "c" (
         MOVE "%%a" "%destination%"
      )
   )
)

ECHO.
ECHO All C_X and C_XX type folders have been moved to %destination%

ECHO.
ECHO.
ECHO.
SET /P destination=Enter foldername where you want to move AAA_XXX type folders to:

IF NOT EXIST "%destination%\" MD "%destination%"
FOR /D %%a IN (???_???) DO (
   SET folder=%%a
      MOVE "%%a" "%destination%"
   )
)

ECHO.
ECHO All AAA_XXX type folders have been moved to %destination%
ECHO.

Commented:
Apologies for rushing the previous replies. Here;s a clearer version;


@ECHO OFF
CLS
ECHO First we will move C_X and C_XX type folders to a new destination
ECHO Then we will move AAA_XXX type folders
ECHO.

SET /P destination=Enter foldername where you want to move C_X and C_XX type folders to:
IF NOT EXIST "%destination%\" MD "%destination%"

FOR /D %%a IN (???_???c_??) DO MOVE "%%a" "%destination%"
ECHO.
ECHO All C_X and C_XX type folders have been moved to %destination%

ECHO.
ECHO.

SET /P destination=Enter foldername where you want to move AAA_XXX type folders to:
IF NOT EXIST "%destination%\" MD "%destination%"

FOR /D %%a IN (???_???) DO MOVE "%%a" "%destination%"
ECHO.
ECHO All AAA_XXX type folders have been moved to %destination%

ECHO.

Commented:
If you don't want to interact with the batch file, you can modify the following code (instructions are included for this directly infront of the two lines which require editing).

Edit the lines: SET aaa_xxx= and SET aaa_xxxc= to include a foldername (or path) ie,

   SET aaa_xxx=NoCFiles
   SET aaa_xxxc=CFiles

or even:

   SET aaa_xxx=C:\Folder1\NoCFiles
   SET aaa_xxxc=C:\Folder1\CFiles



@ECHO OFF
REM Set the following variables to the names of the folders you want ot move files to
REM
REM For example, to move: AAA_XXX type files to: C:\No_C_Files
REM then change the line: SET aaa_xxx= to: SET aaa_xxx=C:\No_C_Files
REM
REM Similarly, to move: AAA_XXXC_X (and XX) type files to just: CFiles
REM then change the line: SET aaa_xxxc= to: SET aaa_xxxc=CFiles

SET aaa_xxx=
SET aaa_xxxc=

IF NOT EXIST "%aaa_xxx%\" MD "%aaa_xxx%"
IF NOT EXIST "%aaa_xxxc%\" MD "%aaa_xxxc%"

FOR /D %%a IN (???_???) DO MOVE "%%a" "%aaa_xxx%"
FOR /D %%a IN (???_???c_??) DO MOVE "%%a" "%aaa_xxxc%"

Author

Commented:
Thanks for all the follow-ups.  Its sorta working. Its not working at all for the xxxc folders, i.e. it doesn't move anything but says it has done so.  And 2, I wanted to have seperate output for each of the prefixes, from above:
---
e.g.

dif_001 - dif_450 in the same new sub directory

dif_001c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 - dif_450c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 into the same new sub directory

and the same thing for dur, dir and glo.
---

Is this possible too? This would result in a total of eight new sub-directories (4 prefixes and XXX or XXXC)  

Thanks again for all of the scripts, patience and annotation.

Cheers,
JE

Commented:
No probs... back in a mo

Commented:
Please try the following. Remember to edit the 'SET' statements as indicated in the batch file.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM Set the following variables to where you want ot move your folders to
REM
REM For example, to move: DIF_xxxC_xx type files to: C:\Folder_DIF_C_Files
REM change the line: SET dif_xxxc= to: SET dif_xxxc=C:\Folder_DIF_C_Files
REM
REM No need to specify the full path if the new folders are created in the current folder,
REM just specify the foldername itself as in: SET dif_xxxc=Folder_DIF_C_Files

SET dif_xxx=folder_dif_files
SET dir_xxx=folder_dir_files
SET dur_xxx=folder_dur_files
SET glo_xxx=folder_glo_files

SET dif_xxxc=folder_dif_c_files
SET dir_xxxc=folder_dir_c_files
SET dur_xxxc=folder_dur_c_files
SET glo_xxxc=folder_glo_c_files

FOR %%a IN (dif dir dur glo) DO (
   IF NOT EXIST "!%%a_xxx!\" MD "!%%a_xxx!"
   IF NOT EXIST "!%%a_xxxc!\" MD "!%%a_xxxc!"
)

FOR %%a IN (dif dir dur glo) DO (
echo a[%%a]
   FOR /F %%b IN ('DIR /AD /B %%a_???c_??') DO (
      MOVE %%b "!%%a_xxxc!"
   )
   FOR /F %%b IN ('DIR /AD /B %%a_???') DO (
      MOVE %%b "!%%a_xxx!"
   )
)
Commented:
Sorry, left a rogue line in there - also added more comments

Please try the following. Remember to edit the 'SET' statements as indicated in the batch file.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM Set the following variables to where you want ot move your folders to
REM
REM For example, to move: DIF_xxxC_xx type files to: C:\Folder_DIF_C_Files
REM change the line: SET dif_xxxc= to: SET dif_xxxc=C:\Folder_DIF_C_Files
REM
REM No need to specify the full path if the new folders are created in the current folder,
REM just specify the foldername itself as in: SET dif_xxxc=Folder_DIF_C_Files

SET dif_xxx=folder_dif_files
SET dir_xxx=folder_dir_files
SET dur_xxx=folder_dur_files
SET glo_xxx=folder_glo_files

SET dif_xxxc=folder_dif_c_files
SET dir_xxxc=folder_dir_c_files
SET dur_xxxc=folder_dur_c_files
SET glo_xxxc=folder_glo_c_files

REM To add more, or lees, or edit prefixes, edit this line
SET prefixes=dif dir dur glo

REM Create folders if they do not already exist
FOR %%a IN (%prefixes%) DO (
   IF NOT EXIST "!%%a_xxx!\" MD "!%%a_xxx!"
   IF NOT EXIST "!%%a_xxxc!\" MD "!%%a_xxxc!"
)

REM Search for folders by their prefix
FOR %%a IN (%prefixes%) DO (
   REM Search for folders by their name pattern and move them
   FOR /F %%b IN ('DIR /AD /B %%a_???c_??') DO MOVE %%b "!%%a_xxxc!"
   FOR /F %%b IN ('DIR /AD /B %%a_???') DO MOVE %%b "!%%a_xxx!"
)

Commented:
If your destination folders also have a common theme then I could also comdense the SET part of the code too so that you don't have to re-type tha same path and foldernames 8 times.

Author

Commented:
They do have a common theme, but this is as far as a I need to have the script taken.  Thanks again. The commented code is great.

Cheers,
JE

Author

Commented:
Thanks.

Commented:
Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial