# Batch file to find and move directories



## Triple6 (Dec 26, 2002)

Hi,

I need help creating a script to run on a Windows server that finds a folder of a specific name inside of a sub folder and moves only that folder and all files inside it out of that folder to the first sub folder of the folder its in and renames it to a specific name.

So as an example I have folders:

\0001\Folder1\Folder2\
\Folder3\

\0002\Folder1\Folder2\
\Folder3\

and so on and so on for thousands of numbered folders.

Folder2 always has the same, pretty sure Folder1 is always the same name, the numbered folders have various numbers. The new folder name will always be Folder4.

End result:

\0001\Folder1\Folder2\
\Folder4\

\0002\Folder1\Folder2\
\Folder4\


----------



## foxidrive (Oct 20, 2012)

If I can paraphrase your question - you want to find a folder called apple and move it to another location - then rename it to tomato.

You appear to have a folder tree with 4 levels like this:


```
\0001\Folder1\Folder2\Folder3\
 \0002\Folder1b\Folder2b\Folder3b\
```
and I gather that you want to move folder3 and folder3b to the root directory, or maybe the main folder which holds 0001 and 0003

Can you clarify the task, and tell me if I understand you so far?


----------



## Triple6 (Dec 26, 2002)

foxidrive said:


> If I can paraphrase your question - you want to find a folder called apple and move it to another location - then rename it to tomato.


Correct.



foxidrive said:


> You appear to have a folder tree with 4 levels like this:
> 
> 
> ```
> ...


Yes, although some directories are deeper than 4 levels, I'm now pretty sure that the folders I need to find and move are generally at the third level along with other folders.



foxidrive said:


> and I gather that you want to move folder3 and folder3b to the root directory, or maybe the main folder which holds 0001 and 0003


I want to move them to the main folder that holds 0001 and 0003, etc.


----------



## foxidrive (Oct 20, 2012)

This will move the folder called apple (with all files and folders inside) into the main\folder and call it tomato.

It will fail when there are dozens of folders called apple as there can only be one tomato folder without mixing up many folders.

You'll need to provide further details about the renaming if you need more help with it.


```
@echo off
cd /d "d:\main\folder"
for /f "delims=" %%a in ('dir "apple" /b /s /ad ') do (
    echo %%a
    move "%%a" ".\tomato"
)
```


----------



## Triple6 (Dec 26, 2002)

If I only wanted to move the first/highest apple directory and leave any other apple directories that are deeper in the tree would I just remove the _/s_? Or is there a way I can move those folders with their existing names into the new tomato directory?


----------



## foxidrive (Oct 20, 2012)

No as /s is needed to traverse the tree.

If you want help to perform a task then you need to specify the task well, because a batch script is prone to large changes with even small changes in the task.


----------



## Triple6 (Dec 26, 2002)

So is there a way to only move the specified folder from one level of the directory tree and ignore any folders at a deeper level?


----------



## foxidrive (Oct 20, 2012)

There are some further questions here, as the task is still indistinct.

In which level of the tree will the folder be, starting at the root? 
Will the folder exist in only one single place, in that level of the tree?


----------



## Triple6 (Dec 26, 2002)

It will generally be at the 3rd level. But there may be additional folders at higher levels. I wouldn't care if the higher level ones were not moved as they are few of those and I can move them separately.


----------



## foxidrive (Oct 20, 2012)

Give this a shot from the base level folder. It'll just echo the move command to the console when it finds folder(s) called *apple* on the third level folder from the root.


```
@echo off
for /f "delims=" %%a in ('dir "apple" /ad /b /s ^| findstr \\.*\\.*\\ ^| findstr /v \\.*\\.*\\.*\\ ') do echo move "%%a" "c:\destination\folder"
pause
```


----------



## Triple6 (Dec 26, 2002)

Thanks, I'll give that a try when I have access to the test machine again.


----------



## Triple6 (Dec 26, 2002)

The first code you gave me works aside from trying to put all the found folders into the same folder. The second one I can't seem to move the folders I need, but that may be my lack of a clear picture of how it's laid out and my lack of understanding the code.

I've included two pictures of the structure I'm working with. The first is the current layout and the second is how I want it to end up. The folders I want to move are called Six02 in this example and I want them to end up at the same level as Five01 and Five02.


----------



## foxidrive (Oct 20, 2012)

*Edited: V2.0*

That's very different to how it has been described. 

Run this in the main folder - it will create temp.bat.txt for you to examine and edit in notepad.
Then you can rename it to temp.bat and run it when it is doing what you need.

This assumes that it is a once off management task.


```
@echo off
del temp.bat.txt 2>nul
for /f "delims=" %%a in ('dir "six02" /b /s /ad ') do (
   >>temp.bat.txt echo pushd "%%~dpa" ^& move "%%a" .. ^& popd
)
```


----------



## Triple6 (Dec 26, 2002)

Just did a quick test on my local machine and that works. Is there a way to add the folder rename to that or should I do that seperately?

And I really appreciate the help.


----------



## Squashman (Apr 4, 2003)

Remove the .txt from the filename and then call the rename batch file as the last line.


----------



## Triple6 (Dec 26, 2002)

Squashman said:


> Remove the .txt from the filename and then call the rename batch file as the last line.


Yes, I got the renaming of the file from .bat.txt to .bat to run it, that part is fine. It's renaming the moved folder that I was inquiring about.


----------



## foxidrive (Oct 20, 2012)

You can add the new name as shown below in the move command:


```
@echo off
del temp.bat.txt 2>nul
for /f "delims=" %%a in ('dir "six02" /b /s /ad ') do (
   >>temp.bat.txt echo pushd "%%~dpa" ^& move "%%a" "..\newname" ^& popd
)
```
This should limit the move to the sixth level folders only, counted from the root - but I didn't test it.


```
@echo off
del temp.bat.txt 2>nul
for /f "delims=" %%a in ('dir "six02" /b /s /ad ^| findstr \\.*\\.*\\.*\\.*\\.*\\.*\\  ^| findstr /v \\.*\\.*\\.*\\.*\\.*\\.*\\.*\\ ') do (
   >>temp.bat.txt echo pushd "%%~dpa" ^& move "%%a" "..\newname" ^& popd
)
```


----------



## Triple6 (Dec 26, 2002)

Oh thanks, didn't realize the rename was that simple. I can test the latest version in a few days but the code in post #13 will work pretty well for me if it doesn't.

I'm gonna try to take your examples and try to learn some of this stuff better, I get half of it but the other half not so much and I know there's a lot more that I simply haven't seen or used before.

I greatly appreciate the help.


----------

