A script for copying certain files to a new location
- I have a folder with over 50,000 images in it. I need about 15,000 of them copied to another folder.
Is there a script (that I can paste the list of the ones I need copied) to perform this action?
Thanks in advance.
Answers
- You can use this script to copy files from one folder to another.
The files you want to copy should be specified in the text file that you specify using the strFileList constant.
Use the constants to specify source folder, target folder and if files should be overwritten or not if they already exist at the target.
The script doesn't output anything if a file is copied successfully. If you want that for logging reasons add Wscript.echo strFileToCopy copied successfully or whatever you want it to say after the line ' File copied successfully
Then run the script using cscript cscriptname.vbs and if you want the result logged to a file you can just type cscript cscriptname.vbs > logfile.log instead
Option Explicit ' The source path for the copy operation. Const strSourceFolder = "H:" ' The target path for the copy operation. Const strTargetFolder = "C:\Temp\Target\"
' The list of files to copy. Should be a text file with one file on each row. No paths - just file name. Const strFileList = "C:\filelist.txt" ' Should files be overwriten if they already exist? TRUE or FALSE. Const blnOverwrite = FALSE
Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Const ForReading = 1 Dim objFileList Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False) Dim strFileToCopy, strSourceFilePath, strTargetFilePath On Error Resume Next Do Until objFileList.AtEndOfStream ' Read next line from file list and build filepaths strFileToCopy = objFileList.Readline strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy) strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
' Copy file to specified target folder. Err.Clear objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite If Err.Number = 0 Then
' File copied successfully
Else
' Error copying file Wscript.Echo "Error " & Err.Number & " (" & Err.Description & "). Copying " & strFileToCopy End If Loop
Let me know if you have any questions.
Per- Marked As Answer byBSonPoshMVP, ModeratorFriday, April 03, 2009 1:36 AM
All Replies
- You can use this script to copy files from one folder to another.
The files you want to copy should be specified in the text file that you specify using the strFileList constant.
Use the constants to specify source folder, target folder and if files should be overwritten or not if they already exist at the target.
The script doesn't output anything if a file is copied successfully. If you want that for logging reasons add Wscript.echo strFileToCopy copied successfully or whatever you want it to say after the line ' File copied successfully
Then run the script using cscript cscriptname.vbs and if you want the result logged to a file you can just type cscript cscriptname.vbs > logfile.log instead
Option Explicit ' The source path for the copy operation. Const strSourceFolder = "H:" ' The target path for the copy operation. Const strTargetFolder = "C:\Temp\Target\"
' The list of files to copy. Should be a text file with one file on each row. No paths - just file name. Const strFileList = "C:\filelist.txt" ' Should files be overwriten if they already exist? TRUE or FALSE. Const blnOverwrite = FALSE
Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Const ForReading = 1 Dim objFileList Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False) Dim strFileToCopy, strSourceFilePath, strTargetFilePath On Error Resume Next Do Until objFileList.AtEndOfStream ' Read next line from file list and build filepaths strFileToCopy = objFileList.Readline strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy) strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
' Copy file to specified target folder. Err.Clear objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite If Err.Number = 0 Then
' File copied successfully
Else
' Error copying file Wscript.Echo "Error " & Err.Number & " (" & Err.Description & "). Copying " & strFileToCopy End If Loop
Let me know if you have any questions.
Per- Marked As Answer byBSonPoshMVP, ModeratorFriday, April 03, 2009 1:36 AM
- Too Cool! Too fast! I don't know how to thank you enough. Took less than 30 minute to copy thos 15,000 file to a ne folder. This would have taken me days, maybe weeks, to manually complete that task. You should submit this to be included with the scripting archive. I am quite sure ther is someone elso who could use it.
HPjr - Yes this is exactly what I am looking for!!! BUT I am an absolute idiot when it comes to scripts. If you could give me more detail on where to put this script to be able to run it, that would be great. I need the dummies instructions to do exactly what this thread is intended from start to finish. If you need to contact me directly u can at evanraisbeck@apersonaloccasion.net THANK YOU in advance
Open notepad
Paste the contents of the script and edit the source path, target path and path to your file list.
Save as, select all file types and save the script with a name that ends with .vbs for example c:\myscript.vbs
vbs scripts are possible to run by double clicking them but this script is better run from a command prompt using the command cscript.exe c:\myscript.vbs
The script runs silently. It doesn't produce any output at all if everything is fine but will let you know if something is wrong. You'll have to check the target folder files to see the progress.- Hello, Your script work fine for me. The only thing that I would need, is a dialog bog that open when you start the script. A dialog box that ask me where is the txt file, what is the sorce folder and what is the destination folder. Once it's done, clic process and then it does it's thing.
Is it complicated to do?
tks
Seby - Awesome I got it to work. That's cool. And I agree with Seby20 but I can do without it. Thank You so much.
- Browsing for folders is possible but the problem is that there's no native way of browsing for files in vbscript. There were a number of ways to do it in XP but as far as I know, none of them work in Vista unless you rely on third party components.
If an inputbox where you enter the file path/name would do it for you it's easier but that's not much better than changing the path in the script. - Ok, well, Could you do it like this....
If my txt file is alway at the same place, it's not a problem, I just overwrite it every time.
But as for files to be copied, it change all the times.
So Can it open a dialog box to tell that look in this folder, all the files are there.
And then start the program copy alone after i've selected the folder?
The destination folder could ask to where I wanna copy those file.
tks
Seby - How about this then?
I added browse dialogs for the folder selections and I rebuilt the output method so that only a summary is shown at the end if it is run using wscript (double click).
If it is run under cscript it will echo a summary at the end as well as results for each file as they are being copied.
I hope it will suit your needs.
Option Explicit ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name. Const strFileList = "c:\filelist.txt" ' Should files be overwriten if they already exist? TRUE or FALSE. Const blnOverwrite = TRUE Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objShell Set objShell = CreateObject("Shell.Application") Dim objFolder, objFolderItem ' Get the source path for the copy operation. Dim strSourceFolder Set objFolder = objShell.BrowseForFolder(0, "Select source folder", 0 ) If objFolder Is Nothing Then Wscript.Quit Set objFolderItem = objFolder.Self strSourceFolder = objFolderItem.Path ' Get the target path for the copy operation. Dim strTargetFolder Set objFolder = objShell.BrowseForFolder(0, "Select target folder", 0 ) If objFolder Is Nothing Then Wscript.Quit Set objFolderItem = objFolder.Self strTargetFolder = objFolderItem.Path Const ForReading = 1 Dim objFileList Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False) Dim strFileToCopy, strSourceFilePath, strTargetFilePath Dim strResults, iSuccess, iFailure iSuccess = 0 iFailure = 0 On Error Resume Next Do Until objFileList.AtEndOfStream ' Read next line from file list and build filepaths strFileToCopy = objFileList.Readline strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy) strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy) ' Copy file to specified target folder. Err.Clear objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite If Err.Number = 0 Then ' File copied successfully iSuccess = iSuccess + 1 If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then ' Running cscript, output text to screen Wscript.Echo strFileToCopy & " copied successfully" End If Else ' Error copying file iFailure = iFailure + 1 TextOut "Error " & Err.Number & " (" & Err.Description & ") trying to copy " & strFileToCopy End If Loop strResults = strResults & vbCrLf strResults = strResults & iSuccess & " files copied successfully." & vbCrLf strResults = strResults & iFailure & " files generated errors" & vbCrLf Wscript.Echo strResults Sub TextOut(strText) If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then ' Running cscript, use direct output Wscript.Echo strText Else strResults = strResults & strText & vbCrLf End If End Sub - It worked!!!!!!
Thanks very much my friend!!!
Last question..
Is is possible that when it open for browsing to open a default place on my harddrive lets say D:\seby\photos\2009\
And then I just have to select inside of 2009 instead of browsing through all the folders everytime.
Same thing for the output.
tks again. you're the best man!!!
seby
Yes, that's easy to fix. You just have to add an option to the BroweForFolder commands.
Like this:
Set objFolder = objShell.BrowseForFolder(0, "Select target folder", 0 , "D:\seby\photos\2009")- Again thank you very much sir.
Seby
- A very big help thanks!
- Hi, Thanks for your script. It has really helped newbies to scripting like us. My challenge is that I want to modify the script to only copy files with names "similar" and not "exactly the same" as the filenames in the text file.For example if the script sees a file name like 'WXXXKI23334', it should search the source location and copy the files having filenames with at least this characters e.g. '00WXXXK123334L' should be copied since it has contains 'WXXXK123334'. Please help.
It's not that difficult to modify the script to use the entries in the input file as search patterns.
You will need to modify these two lines:
strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy)
strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
Change them to this:
strSourceFilePath = objFSO.BuildPath(strSourceFolder, "*" & strFileToCopy & "*")
strTargetFilePath = strTargetFolder
However, that would make the output of the script totally inaccurate.
There's no good way to count the files that are being copied using the CopyFile method when you're using wildcards so the easiest thing would be to remove the logging functions or just ignore the file counts completely.
If it's important to report on the number of files being copied the script must be totally rewritten and I don't feel like that right now so I hope that you can manage with this small change.- Proposed As Answer bysansouchi Thursday, May 14, 2009 5:32 PM
- Thanks for your response. I do not really need very accurate output, anything similar to what is in the fillist would do. Also it is not important to know the number of files copied. I would modify the script and let you know how it goes. Thank you very much for even responding. I am very grateful.
- Proposed As Answer bysansouchi Thursday, May 14, 2009 5:32 PM
- Thanks. It worked fine for me. Hope it does for others too
- This works great, thank you! How do i copy all files in a directory to a new direcotry and then automate the process?
- That's really another issue and much simpler, just this line in a batch script (.bat .cmd): copy c:\folder\*.* d:\folder\
Or if it has to be in vbscript: http://www.microsoft.com/technet/scriptcenter/scripts/storage/files/stfivb02.mspx
When it comes to automating I guess you mean scheduling.
Use task scheduler to configure your script to run at certain times or events. perhof,
After much googling, I was fortunate enough to find your beautiful script!
I added a couple of things like subfolder recusion ( by tweaking scripts I found) .
Would you be kind enough to look this over and see if it's ok or needs fixing?
Thanks for any help,
ec
[code]
' Read a list of images from text file
' and copy those images from SourceFolder\SubFolders to TargetFolder' Should files be overwriten if they already exist? TRUE or FALSE.
Const blnOverwrite = TRUEDim objFSO, objShell, WSHshell, objFolder, objFolderItem, strExt, strSubFolder
Dim objFileList, strFileToCopy, strSourceFilePath, strTargetFilePath
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set WSHshell = CreateObject("WScript.Shell")
Const ForReading = 1' Make the script useable on anyone's desktop without typing in the path
DeskTop = WSHShell.SpecialFolders("Desktop")
strFileList = DeskTop & "\" & "images.txt"' File Extension type
strExt = InputBox("Please enter the File type" _
& vbcrlf & "For Example: jpg or tif")
If strExt="" Then
WScript.Echo "Invalid Input, Script Canceled"
Wscript.Quit
End if' Get the source path for the copy operation.
Dim strSourceFolder
Set objFolder = objShell.BrowseForFolder(0, "Select source folder", 0 )
If objFolder Is Nothing Then Wscript.Quit
Set objFolderItem = objFolder.Self
strSourceFolder = objFolderItem.Path' Get the target path for the copy operation.
Dim strTargetFolder
Set objFolder = objShell.BrowseForFolder(0, "Select target folder", 0 )
If objFolder Is Nothing Then Wscript.Quit
Set objFolderItem = objFolder.Self
strTargetFolder = objFolderItem.PathSet objFileList = objFSO.OpenTextFile(strFileList, ForReading, False)
On Error Resume Next
Do Until objFileList.AtEndOfStream
' Read next line from file list and build filepaths
strFileToCopy = objFileList.Readline & "." & strExt' Check for files in SubFolders
For Each strSubFolder in EnumFolder(strSourceFolder)
For Each strFileToCopy in oFSO.GetFolder(strSubFolder).FilesstrSourceFilePath = objFSO.BuildPath(strSubFolder, strFileToCopy)
strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
' Copy file to specified target folder.
Err.Clear
objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite
If Err.Number = 0 Then
' File copied successfully
iSuccess = iSuccess + 1
If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then
' Running cscript, output text to screen
Wscript.Echo strFileToCopy & " copied successfully"
End If
Else
' Error copying file
iFailure = iFailure + 1
TextOut "Error " & Err.Number & _
" (" & Err.Description & ")trying to copy " & strFileToCopy
End If
Next
Next
LoopstrResults = strResults + 0 '& vbCrLf
strResults = strResults & iSuccess & " files copied successfully." & vbCrLf
strResults = strResults & iFailure & " files generated errors" & vbCrLf
Wscript.Echo strResultsSub TextOut(strText)
If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then
' Running cscript, use direct output
Wscript.Echo strText
Else
strResults = strResults & strText & vbCrLf
End If
End SubFunction EnumFolder(ByRef vFolder)
Dim oFSO, oFolder, sFldr, oFldr
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not IsArray(vFolder) Then
If Not oFSO.FolderExists(vFolder) Then Exit Function
sFldr = vFolder
ReDim vFolder(0)
vFolder(0) = oFSO.GetFolder(sFldr).Path
Else sFldr = vFolder(UBound(vFolder))
End If
Set oFolder = oFSO.GetFolder(sFldr)
For Each oFldr in oFolder.Subfolders
ReDim Preserve vFolder(UBound(vFolder) + 1)
vFolder(UBound(vFolder)) = oFldr.Path
EnumFolder vFolder
Next
EnumFolder = vFolder
End Function
[/code]