Link to home
Start Free TrialLog in
Avatar of justearth
justearthFlag for United States of America

asked on

Move groups of folders to new folders with script

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
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

@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\"
   )
)
Avatar of justearth

ASKER

t0t0:
Where do I add the input directory?

Thanks,
JE
okay, back i a mo
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
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.
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.
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%"
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
No probs... back in a mo
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!"
   )
)
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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

Thanks.
Thank you.