|
Answer» Can anyone tell me how I can write a batch file on an XP system that will find all the files of a certain file type on an entire hard drive and copy just the files to a specific folder?
For example, I would like to gather all the text files on a drive and copy them all to a folder for further scrutiny.
This seems simple enough, but I haven’t figured it out yet SINCE XCOPY will copy the entire directory along with the files.
Thanks in advance. I am always amazed at the people who look for batch solutions, when they have so MANY other options:
Code: [Select]strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _ ExecQuery("Select * from CIM_DataFile where Extension = 'txt'")
For Each objFile in colFiles strCopy = "C:\Archive\" & objFile.FileName _ & "." & objFile.Extension objFile.Copy(strCopy) Next
Save this little ditty with a VBS extension and run from the command prompt as cscript scriptname.vbs
PS. I used c:\archive as the directory to put the copies; change as you wish.
Note: this may run a while, so if you have a spare copy of War and Peace, now would be a good time to crack it open.
8-)Hey thanks man, the script works great!
I guess I just thought there would be a DOS solution to this problem.
I am not really up to speed on VBscript. Is there any chance that you could explain how the script works? Do you know of any good VBS tutorials online?
I see that it gathers all the files off of EVERY drive. Is there a way to make it drive specific?
Thanks for all your help! It's easy enough to restrict this script to a specific drive by adding new criteria to the SELECT statement:
Code: [Select]strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _ ExecQuery("Select * from CIM_DataFile where Drive = 'C:' And Extension = 'txt'")
For Each objFile in colFiles strCopy = "C:\Archive\" & objFile.FileName & "." & objFile.Extension objFile.Copy(strCopy) Next
How does it work? [sigh] I wish I knew
Actually it's fairly straightforward. The first 3 lines are boilerplate code to connect to the Windows Management Instrumentation service (WMI). Once connected to WMI, the script runs a query against the CIM_Datafile class specifically looking for files on the C drive and having a txt extension. The CIM_DataFile class represents all the files on a system. Each file selected is added to colFiles which is used as a collection.
Next up, the script loops thru the collection and processes each selected file by concatenating a literal ("C:\Archive\") and two properties of each file (FileName and Extension). A dot is thrown in to create a legal path. This path represents the destination directory for the COPY method, which is performed in the very next statement.
Try using Google to find VBS tutorials. For other script resources (tools, articles, and a script repository where you can copy/paste scripting examples and with minor modifications, use on your own system) check out the Script Center
Good luck. 8-)
FYI: Using recursion, this script could have been done in batch But nobody needs that much aggravation.The batch code to accomplish that task isn't too aggrivating in my opinion: Code: [Select]for /f "delims=" %%a in ('dir c:\*.txt /a-d /s /b') do copy "%%a" c:\archive The code is basically: Use the DIR command to list all *.txt files in the C: drive, then loop through each (with FOR) copying to (an existing) c:\archive.Wow, I knew it could be done in DOS!
In any event, this has proven rather useful to me, so thanks to all.
These scripts can be used to sort, organize and back up all important files by file type. Very cool indeed!
As for the batch code, it looks pretty strait forward compared to the VB script. The VBscript takes a while to run if you have LOTS of DRIVES, but seems to work well and is very stable.
It is easy to get overwhelmed when you start researching windows programming because the scope is vast. I did some research on CIM and here is what I found out.
According to Microsoft, CIM classes are the parent classes upon which WMI classes are built. I found this link to be helpful. Maybe I can even figure out how to make further use of other CIM properties.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/cim_datafile.asp
|