Search code examples
windowsdirectoryautomationcopysubdirectory

Copy the first file from every folder to a new location in windows


I have a folder with about 100-200 subfolders. All these have 20-30 Jpegs each. I wish to copy only the first Jpeg file from all the subfolders to a New Folder. Is there a way to automate this?

Thanks!


Solution

  • Use a script like vbScript:

    Const rootFolder = "c:\Rootfolder"
    Const targetFolder = "c:\TargetFolder"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    For Each objFolder In fso.GetFolder(rootFolder).SubFolders
    	i = 0
    	For Each sFile In objFolder.Files
    		If i = 0 then
    			fso.GetFile(sFile).Copy targetFolder & "\" & fso.GetFileName(sFile),True
    			i = 1
    		End if
    	Next
    Next