?
Solved

Need script to batch move files from folders

Posted on 2011-04-28
13
Medium Priority
?
494 Views
Last Modified: 2012-08-13
I recently ran a file recovery program from a damaged disk, however it restored all my files into 368 seperate folders, with no real logistics behind it.  I need a script or batch file that will move files by extension into seperate folders.

All files are located in folders called G:\Recover\recup_dir.1 through G:\Recover\recup_dir.368

Within each of the recup_dir.1 , .2 , .3 etc... folders are various different file types ranging from exe to jpg, etc.

I would like the script/batch file to run through each of these recup_dir.### folders and MOVE files by extension into a new folder created under G:\Recover\JPG and G:\Recover\DOC etc.

If also possible, I would like to specify it move files with *.jpg extension only larger than 1MB into the G:\Recover\JPG folder.

Thank you in advance. Be advised I have limited knowledge in creating/modifying scripts, so I basically need the code and just so I can execute it.
0
Comment
Question by:fireguy1125
13 Comments
 
LVL 5

Expert Comment

by:jason987
ID: 35487990
Is there a specific reason you want this in script format?  Windows explorer search can easily do this for you, to include file size exclusion.  When you get the results just drag them into the folder you want.
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 35488171
Wow, it slipped my mind.  Well in any case I tried on the mp3s, which were only 30 or so of them.  But when I try it on the DOC files, I run the search results, ends up with about 1,500 of them, then i select all, hit cut, then when i go to paste in the DOC folder, an Error Moving File or Folder window comes up with: Cannot move file: cannot read from the source file or disk
0
 
LVL 4

Expert Comment

by:MaximumIQ
ID: 35488175
try this code, I just wrote it in Notepad so hopefully I didn't have any typos.

I recommend you backup those files somewhere before you run the script
 
On Error Resume Next
Dim SourceFolder, DestinationFolder, FileExt As String
SourceFolder = "G:\Recover\recup_dir."
DestinationFolder = "G:\Recover\"

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
For i = 1 To 368
    For Each fil In fso.getfolder(SourceFolder & i).Files
        FileExt = Right(fil, InStrRev(fil, "."))
        If fso.folderexists(DestinationFolder & fil) = False Then
            fso.createfolder (DestinationFolder & fil)
        End If
        fso.movefile fil, DestinationFolder & fil & "\"
    Next fil
Next

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:MaximumIQ
ID: 35488179
Some of the recovered file are probably corrupt then, in that case my script will likley give you the same result
0
 
LVL 7

Assisted Solution

by:CitizenRon
CitizenRon earned 400 total points
ID: 35488193
Whatever the reason, here's an attempt at a script to do what you want.  Hope it works right, I had to simulate everything.
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFolder = "G:\Recover\"

Set objSourceFolder = objFSO.GetFolder(strSourceFolder)

For Each Subfolder in objSourceFolder.SubFolders
	Set objFolder = objFSO.GetFolder(Subfolder.Path)
	If Left(objFolder.Name,6) = "recup_" Then
		Set colFiles = objFolder.Files
		For Each objFile in colFiles
			strExtension = objFSO.GetExtensionName(objFile.Name)
			If LCase(strExtension) = "jpg" And objFile.Size < 1048576 Then Exit For
			If Not objFSO.FolderExists(strSourceFolder & strExtension) Then objFSO.CreateFolder(strSourceFolder & strExtension)
			objFile.Move(strSourceFolder & strExtension)
		Next
	End If
Next

Open in new window

I'm assuming from what you posted that everything is only 1 folder deep, i.e. ALL the files recovered are contained in the root of the "recup_dir" folders.  If not, we'll have to add recursion to the script to go into further subfolders.

The Move method will throw an error if there are more than one file with the same name in different "recup_dir" folders since the destination of the files will be the same.  If it works otherwise, just put:
On Error Resume Next
on the first line of the script and it will simply continue anytime there is an error moving a file.
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 35488249
Ok, so to better complicate things, and looking at the search results more carefully there are several files of the same name located within zip files and other folders.  see attached image.  Any hope?
samefiles.JPG
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 35488294
May something in the script that if it finds the same filename to rename additional files with the same name by say adding a _1 _2 _3, etc to them? Not sure if the duplicate names contain identical data, otherwise i'd say just search and delete duplicates.
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 35488505
Also, I need a script that takes the 6,800 jpg files that are randomly named, to see the modified date of each of these jpg files and move them to a folder that the script will creat with the modified date as the folder name.

So I have all the pictures in G:\Recover2\JPG folder would like subfolders created with the modified date as the name and move those pictures on that date modified into each folder.  
0
 
LVL 4

Expert Comment

by:MaximumIQ
ID: 35492345
replace this line from CitizenRon's code with the code below.
objFile.Move(strSourceFolder & strExtension)


 
if not objFSO.fileexists(objFile) then
      objFile.Move(strSourceFolder & strExtension)
else
      objFile.Move(strSourceFolder & strExtension & "\1_" & objFile.Name 
end if

Open in new window


 This will add 1_ in tbe beggining of the file name if it already exists
0
 
LVL 4

Accepted Solution

by:
MaximumIQ earned 1600 total points
ID: 35492366
Here is what the whole script should look like
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFolder = "G:\Recover\"

Set objSourceFolder = objFSO.GetFolder(strSourceFolder)

For Each Subfolder in objSourceFolder.SubFolders
	Set objFolder = objFSO.GetFolder(Subfolder.Path)
	If Left(objFolder.Name,6) = "recup_" Then
		Set colFiles = objFolder.Files
		For Each objFile in colFiles
			strExtension = objFSO.GetExtensionName(objFile.Name)
			If LCase(strExtension) = "jpg" And objFile.Size < 1048576 Then Exit For
			If Not objFSO.FolderExists(strSourceFolder & strExtension) Then objFSO.CreateFolder(strSourceFolder & strExtension)
			if Not objFSO.FileExists(objFile) then
      				objFile.Move(strSourceFolder & strExtension)
			Else
      				objFile.Move(strSourceFolder & strExtension & "\1_" & objFile.Name 
			End If
		Next
	End If
Next

Open in new window

0
 
LVL 4

Expert Comment

by:MaximumIQ
ID: 35492577
Before we go into the modified date script. Check to make sure the modified date didn't get reset when you recovered the files
0
 
LVL 1

Author Comment

by:fireguy1125
ID: 35497514
Ok great, got the pictures into the JPG folder.  And yes I can confirm that the modified date didn't get reset.
0
 
LVL 1

Author Closing Comment

by:fireguy1125
ID: 35509784
Thank you both MaximumIQ and CitizenRon. I have opened another question for the Modified Date script here: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26990370.html
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

807 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