Computer Hope

Microsoft => Microsoft DOS => Topic started by: novice84 on July 13, 2013, 12:04:38 AM

Title: xcopy and rd command limitation
Post by: novice84 on July 13, 2013, 12:04:38 AM
XCOPY

in folder C:\test I have 50 folders from Folder1 to Folder50

I want to copy few folders with same name in C:\test2

Code: [Select]
xcopy C:\test\Folder3 C:\test2 /y /s /e
xcopy C:\test\Folder8 C:\test2 /y /s /e
xcopy C:\test\Folder12 C:\test2 /y /s /e

it is copying everything from Folder3, Folder8 & Folder12 to C:\test2 instead Copying each folder itself.

I want to copy those folders with everything inside these to C:\test2
how to do it? is there any other command should be used ?

RD

Same case (vise versa) with RD command

Code: [Select]
RD /S /Q C:\test\Folder4
RD /S /Q C:\test\Folder6
RD /S /Q C:\test\Folder15

I want to delete all files and folders inside above folders but don't want to delete folders.

I have searched for these answers so much, but no solution found.
Title: Re: xcopy and rd command limitation
Post by: foxidrive on July 13, 2013, 04:01:43 AM
Testing the issue there are two problems.  The target folders need a \ at the end, and those same folders have to be specified.

I tested this on drive d:

Code: [Select]
@echo off
set drv=d:
xcopy %drv%\test\Folder3 %drv%\test2\Folder3\ /y /s /e
xcopy %drv%\test\Folder8 %drv%\test2\Folder8\ /y /s /e
xcopy %drv%\test\Folder12 %drv%\test2\Folder12\ /y /s /e
pause


Here is one way to solve the RD folder issue.  There are other ways.

Code: [Select]
@echo off
for %%a in (Folder4 Folder6 Folder15) do (
RD /S /Q C:\test\%%a
MD C:\test\%%a
)
Title: Re: xcopy and rd command limitation
Post by: novice84 on July 13, 2013, 06:55:45 PM
thanks foxidrive xcopy method you told is more than perfect.
for single or perticular folders it's ok
I want to mix with FOR command
say if we have to find a folder(s) named "tobecopied" to destnation folder
we can make list as you told in previous post
how it will pick folder name itself.


Here is one way to solve the RD folder issue.  There are other ways.

Code: [Select]
@echo off
for %%a in (Folder4 Folder6 Folder15) do (
RD /S /Q C:\test\%%a
MD C:\test\%%a
)

regading above method, it removing and creating it again,

not the perfect solution, but anyway it will serve purpose very well.

same case as xcopy, to get list from FOR command and removing those folders having "Check" name in it.

I was wondering to get 2 things done with one shot.

I hope I have explained very well, if it's not clear, please reply, I will explain with example.

thanks
Title: Re: xcopy and rd command limitation
Post by: foxidrive on July 13, 2013, 08:17:23 PM
for single or perticular folders it's ok
I want to mix with FOR command
say if we have to find a folder(s) named "tobecopied" to destnation folder
we can make list as you told in previous post
how it will pick folder name itself.

That would depend if the folder to be copied is in the current folder, or if it is somewhere in a folder tree.
The simplest code is different for the two cases.


Quote from:
regading above method, it removing and creating it again,
same case as xcopy, to get list from FOR command and removing those folders having "Check" name in it.

Do you want to copy a set of folders with "Check" in the name, and then remove all files/folders from them but leave the Check folders?  Are they in one folder or are they in a directory tree?  Where are they being copied to?

Please explain the task a little more clearly.
Title: Re: xcopy and rd command limitation
Post by: novice84 on July 13, 2013, 09:24:32 PM
That would depend if the folder to be copied is in the current folder, or if it is somewhere in a folder tree.
The simplest code is different for the two cases.

Please forgive my lack of knowledge, I thought it would be simple with just PUSHD command to change path in any case.

well, I have both cases, one to copy from same drive from one location to other, and another case to copy from different drive to C:\somewhere. I prefer to do this now from one drive to another e.g.

find folder having name(s) "*ToBeCopied" from C:\reports to D:\archieve\test
difficult part could be to find it under directory tree in subfolders keeping the same name

Do you want to copy a set of folders with "Check" in the name, and then remove all files/folders from them but leave the Check folders?  Are they in one folder or are they in a directory tree?  Where are they being copied to?

yes you understood correctly. I prefer to leave original folder tree as it is. Just copy folders to Archieve folder on different drive (in case I need to trace back past history), and delete contents from inside of source folders, leaving folders behind.

what I am doing at the moment, I am getting list of folder with For command, which I need to copy/remove inside content.
Then copying each folder manually deleting contents from source folders after that.
Title: Re: xcopy and rd command limitation
Post by: foxidrive on July 14, 2013, 05:44:42 AM
My question was: are there folders in a tree that have to be copied? and I think from previous posts and your comments that the answer is yes.

Try this:

Code: [Select]
@echo off
set "target=D:\archieve\test"
set "source=C:\results"
set "text=ToBeCopied"
setlocal enabledelayedexpansion
pushd "%source%"
for /f "delims=" %%a in ('dir /b /s /a:d ') do (
 set "folder=%%~nxa"
  if not "!folder:%text%=!"=="!folder!" (
    xcopy "%%a\*.*" "%target%%%~pnxa\" /s/h/e/k/f/c
      pushd "%%a"
        rd /s /q "%%a" 2>nul
      popd
  )
)
popd