Solved

Help creating a .bat file to move thousands of files out of folders/sub folders into a different folder

Posted on 2010-08-16
9
586 Views
Last Modified: 2012-05-10
Hi,

  I need some help in creating a .bat file to move thousands of files out of folders/sub folders into a different folder.

Here is my situation -  I have a folder structure similar to:  d:\test\folder_a\folder_b\ filled with thousands of files with the extension '.0'.  There won't be any additional file types other than those.  I want all of those files moved out of the folder/sub folders into d:\images.  I also want all of the sub directories to be deleted but to leave the root dir.  

I have seen a few .bat examples, but nothing I could find suited my needs.  I have included a few examples what I think might work, but needs some tweaking to be able to move all of the files from one folder to another instead of just back to the 'root' of the original directory.

Thanks!

Any help would be greatly appreciated!
@echo off
setlocal
 
set root=f:
set mask=*.*
 
for /F "tokens=*" %%G in ('dir "%root%\%mask%" /A:-D /B /S') do ECHO move "%%G" "%root%"
pause

________________________________________________________________________________________


@echo off
setlocal
 
set root=D:\GHOST
set mask=*.mp3
 
for /F "tokens=*" %%G in ('dir "%root%\%mask%" /A:-D /B /S') do ( 
ECHO move "%%G" "%root%"
move "%%G" "%root%" 
)
pause

________________________________________________________________________________________

@echo
setlocal enabledelayedexpansion
 
set root=d:\test
 
for /F "tokens=* usebackq" %%G in (`dir "%root%" /A:D /B`) do (
 for /F "tokens=* usebackq" %%H in (`dir "%root%\%%G" /A:-D /B /S`) do (
  echo N|move /-Y "%%H" "%root%"
 )
)
 
for /F "tokens=* usebackq" %%G in (`dir "%root%" /A:D /B`) do (
 set contents=
 for /F "tokens=* usebackq" %%H in (`dir "%root%\%%G" /A:-D /B /S 2^>NUL`) do (
  set contents=%%H
 )
 if not defined contents rd /q /s "%root%\%%G"

Open in new window

0
Comment
Question by:doc_jay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33448802
How about this?

@echo off
set BaseDir=d:\test
set DestDir=d:\images
for /F "tokens=*" %%A in ('dir /ad /s /b "%BaseDir%"^|sort /r') do (
  move "%%A\*.*" "%DestDir%"
  rd "%%A"
)

~bp
0
 
LVL 3

Expert Comment

by:adallen
ID: 33448878
Why don't you just:

move d:\test\folder_a\folder_b\ d:\images\
0
 
LVL 12
ID: 33448959
I think you only want the files, and not the directories, correct?

FOR /F %%F IN ('DIR A:D "C:\Folder_A" /B') DO Robocopy "C:\Folder_A\%%F" "D:\Images" *.0 /ZB /MOVE & Deltree "C:\Folder_A\%%F"

done and done.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:doc_jay
ID: 33448965
very sorry, I guess I didn't make my situation exactly clear.  I have hundreds of folders with sub folders and they all don't have the same path of d:\test\folder_a\folder_b.  Each folder in the d:\test\ folder is actually a date, for example '20070621' for June 21 2007.  I just need the .bat file to look recursively through each folder inside d:\test.
0
 

Author Comment

by:doc_jay
ID: 33449019
Billprew --

   thanks for  your quick respone and the script worked, except at the end of the operation it came back with:  'the filename, directory name, or volume label syntax is incorrect.'   Anything to be concerned about it or just ignore it?

Thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33449106
Do any of the folder or filenames have funny characters in them, like perhaps the ampersand?

~bp
0
 

Author Comment

by:doc_jay
ID: 33449168
nope, none of them do.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33449241
Odd, that's typically what I have experienced with that message, but there may be other things that can cause it.

Did you run a real test, and did it move all files involved as desired, and remove all folders?  

Make sure you check before and after file counts, and that all folders were removed (which will only happen if they were empty).

You could also try running it with the ECHO OFF changed to ECHO ON to see where the error is caused.

~bp
0
 

Author Closing Comment

by:doc_jay
ID: 33449495
Worked as expected - thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 72
find and replace 5 57
Batch Script for opening the DOS prompt and run a command 9 128
For loop to add numbers 3 103
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question