|
Answer» Hi fellows, I'm trying to copy files with the same file extension from a directory and its subdirectories and, to accomplish that task, I'm using the xcopy /s command. But, besides copying the files, the subdirectories are copied too, and I don't want that happen. Can anybody help me?? Thank'sI may be WRONG but I THOUGHT XCOPY was used for directory structures. This little snippet might be helpful:
Code: [Select]@echo off for %%i in ('dir [highlight]path\*.exe[/highlight] /s /B /a:-d') do ( copy %%i [highlight]targetpath[/highlight] )
path and targetdirectory are placeholders for which you must supply values.
Hope this helps. 8-)
Note: *.exe is just an example; change to whatever extension needed.Hi Sidewinder, Thank's for HELPING me!! I'm not good at DOS programming so I'd like to ask you some questions:
1 - Would Path be the directory where my files are at?
2 - Would Targetpath be the directory where I want my files be placed?
Could explain me how your code works?
Thank's!! I'm not Sidewinder, but:
1) Yes
2) Yes
As for the code, my DOS is a little rusty, so he'll need to explain it. Until he does, treat it like it's his magical incantation. Thank's for your help, Dilbert!! alpcruz,
How does it work? Beats me! In the example posted, the code gets a list of all the exe files in a directory and it's subdirectories; then copies them one by one to the target directory.
8-)Sidewinder,
Thank's for the explanation!!
I tested your code and it worked well but it is still not what I really want.
Let me explain:
I tested your code with 4 TXT files. They are placed at C:\Test1\Test2\Test3 but I put the batch file at C:\Test1. So, in the code, dir path = C:\Test1\Test2\Test3 and Targetpath = C:\Test.
What I really want is to set dir path = C:\Test1 and make it get the TXT files at Test1, Test2 and Test3 and copy them to C:\Test.
In this example it is easy because the NAMES of directories are short, but, if they are big dir path would be very large in some cases!!
If you have no doubts about what I want, I ask you: Is it possible to do that??
Thank's for your pacience!!I can't find anything that will handle this for you with an easier code block than
Code: [Select]COPY C:\Test1\*.txt C:\Test COPY C:\Test1\Test2\*.txt C:\Test COPY C:\Test1\Test2\Test3\*.txt C:\Test Trust me, I looked.
I did have a code that would work if Test 1, Test 2 and Test3 were all in C:\, Like so:
C:\Test1 C:\Test2 C:\Test3 C:\Test <- Has the test.bat file
In the above scenario, this works:
Code: [Select]FOR %%A IN (1 2 3) DO COPY C:\Test%%A\*.txt C:\Test\ I had one text file in each folder, and all three copied successfully. Unfortunately, that isn't what you wanted. But it's closer. Dilbert,
I appreciate your help!!
As you said, it's not precisely what I want but those are good ideas and maybe I can handle, in the future, a scenario like that.
I will keep those codes you posted with me!
Thank's a lot!!alpcruz,
Sorry for the confusion. I failed to include the /f switch in the original code:
Code: [Select]@echo off for /f %%i in ('dir path\*.exe /s /b /a:-d') do ( copy %%i targetpath )
Note: the /f switch is supported only in some versions of the command prompt.
As Dilbert has pointed out, there are usually many ways to do the same thing. Think of it as a buyers market.
8-)Ok, Fellows!!
Your tips helped me a lot. I have a better understanding now and I think I can do what I want.
Thank you both!!
|