decoy5657 Posted July 12, 2006 Share Posted July 12, 2006 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. Link to comment Share on other sites More sharing options...
pmshah Posted July 12, 2006 Share Posted July 12, 2006 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. Link to comment Share on other sites More sharing options...
Yzöwl Posted July 12, 2006 Share Posted July 12, 2006 (edited) 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, 2006 by Yzöwl Link to comment Share on other sites More sharing options...
BlkCrowe Posted July 12, 2006 Share Posted July 12, 2006 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 Link to comment Share on other sites More sharing options...
gunsmokingman Posted July 12, 2006 Share Posted July 12, 2006 (edited) 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, 2006 by gunsmokingman Link to comment Share on other sites More sharing options...
pmshah Posted July 13, 2006 Share Posted July 13, 2006 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. Link to comment Share on other sites More sharing options...
Yzöwl Posted July 13, 2006 Share Posted July 13, 2006 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'! Link to comment Share on other sites More sharing options...
Delprat Posted July 13, 2006 Share Posted July 13, 2006 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) Link to comment Share on other sites More sharing options...
Yzöwl Posted July 13, 2006 Share Posted July 13, 2006 The robocopy /s switch copies subfolders, it doesn't recurse them in the manner you'd expect. Link to comment Share on other sites More sharing options...
gunsmokingman Posted July 13, 2006 Share Posted July 13, 2006 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. Link to comment Share on other sites More sharing options...
decoy5657 Posted July 13, 2006 Author Share Posted July 13, 2006 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! Link to comment Share on other sites More sharing options...
Takeshi Posted July 15, 2006 Share Posted July 15, 2006 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. Link to comment Share on other sites More sharing options...
pmshah Posted July 15, 2006 Share Posted July 15, 2006 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now