Batch file to Create folder structure and move file types

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Batch file to Create folder structure and move file types

#1 Post by Andrius » 26 Jun 2012 15:26

I would like to create a batch file that does the following

(Place the batch file in the root directory of lets say 10 folders )
(Folder structure example : c:\Root\Folder1\ -- c:\Root\Folder10)
Inside each folder is an undetermined amount of random files with file types such as .psd .png .obj . ma .mb

User now executes the bat file

I would like the bat file to read the directory that it's currently in and realize it has 10 (example amount) folders to deal with.
I would now like the bat file to drill into these folders one at a time. and create a sub structure

Folder1\
Folder1\animation
Folder1\export
Folder1\model
Folder1\source
Folder1\texture

I want to keep the root folder name but create those folders within that folder.

I would then like the bat file to sort the contents of each folder (Folder 1 for example) and move the file types to the appropriate directories (eg: .psd to be moved to \texture .ma & .mb & .obj to be moved to \model)

Once the bat file has sorted the files that were in Folder 1 and placed them into the proper subdirectories then move on to the next folder in the list (eg Folder2)

Keep this in a loop until all (10) have been sorted and cleaned up. Then close bat file leaving the user with 10 nice new subdirectory structures for those 10 folders he originally started with.

Does this make sense?

I am dealing with a LOT of incoming files and am getting really sick of moving and sorting by hand. A bat file would be perfect to help me out here I think. I am pretty much a noob when it comes to this stuff but I do know basics if that helps.

MANY MANY Thanks in advance for your assistance with this.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file to Create folder structure and move file type

#2 Post by Fawers » 26 Jun 2012 15:47

Are .psd .png .obj . ma .mb all the extensions? In which folder each one of them must go, exactly?

Your request does make sense, but full information is required.

1) File Extensions (all of them)
2) Folder - File (which file extensions go to which folder)
3) Extra Info (you said, "Inside each folder is an undetermined amount of random files with file types such as .psd .png .obj . ma .mb" ~~> it means they are not inside other subfolders, right? Just to have it clear)
4) Chance of changing (is this going to be a default? does it have any chance to change?

Once all information is given, working on a batch file will be easier.

By the way, welcome to the forum.

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: Batch file to Create folder structure and move file type

#3 Post by Andrius » 26 Jun 2012 16:04

Fawers wrote:Are .psd .png .obj . ma .mb all the extensions? In which folder each one of them must go, exactly?

Your request does make sense, but full information is required.

1) File Extensions (all of them)
2) Folder - File (which file extensions go to which folder)
3) Extra Info (you said, "Inside each folder is an undetermined amount of random files with file types such as .psd .png .obj . ma .mb" ~~> it means they are not inside other subfolders, right? Just to have it clear)
4) Chance of changing (is this going to be a default? does it have any chance to change?

Once all information is given, working on a batch file will be easier.

By the way, welcome to the forum.


Wow thank you so much for the quick reply! I really appreciate the help!

1) File Extensions
("animation" Folder will contain .mma filetypes)
("export" Folder will contain .mmf filetypes)
("model" Folder will contain .max .obj .ma .mb file types)
("source" Folder will be omitted at this time due to lack of information from my team.. More for future work)
("texture" Folder will contain .png .psd .bmp .jpg file types)

3) Extra Info
Yes it means that all files that need to be sorted and placed will be within the root of "folder1" through "folder10" for example. c:\Root\Folder1\randomfiles (no sub folders in this directory until we create them with the bat process.

4) Chance of Changing
Slim. It would be nice to have it somewhat flexible however that may get super complicated and become a full out app. So in the interest of keeping things simple and efficient.

Again.. thank you so much!

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file to Create folder structure and move file type

#4 Post by Fawers » 26 Jun 2012 16:22

I see. Writing a batch file from this info is possible.

Code: Select all

@echo off
::
:MainCode
for /f "delims=" %%a in ('dir /b /ad') do (
  echo Processing folder: %%a
  ECHO pushd "%%a"
  call :MakeDirs
  call :MoveFiles
  ECHO popd
  echo,
)
pause
exit /b

:MakeDirs
for %%f in (animation export model source texture) do (
  ECHO if not exist "%%f" md "%%f"
)
exit /b

:MoveFiles
::Animation - File types: .mma
for %%t in (mma) do ECHO move *.%%t "Animation"

::Export - File types: .mmf
for %%t in (mmf) do ECHO move *.%%t "Export"

::Model - File types: .obj .ma .mb .max
for %%t in (obj ma mb max) do ECHO move *.%%t "Model"

::Source - File types: (not available yet)
::when file types are available, remove REM and fill in the parenthesis from line below
REM for %%t in () do ECHO move *.%%t "Source"

::Texture - File types: .psd .png .bmp .jpg
for %%t in (psd png bmp jpg) do ECHO move *.%%t "Texture"
exit /b

This code will not process the commands; instead, it will ECHO (or PRINT, in case you are acquainted to other languages) to show what the output will be. If the output is the desired one, you can 1) remove the UPPERCASE ECHOes to make it actually process the commands.

or 2) copy-paste:

Code: Select all

@echo off
::
:MainCode
for /f "delims=" %%a in ('dir /b /ad') do (
  echo Processing folder: %%a
  pushd "%%a"
  call :MakeDirs
  call :MoveFiles
  popd
  echo,
)
::Remove the PAUSE on the line below if everything is ok.
PAUSE
exit /b

:MakeDirs
for %%f in (animation export model source texture) do (
  if not exist "%%f" md "%%f"
)
exit /b

:MoveFiles
::Animation - File types: .mma
for %%t in (mma) do move *.%%t "Animation"

::Export - File types: .mmf
for %%t in (mmf) do move *.%%t "Export"

::Model - File types: .obj .ma .mb .max
for %%t in (obj ma mb max) do move *.%%t "Model"

::Source - File types: (not available yet)
::when file types are available, remove REM and fill in the parenthesis from line below
REM for %%t in () do move *.%%t "Source"

::Texture - File types: .psd .png .bmp .jpg
for %%t in (psd png bmp jpg) do move *.%%t "Texture"
exit /b

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: Batch file to Create folder structure and move file type

#5 Post by Andrius » 26 Jun 2012 16:34

IT WORKS SOOOOOO WELL!!!!!!!!! There is only a minor issue and it's throwing the following error. It seems that the process itself succeeds overall except I get this output in the window. Is this simply because it's checking for filetypes that don't exist?

Processing folder: 43333_item2
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
C:\Users\_____\Desktop\tetetete\43333_item2\fdsfsdfds.obj
1 file(s) moved.
A duplicate file name exists, or the file
cannot be found.
C:\Users\_____\Desktop\tetetete\43333_item2\fdsfdsf.mb
1 file(s) moved.
A duplicate file name exists, or the file
cannot be found.
C:\Users\_____\Desktop\tetetete\43333_item2\fsdfdsfs.psd
1 file(s) moved.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.

Processing folder: 43434_item
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
C:\Users\_____\Desktop\tetetete\43434_item\body_4661.obj
1 file(s) moved.
A duplicate file name exists, or the file
cannot be found.
C:\Users\_____\Desktop\tetetete\43434_item\body_4661.mb
1 file(s) moved.
A duplicate file name exists, or the file
cannot be found.
C:\Users\_____\Desktop\tetetete\43434_item\body_4661.psd
1 file(s) moved.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.

Press any key to continue . . .

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file to Create folder structure and move file type

#6 Post by Fawers » 26 Jun 2012 17:11

Oh, silly me! It returns this error probably because there aren't any files of certain extensions.

In "MoveFiles", replace current code with this:

Code: Select all

::Animation - File types: .mma
for %%t in (mma) do if exist *.%%t move *.%%t "Animation"

::Export - File types: .mmf
for %%t in (mmf) do if exist *.%%t move *.%%t "Export"

::Model - File types: .obj .ma .mb .max
for %%t in (obj ma mb max) do if exist *.%%t move *.%%t "Model"

::Source - File types: (not available yet)
::when file types are available, remove REM and fill in the parenthesis on line below
REM for %%t in () do if exist *.%%t move *.%%t "Source"

::Texture - File types: .psd .png .bmp .jpg
for %%t in (psd png bmp jpg) do if exist *.%%t move *.%%t "Texture"

If the error persists, then files with the same names exist in the subfolders (although I don't think it is the case here). To correct it, add the /y¹ switch to the MOVE commands.

1. /y assumes you do want to overwrite any existing file with the same name.

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: Batch file to Create folder structure and move file type

#7 Post by Andrius » 27 Jun 2012 09:21

Totally fixed! Thanks! This is going to save me a TON of man hours :)

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: Batch file to Create folder structure and move file type

#8 Post by Andrius » 28 Jun 2012 11:32

I would like to make a simple modification to this program so please let me know if I am going about this correctly.

I would like to create 2 folders within the texture folder (png and psd) and then sort psd intot he PSD folder and sort PNG intot he PNG folder so would I change the code as follows?

Code: Select all

@echo off
::
:MainCode
for /f "delims=" %%a in ('dir /b /ad') do (
  echo Processing folder: %%a
  pushd "%%a"
  call :MakeDirs
  call :MoveFiles
  popd
  echo,
)
::Remove the PAUSE on the line below if everything is ok.
PAUSE
exit /b

:MakeDirs
for %%f in (animation export model source texture texture\psd texture\png) do (
  if not exist "%%f" md "%%f"
)
exit /b

:MoveFiles
::Animation - File types: .mma
for %%t in (mma) do if exist *.%%t move *.%%t "Animation"

::Export - File types: .mmf
for %%t in (mmf) do if exist *.%%t move *.%%t "Export"

::Model - File types: .obj .ma .mb .max
for %%t in (obj ma mb max) do if exist *.%%t move *.%%t "Model"

::Source - File types: (not available yet)
::when file types are available, remove REM and fill in the parenthesis on line below
REM for %%t in () do if exist *.%%t move *.%%t "Source"

::Texture - File types: .bmp .jpg
for %%t in (bmp jpg) do if exist *.%%t move *.%%t "Texture"

::Texture PSD - File types: .psd
for %%t in (psd) do if exist *.%%t move *.%%t "Texture\psd"

::Texture - File types: .png
for %%t in (png) do if exist *.%%t move *.%%t "Texture\png"
exit /b

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file to Create folder structure and move file type

#9 Post by Fawers » 28 Jun 2012 11:57

My brain is able to process it normally. :D
Hint: you can change the 2 last "texture" parts into 1. Try this:

Code: Select all

::Texture PSD & PNG
for %%t in (psd png) do if exist *.%%t move *.%%t "Texture\%%t"

instead of

Code: Select all

::Texture PSD - File types: .psd
for %%t in (psd) do if exist *.%%t move *.%%t "Texture\psd"

::Texture - File types: .png
for %%t in (png) do if exist *.%%t move *.%%t "Texture\png"


And you might also want to change ":MakeDirs" to the following:

Code: Select all

for %%f in (animation export model source texture "texture\psd" "texture\png") do (
  if not exist "%%~f" md "%%~f"
)

^ I don't know how backslashes \ are processed, but if the code shows erros in the creation of folders, then you might want to consider the backslashes as the causes for them. Enclosing them in quotes will avoid any errors, and the tilde (~) in the FOR variable removes any quotes on the string (so we won't have strings like ""texture\psd"" and ""texture\png"").

Andrius
Posts: 37
Joined: 26 Jun 2012 15:15

Re: Batch file to Create folder structure and move file type

#10 Post by Andrius » 28 Jun 2012 12:00

You are the batch file god to my problems. Thank you endlessly :)

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Batch file to Create folder structure and move file type

#11 Post by Fawers » 28 Jun 2012 12:04

Don't hesitate to come back whenever you want, mate. :wink:

And there are many higher 'gods' than me on this forum. :p

Post Reply