Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to Search for and move specific files

Posted on 2010-11-11
11
Medium Priority
?
678 Views
Last Modified: 2012-05-10
Hello All,

I am looking for a script to search a file server for specific files and then move them to an external drive. I have an excel spreadsheet of all the file names I am looking for but they are spread out over several directories on several servers.

I am ok with running the script individually on each server but I need a script that will take the file names in my spreadsheet and search the server for them. Once they are found they need to be moved to a specified location.

I need to do this for approximately 15,000 files - all of which I have the names of.

Any help would be greatly appreciated.

Thanks,
0
Comment
Question by:tabush
[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
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34117040
I suspect no matter how that is coded it is going to run for quite a bit of time, are you okay with that?

~bp
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34117048
Would you be okay with first exporting the list of files from excel to a text file, and then letting the script read the text file?

~bp
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34117158
Here's a first pass at a solution for you.  It reads the list of files from a text file, loads them into a dictionary for fast searching, and then processes all files in the tree rooted at the folder you specify.  Adjust the paths at the top to suit your needs.

Currently the script will just echo to the console the files it would copy so you can see if it works right.  If so, comment out the wscript.echo and uncomment the f.copy line below it.

~bp
' Define constants
Const ForReading = 1
 
' Define literals
strList = "C:\Temp\EE26609636.txt"
strSource = "C:\Temp"
strDest = "C:\Temp\EE26609636\"
 
' Set up objects needed
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set dicFiles = CreateObject("Scripting.Dictionary")
dicFiles.CompareMode = vbTextCompare
 
' Load dictionary entries from text file of file names
Set objList = objFSO.OpenTextFile(strList, ForReading)
Do Until objList.AtEndOfStream
   strLine = objList.Readline
   If Not dicFiles.Exists(strLine) Then
      dicFiles.Add strLine, ""
   End If
Loop
objList.Close
set objList = Nothing
 
' Start recursive tree scan looking for files to copy 
ScanFolder(objFSO.GetFolder(strSource))
 
Function ScanFolder(objFolder)
   ' Look at all files in this folder, copy if in dictionary of files to copy
   For Each f In objFolder.Files
     If dicFiles.Exists(f.Name) Then
       Wscript.Echo f.Path
       'f.Copy strDest
     End If
   Next
 
   ' Recursively process each subfolder in this folder
   For Each sf in objFolder.SubFolders
      ScanFolder(sf)
   Next
End Function
 
' Wrap up
set dicFiles = Nothing
set objFSO = Nothing

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:Gitcho
ID: 34117955
uh - did you want this done in powershell, or vbScript? (above example is vbScript)

Export your spreadsheet to a CSV with all the files to search in one column with the heading "filename"

Run this script on each server ... it will take a while

#CSV file
$CSVfile = "c:\file_names.csv"
#Path to copy all discovered files to 
$destinationPath = "c:\newfolder"
#import list of short filenames ("spreadsheet.xls" ... NOT "c:\files\spreadsheet.xls")
$filesToSearch = Import-CSV $CSVfile
#create single-dimensional array 
$filesToSearch = $filesToSearch | % { $_.filename } 
#iterate through all files on the server
(gci c:\ -recurse | ? { $_.psIsContainer -eq 0 })  | % { 
    If ($filesToSearch -contains $_.name) { 
         move-item $_.FullName -destination $destinationPath;
    }
}

Open in new window

0
 
LVL 5

Expert Comment

by:Gitcho
ID: 34117960
ahh - I see ...  was posted in powershell forum as well - my mistake.
0
 
LVL 2

Author Comment

by:tabush
ID: 34120603
bp,

Thanks for the script. I tested it and while it works if I specify a specific directory, when I specify the source as "C:\" (as this would be the source on the server that I would like to recursively search) I get an error:

Line: 30
Char: 4
Error: Permission Denied
Code: 800A0046

Now, I understand that error may be occuring as there may be files that are read-only. Is there a way to get around this so the script doesnt break if it hits a read only file?

Thanks
0
 
LVL 8

Expert Comment

by:Bill
ID: 34121545
There are a host of file copy utilities out there as well.  Rich Copy might be worth a look.

RichCopy.pdf
0
 
LVL 8

Expert Comment

by:Bill
ID: 34121575
Rich Copy is sort of a GUI based, souped up version of Robocopy.
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 34121882
==> Code: 800A0046

Seems like we should be able to trap that with "on error" or something and work around it, I'll see what I can do...

~bp
0
 
LVL 58

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 34125612
This should fix the 800A0046 error.

' Define constants
Const ForReading = 1
 
' Define literals
strList = "C:\Temp\EE26609636.txt"
strSource = "C:\"
strDest = "C:\Temp\EE26609636\"
 
' Set up objects needed
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set dicFiles = CreateObject("Scripting.Dictionary")
dicFiles.CompareMode = vbTextCompare
 
' Load dictionary entries from text file of file names
Set objList = objFSO.OpenTextFile(strList, ForReading)
Do Until objList.AtEndOfStream
   strLine = objList.Readline
   If Not dicFiles.Exists(strLine) Then
      dicFiles.Add strLine, ""
   End If
Loop
objList.Close
set objList = Nothing
 
' Start recursive tree scan looking for files to copy 
ScanFolder(objFSO.GetFolder(strSource))
 
Function ScanFolder(objFolder)
   On Error Resume Next
   ' Look at all files in this folder, copy if in dictionary of files to copy
   For Each f In objFolder.Files
     If dicFiles.Exists(f.Name) Then
       Wscript.Echo f.Path
       'f.Copy strDest
     End If
   Next
 
   ' Recursively process each subfolder in this folder
   For Each sf in objFolder.SubFolders
      ScanFolder(sf)
   Next
   On Error Goto 0
End Function
 
' Wrap up
set dicFiles = Nothing
set objFSO = Nothing

Open in new window


~bp
0
 
LVL 2

Author Closing Comment

by:tabush
ID: 34155305
Great Work! The script runs as it should. I made a few changes but it looks good.

Thanks
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

604 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