1

I'd like to be able to move Folders (and any subfolders and files) from one location to another; however, I only want one specific sub folder moved from each folder in the starting location.

Here's an example of my existing folder structure:

E:\Estimates\Estimating Files\E27001
E:\Estimates\Estimating Files\E27001\27001A - Customer1\Drawings
E:\Estimates\Estimating Files\E27001\27001A - Customer1\Costings
E:\Estimates\Estimating Files\E27001\27001B - Customer2\Drawings
E:\Estimates\Estimating Files\E27001\27001B - Customer2\Costings
E:\Estimates\Estimating Files\E27001\27001C - Customer3\Drawings
E:\Estimates\Estimating Files\E27001\27001C - Customer3\Costings
E:\Estimates\Estimating Files\E27002
E:\Estimates\Estimating Files\E27002\27002A - Customer1\Drawings
E:\Estimates\Estimating Files\E27002\27002A - Customer1\Costings

I have lots of estimates, as you can see some with the same number but a different suffix letter, and the Customer name obviously changes.

I want to move only the Drawings folders to a new drive and folder structure, like this:

S:\E27xxx\0xx\01\A - Customer1\
S:\E27xxx\0xx\01\B - Customer2\
S:\E27xxx\0xx\01\C - Customer3\
S:\E27xxx\0xx\02\A - Customer1\

The Drawings folders will have subfolders and files that I want moved across to the new location as well.

So far, I've only managed to create the empty folders up to this level:

S:\E27xxx\0xx\01\A - Customer1\
S:\E27xxx\0xx\01\B - Customer2\
S:\E27xxx\0xx\01\C - Customer3\
S:\E27xxx\0xx\02\A - Customer1\

using a small batch file, for all estimates from 27000 to 30000.

But moving the files and folders is beyond my capabilities, hopefully someone will be able to help me out Or know of an existing utility that might work!

Thanks!

0

3 Answers 3

0

I want to move only the Drawings folders to a new drive and folder structure, like this:

This batch file could help you. It finds all the "Drawings" subfolders, it tokenizes the parts of the directory paths to construct the new directory struct, then copies each found directory with subdirectories using RoboCopy tool.

VERY IMPORTANT:

  • Ensure to place the batch file inside "...\Estimating Files" folder and run it from there, otherwise, manually set the sourceDir variable.

  • Before use this script, you should manually adjust the token count of the For loop.

    For example, I used this source folder C:\Source\Estimates\Estimating Files, we need to miss the first four tokens because we want to start counting after the Estimating Files part, thats why I set Tokens=4,5,6 in the script below, if you have a different path then you should adjust the tokens.

    If your source dir is E:\Estimates\Estimating Files then you should set the tokens parameter like this: Tokens=3,4,5.

  • If want to automatically delete the copied source structure, just append a /Move in the RoboCopy parameters.


Source-code

@Echo OFF

Set "sourceDir=%CD%"
Set "targetDir=C:\Target"

Set "findPattern=Drawings"

For /F "Tokens=4,5,6 Delims=\" %%a In (
    'Dir /B /S /A:D "%sourceDir%\*%findPattern%"'
) Do (
    Call Set "Token1=%%~a"
    Call Set "Token2=%%~b"
    Call Set "Token3=%%~c"
    Call Set "sourcePath=%CD%\%%~a\%%~b\%%~c"
    Call Set "targetPath=%targetDir%\%%Token1%%\%%Token1:~3%%\%%Token2:~3,2%%\%%Token2:~5%%\%%Token3%%"

    Echo+
    Call Echo Source: "%%sourcePath%%"
    Call Echo Target: "%%targetPath%%"

    (Call RoboCopy.exe "%%sourcePath%%" "%%targetPath%%" /E /ZB /COPYALL)1>Nul

)

Pause&Exit /B 0

Output

Source: "C:\Source\Estimates\Estimating Files\E27001\27001A - Customer1\Drawings"

Target: "C:\Target\E27001\001\01\A - Customer1\Drawings"

Source: "C:\Source\Estimates\Estimating Files\E27001\27001B - Customer2\Drawings"

Target: "C:\Target\E27001\001\01\B - Customer2\Drawings"

Source: "C:\Source\Estimates\Estimating Files\E27001\27001C - Customer3\Drawings"

Target: "C:\Target\E27001\001\01\C - Customer3\Drawings"

Source: "C:\Source\Estimates\Estimating Files\E27002\27002A - Customer1\Drawings"

Target: "C:\Target\E27002\002\02\A - Customer1\Drawings"

6
  • Hi thanks for your suggestions. Source: "X:\Estimating Dept\Estimating Files\Estimates\E27000 - E28999\Estimating Files\Estimates\E27000 - E28999" Target: "S:\E27xxx\Estimating Files\imating Files\im\ates\E27000 - E28999" The Target looks wrong; there's \ inbetwwen the words, also no folders or files were moved. Oct 12, 2015 at 14:10
  • Please read carefuly the "Very Important" section I wrote.the source folder you mention is totally different from the structure example that you gave in your question. try setting "tokens=6,7,8" or "tokens=7,8,9" in the batch file. Oct 12, 2015 at 14:16
  • Could you explain what the tokens are referring to? I'm just about getting the hang of RoboCopy (such a versatile function!) Also, I want the subfolders to literally be ...\26xxx\9xx\... ...\27xxx\0xx\... ...\27xxx\1xx\... This is so the Estimates are grouped into thousands, hundreds, then each hundreds folder will have 100 folders 00-99. Oct 12, 2015 at 16:26
  • The tokens reffers to tokenization or delmiitation of a string. I used the character "\" to delimitate the parts of the directory path. In other words, to split the directory path to put each foldername in a different variable to work with them. Imagine you have this path "C:\Folder1\Subfolder1" and the For loop has set "Tokens=1,2,3" then the path it will be splitted in this way "C:", "Folder1", and "Subfolder1", we name that a "token", three tokens in that example so they will be stored in these For vars: %%a, %%b, and %%c. Oct 12, 2015 at 16:31
  • I'm starting to get the hang of this now. Thanks for your help so far. It now creates my folders the way I want, but now it's copying all the subfolders, not just the one with the FindPattern. Call Set "sourcePath=%CD%\%%~a\%%~b\%%~c" Call Set "targetPath=%targetDir%\%%Token1:~3,1%%xx\%%Token2:~4,2%%\%%Token3:~6%%" ... (Call RoboCopy.exe "%%sourcePath%%" "%%targetPath%%" /E /ZB /COPYALL)1>Nul Any suggestions? Oct 13, 2015 at 15:48
1

Try this script. You might need to edit the drive letters though

setlocal EnableDelayedExpansion
@echo off
Q:
cd "Estimating\Estimating Files"
FOR /D /R %%G IN ("*Drawings*") DO (
FOR /F "tokens=4,5 delims=\" %%H IN ("%%G") DO (
set temp=%%H
set num=!temp:~4,2!
set temp=%%I
set alpha=!temp:~5!
MKDIR "Q:\E27XXX\0XX\!num!\!alpha!\Drawings"
CALL :mover "%%G" !num! !alpha!
)
)

:mover
FOR /R %1 %%X IN (*) DO (
COPY "%%X" "Q:\E27XXX\0XX\%2\%3 %4 %5\Drawings"
)
3
  • Please upvote & select my answer if you feel it helped :) Thanks Oct 12, 2015 at 10:46
  • When I try this I get the following error: E:\Estimating Dept\Estimating Files\Estimates\E27000 - E28999>setlocal EnableDelayedExpansion The system cannot find the path specified. Oct 12, 2015 at 14:08
  • I'm hoping you modified the script to account for the changes for the new parent folder "Estimating Dept" which my script doesn't account for. You might need to include this in the paths where needed :) Oct 12, 2015 at 17:03
0

Here is the final code I used to copy the folders to the new directory. Thanks to @ElektroStudios for their help.

@Echo OFF

Set "sourceDir=%CD%"
Set "targetDir=S:\E30xxx"

Set "findPattern=2 - Drawings"

For /F "Tokens=6,7,8,9 Delims=\" %%a In (
    'Dir /B /S /A:D "%sourceDir%\*%findPattern%"'
) Do (
    Call Set "Token1=%%~a"
    Call Set "Token2=%%~b"
    Call Set "Token3=%%~c"
    Call Set "Token4=%%~d"
    Call Set "sourcePath=%CD%\%%~a\%%~b\%%~c\%%~d"
    Call Set "targetPath=%targetDir%\%%Token1:~3,1%%xx\%%Token2:~4,2%%\%%Token3:~6%%"

    Echo+
    Call Echo Source: "%%sourcePath%%"
    Call Echo Target: "%%targetPath%%"

    (Call RoboCopy.exe "%%sourcePath%%" "%%targetPath%%" /E /ZB /COPYALL)1>Nul

)

Pause&Exit /B 0

Just a case of getting to grips with RoboCoby and tokenisation of the delimited string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .