Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read filenames from a text file, match to folder and copy

Status
Not open for further replies.

electricwizard84

Technical User
Feb 18, 2011
17
0
0
AU
Hi,

I have developed a script that lists all the files in a folder (and subfolders) and writes the filename to a text file. For example, if filename.avi is in a folder c:\test\movie\filename.avi then the script will create a text file (and located as a destination directory) and on the first line will write filename.avi

If have removed some files from my complete list and what i want is a script that will match the filename of the file located in the main folder (including subfolders) and copy the files to a new folder. For example, if my text file has a list of files written as filename.avi on the first line and filename2.avi on the second line, the script will match the filenames that are located in c:\test (root folder) and copy the files to a new folder.

I know it's a big ask, but would there be anyone who would be able to get my started on this task?

 
Open the text file and read each line. Read a line and check to see if the file exists in the root folder. If it does, copy it to another folder.

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")

strRootFolder = "C:\test"
strDestFolder = "C:\some\path\files"
strReadInFile = "C:\some\path\readin.txt"

'Open text file for reading
set objStream = objFSO.OpenTextFile(strReadInFile, 1, true, 0)

do while not (objStream.AtEndOfStream)
   'read current line in file
   strLine = trim(objStream.ReadLine)
   
   'does the text we read in exist as a file in the root folder?
   if (objFSO.FileExists(strRootFolder & "\" & strLine)) then
      'yep, it does.  Copy it to some other folder.
      objFSO.CopyFile strRootFolder & "\" & strLine, strDestFolder & "\" & strLine, true
   end if
loop

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom a good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Hi Geates,

Thanks so much for your help, and especially providing some code. I'm not sure whether i'm missing something, but even after i declare the variables, the code doesn't seem to do anything. For example, i create some test folders on my desktop, create a list.txt file and then execute the script. Nothing happens and i receive no errors. I've even added in to certain lines a msgbox("works")just to see whether the code is working up to a point, but everything seems to register.

Any idea?
 
I've figured out the problem! When the program is checking to see if the filename.avi exists in the root folder, it does not check subfolders. Is there a way how to improve to code so as to allow this?
 
Yes, the function that checks through folders needs to be recursive - meaning, it calls itself. Everytime a folder is encountered, call the function for that folder.

"pseudo" code
Code:
function searchFolder(strFolder, strFile)
   set objFolders = objFSO.GetFolder(strFolder).SubFolders
   for each objFolder in objFolders
      [red]strPath = searchFolder(objFolder.Path, strFile)[/red]
   next

   set objFiles = objFSO.GetFolder(strFolder).Files
   for each objFile in objFiles
      if (objFile.Name = strFile) then strPath = objFile.Path
   next
   searchFolder = strPath
end function

However, to call the first iteration of this function for every file in your list would be a waste of efficiency. Instead, modify the code to store a directory listing to an array and simply compare off of that. Either way will work but the later is a tad more difficult to implement.

Code:
function indexFolder(strFolder, arrIndex)
   set objFolders = objFSO.GetFolder(strFolder).SubFolders
   for each objFolder in objFolders
      arrIndex = searchFolder(objFolder.Path, arrIndex)
   next

   set objFiles = objFSO.GetFolder(strFolder).Files
   for each objFile in objFiles
      redim preserve arrIndex(ubound(arrIndex) + 1)
      arrIndex(ubound(arrIndex)) = objFile.Path
   next
   searchFolder = arrIndex
end function

- Geates


"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are seldom good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top