July 12, 200620 yr I am looking to copy a number of .csv files from several sub directories, to a single final destination. Example:C:\data\aab\weekly.csvC:\data\aac\daily.csvC:\data\aad\example.csv(There are other files in the directories, so I will need the script to copy only .csv files)Copy them all to c:\CSVFiles\So to recap… copy all .csv files under c:\data\ to c:\CSVFilesI’d like to use robocopy as the engine for copying them. I guess it would have to be an if then statement to gather what subdirectories are there, and then send a bunch of separate command lines to execute robocopy.
July 12, 200620 yr Download the current version of 4dos, which is now free, & lookup the information on global function. It will work like a charm.If you are willing to pay for it you can opt for 4nt.
July 12, 200620 yr I'll leave the robocopy switches you require to yourself, but a simple 'for loop' should be all you need:FOR /R C:\DATA %%? IN (*.CSV) DO (ROBOCOPY "%%?" C:\CSVFILES\)Hope it helps!<Edit>Just a small addition, I added double-quotes to the variable just in case of spaces in the file name or path.</Edit> Edited July 12, 200620 yr by Yzöwl
July 12, 200620 yr Try this simple batch file:@ECHO OFF::=== INITIALIZE VARIABLES===SET SOURCEPATH=C:\DATASET DESTPATH=C:\CSVFILES::=== COPY FILES FROM SUBDIRECTORIES ===FOR /D %%i in (%SOURCEPATH%\*) DO ( COPY %%i\*.CSV %DESTPATH%)::=== OPTIONAL - DELETE ORIGINAL FILES ===::DEL /f /s /q %SOURCEPATH%\*.CSV::=== ALL DONE ===EXIT
July 12, 200620 yr Here is a vbs script that searches all local drives, folders and sub folders for the file extention CSVWhen it finds one it copies it to the location you want.This one copies the files to the destination folderstrComputer = "." Dim colFiles, Destination, File, Fso, objFile, objWMI Set Fso = CreateObject("Scripting.FileSystemObject") Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colFiles = objWMI.ExecQuery("Select * from CIM_DataFile Where Extension = 'csv'") Destination = "C:\CSVFiles\"'/-> Check For The Folder Exists If Not Fso.FolderExists(Destination) Then Fso.CreateFolder(Destination) End If '/-> Loop To Check All Local Hard Drives And Folders And Sub Folders For Each objFile in colFiles File = Destination & objFile.FileName & "." & objFile.Extension objFile.Copy(File) Next '/-> If There Is No File Type Found If colFiles.count = 0 Then MsgBox "Cannot find that file type", 0 +32, "No File type" WScript.Quit End If MsgBox "Completed copy", 0 + 32, "Finished"This one copies the files to the destination folder, and if a the file exists in the destinationfolder this will overwrite the existing filestrComputer = "."Const OverWriteFiles = TRUE Dim colFiles, Destination, File1, File2, Fso, objFile, objWMI Set Fso = CreateObject("Scripting.FileSystemObject") Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colFiles = objWMI.ExecQuery("Select * from CIM_DataFile Where Extension = 'csv'") Destination = "C:\CSVFiles\"'/-> Check For The Folder Exists If Not Fso.FolderExists(Destination) Then Fso.CreateFolder(Destination) End If '/-> Loop To Check All Local Hard Drives And Folders And Sub Folders For Each objFile in colFiles File2 = Destination & objFile.FileName & "." & objFile.Extension File1 = objFile.Drive & objFile.Path & objFile.FileName & "." & objFile.Extension Fso.CopyFile(File1), (File2), OverWriteFiles Next '/-> If There Is No File Type Found If colFiles.count = 0 Then MsgBox "Cannot find that file type", 0 +32, "No File type" WScript.Quit End If MsgBox "Completed copy", 0 + 32, "Finished" Edited July 12, 200620 yr by gunsmokingman
July 13, 200620 yr Looks like people like to come up with more complex answers than what is required. The 4dos or 4nt solution I offered would go something like this."global copy c:\data\*.csv c:\csvfiles\ " without the quotes wherein global parses all the subdirectories & in each directory runs the command that follows. Global can be used with any other valid command like xcopy, move,del, etc.You can use the standard switches for the dos/4dos copy command to overwrite older files or what ever you want.
July 13, 200620 yr Looks like people like to come up with more complex answers than what is required.If you believe that then I think that you should learn to read and understand!Where in your response did you use robocopy?You dont know if the questioner wishes to retain specific attributes, owners, acls security etc.Why would they need to install another product in order to do a simple copy exercise?The tools, used by all other responders bar yourself, are already available to the questioner. Downloading and installing another product, for me, when the tools to do the job already exist is not an efficient solution.Did my response use the tools already available to the questioner?Was my response a short single line command?Does my response perform the task required?Please explain, in 'simple' terms what you mean by 'complex'!
July 13, 200620 yr Is this not enough ?ROBOCOPY C:\DATA C:\CSVFILES /IF:*.CSV(i don't have it under my hand to try - if robocopy has a /S switch, it is required)
July 13, 200620 yr The robocopy /s switch copies subfolders, it doesn't recurse them in the manner you'd expect.
July 13, 200620 yr Looks like people like to come up with more complex answers than what is required. The 4dos or 4nt solution I offered would go something like this."global copy c:\data\*.csv c:\csvfiles\ " without the quotes wherein global parses all the subdirectories & in each directory runs the command that follows. Global can be used with any other valid command like xcopy, move,del, etc.You can use the standard switches for the dos/4dos copy command to overwrite older files or what ever you want.Since I am better at VBS then cmd promt, the scripts I provided goes threw all the folders on the local hard drives. I could have it go threw just C:\data\ and it sub folders but it would of been a bigger script, then what I wrote.I agree with Yzöwl it makes no sence to add any thing when there are built in tools that does what decoy5657 ask for.
July 13, 200620 yr Author Try this simple batch file:@ECHO OFF::=== INITIALIZE VARIABLES===SET SOURCEPATH=C:\DATASET DESTPATH=C:\CSVFILES::=== COPY FILES FROM SUBDIRECTORIES ===FOR /D %%i in (%SOURCEPATH%\*) DO ( COPY %%i\*.CSV %DESTPATH%)::=== OPTIONAL - DELETE ORIGINAL FILES ===::DEL /f /s /q %SOURCEPATH%\*.CSV::=== ALL DONE ===EXITThis one did the trick!
July 15, 200620 yr Often there's more than one way to do things in computers and outside computers. I quite like it when different people come up with their solutions. Of course in a given situation one would be the best, or the most simple, or the most elegant for that particular need. I'm inclined to opt for the most simple which is usually the most elegant.It reminds me of my well respected old (now deceased) maths teacher at school in SW England. He would show us without fail all the possible ways to solve a maths problem. Truly inspiring.
July 15, 200620 yr Looks like people like to come up with more complex answers than what is required.If you believe that then I think that you should learn to read and understand!Where in your response did you use robocopy?You dont know if the questioner wishes to retain specific attributes, owners, acls security etc.Why would they need to install another product in order to do a simple copy exercise?The tools, used by all other responders bar yourself, are already available to the questioner. Downloading and installing another product, for me, when the tools to do the job already exist is not an efficient solution.Did my response use the tools already available to the questioner?Was my response a short single line command?Does my response perform the task required?Please explain, in 'simple' terms what you mean by 'complex'!If you were to read the exact wording of the original request you will see " I would like to use robocopy..." it is not necessarily an absolute must. It is not part of the windows either. What I have suggested is a reasonably small substitute command shell with extremely powerful features including macros & graphical displays. I am an old dos hand (senior citizen at that) who wet his feet in msdos 3.2. We had to practically do everything from the command prompt & writing fancy batchfiles & macros. There are a whole lot of commands available under cmd which are not available under gui. In any case in no way did I mean to offend any one of you.
Create an account or sign in to comment