Jump to content

[Question] - Complex file copying situation....


decoy5657

Recommended Posts

I am looking to copy a number of .csv files from several sub directories, to a single final destination.

Example:

C:\data\aab\weekly.csv

C:\data\aac\daily.csv

C:\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:\CSVFiles

I’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


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

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 by Yzöwl
Link to comment
Share on other sites

Try this simple batch file:

@ECHO OFF

::=== INITIALIZE VARIABLES===

SET SOURCEPATH=C:\DATA

SET 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

Here is a vbs script that searches all local drives, folders and sub folders for the file extention CSV

When it finds one it copies it to the location you want.

This one copies the files to the destination folder

strComputer = "."
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 destination

folder this will overwrite the existing file

strComputer = "."
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 by gunsmokingman
Link to comment
Share on other sites

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

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

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

Try this simple batch file:

@ECHO OFF

::=== INITIALIZE VARIABLES===

SET SOURCEPATH=C:\DATA

SET 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

This one did the trick!

Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...