# BAT File, Create Folder, Move Files



## poleary2000 (Mar 24, 2004)

I had some incredible help here last year in this thread. I've been using it without fail for over a year. 
http://forums.techguy.org/dos-other/916240-batch-move-files-delete-folder.html

It did exactly what I needed. However, now I also have a need to reverse the process for a different project. It really is just about the same thing in the older thread, just in reverse. It really comes in handy for me to be able to do these two things. I've messed around with trying to do it myself, but I can't seem to get it.

I want to go from this:

Folder A

Sub Folder A​
File A​
File B​
File C​
SubFolder B​
File A​
File B​
File C​
To this:

Folder A

Sub Folder A​
Home​
File A​
File B​
File C​
SubFolder B​
Home​
File A​
File B​
File C​


----------



## poleary2000 (Mar 24, 2004)

I created this, but it only makes the New Folder but doesn't move the files. Thoughts?

@Echo Off
Set _Source=D:\My Documents\Desktop\Test
Set _NewFolder=Home
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PushD "%_Source%"
For /F "Tokens=* Delims=" %%A In ('Dir /AD /B') Do (

mkdir "%_Source%\%%A\%_NewFolder%"	
For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Source%\%%A"') Do Move "%_Source%\%%A\%%I" "%_Source%\%%A\%_NewFolder%"
Move /-Y "\%_Source%\%%A\*.*" "%_Source%\%%A\%_NewFolder%"

)
PopD


----------



## Squashman (Apr 4, 2003)

Wow, you sure don't get around here to help to often. Any reason why?

I can probably reverse engineer the batch file. TheOutcaste pretty much taught me everything I know about batch files.

Just looking at TheOutcaste's batch file can you answer a few questions for me?
Is Folder A the source directory in his batch file? That is the way it looks to me.
Are there any folders underneath the Sub Folders or is it only files in the Sub Folders?


----------



## Squashman (Apr 4, 2003)

Well this technically worked but I got an error message but I am too tired to figure it out.
Batch

```
@Echo Off
Set _Source=C:\Users\Squashman\batch\Source A
Set _NewFolder=Home
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PushD "%_Source%"
For /F "Tokens=* Delims=" %%A In ('Dir /AD /B') Do (
	pushd "%%~A"
	mkdir "%_NewFolder%"
	Move *.* "%_NewFolder%"
	popd
)
popd
```
Output

```
C:\Users\Squashman\batch>dir /a-d /b /s "Source A"
C:\Users\Squashman\batch\Source A\Sub 1\File A.txt
C:\Users\Squashman\batch\Source A\Sub 1\File B.txt
C:\Users\Squashman\batch\Source A\Sub 1\File C.txt
C:\Users\Squashman\batch\Source A\Sub 2\File A.txt
C:\Users\Squashman\batch\Source A\Sub 2\File B.txt
C:\Users\Squashman\batch\Source A\Sub 2\File C.txt

C:\Users\Squashman\batch>Move2Home.bat
The filename, directory name, or volume label syntax is incorrect.
C:\Users\Squashman\batch\Source A\Sub 1\File A.txt
C:\Users\Squashman\batch\Source A\Sub 1\File B.txt
C:\Users\Squashman\batch\Source A\Sub 1\File C.txt
        3 dir(s) moved.
C:\Users\Squashman\batch\Source A\Sub 2\File A.txt
C:\Users\Squashman\batch\Source A\Sub 2\File B.txt
C:\Users\Squashman\batch\Source A\Sub 2\File C.txt
        3 dir(s) moved.

C:\Users\Squashman\batch>dir /a-d /b /s "Source A"
C:\Users\Squashman\batch\Source A\Sub 1\Home\File A.txt
C:\Users\Squashman\batch\Source A\Sub 1\Home\File B.txt
C:\Users\Squashman\batch\Source A\Sub 1\Home\File C.txt
C:\Users\Squashman\batch\Source A\Sub 2\Home\File A.txt
C:\Users\Squashman\batch\Source A\Sub 2\Home\File B.txt
C:\Users\Squashman\batch\Source A\Sub 2\Home\File C.txt

C:\Users\Squashman\batch>
```


----------



## poleary2000 (Mar 24, 2004)

True. I'm not a "tech support guy" by any means. Last year, I did a search on google about moving files around. One of the links lead me to a batch file discussion. So, I starting searching about batch files and ended up here on the forum where I posted the question. I had no idea what a batch file even was (as you can see in the link for the old post, it took a while just for people to even understand what I wanted). I posted here, got some wonderful help and went along my way. There really isn't much I'd be able to contribute around here. Thanks for the attempt. I'll keep playing around with it to see if anything works.


----------



## poleary2000 (Mar 24, 2004)

I ran a test and that seemed like it worked! I'll try a few more times before putting it on my data.


----------



## poleary2000 (Mar 24, 2004)

Ah, one thing though. In the Subfolders, there may be a folder that needs to be moved also. That folder also needs to be moved. It is named the same thing everytime. For example, call it SITE_A. I would like to move all of the files and also the folder SITE_Ato the new HOME folder. The SITE_A folder may not always exist.


----------

