Xcopy files and folders into a new folder within the source folder - xcopy

I have a little structure like this:
[ ] root
|
+---- text.txt
+-[ ] folder
+-[ ] copy
Now I'd like to copy all contents (including folder and its possible contents) of root into copy with Xcopy.
How would I do that?

If you really want to do as you ask, you could do it this way:
xcopy root root\copy /E
However, I would not advise that, because you would be making copies of copies since copy is a folder under root.
I would advise a different structure whereby copy is at the same level as root, and you could:
xcopy root root_copy /E
The documentation can be found at various places, depending on your operating system, such as:
http://support.microsoft.com/kb/289483
http://technet.microsoft.com/en-us/library/bb491035.aspx
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
Here's what I typically use:
xcopy src dest /Q /H /E /I /Y
/Q - Does not display file names while copying.
/H - Copies hidden and system files also.
/E - Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/I - If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Y - Suppresses prompting to confirm you want to overwrite an existing destination file.

Related

Copying files from similar multiple folders into each of their child folders

I would like to use a batch file to copy files in a folder to a backup folder (OLD) within, on a Windows server. And I want to do this for multiple folders.
So for example, I have multiple folders, named like this:
C:\A01
C:\A02
C:\A03
...
C:\A50
I will nickname them here as A##.
Each folder has hundreds of files.
Then each year before updating them, I copy those files into a backup folder called OLD, for each A##, so I have a backup of last year's files. Then I copy in new files into C:\A##, in another manual process, individually.
So there will be folders like:
C:\A01\OLD
C:\A02\OLD
C:\A03\OLD
...
C:\A50\OLD
When I do the copy, to the OLD folder, I keep the most recent version of each file. But then the files that I later manually copy in will overwrite in the C:\A## folder and be this year's files, while C:\A##\OLD will contain last year's files.
How can I script this so it will copy all the files for each C:\A## folder into their respective \OLD folder?
I assume I would use a variable for the path I want to copy from and to.
I could use Robocopy or Xcopy.
in a script file
#echo off
cd c:\
for /f "usebackq delims=:" %%a IN (`dir /AD /B ^| FINDSTR /I /R "A[0-9]*"`) DO (
#mkdir "%%a\OLD"
xcopy "%%a\*.*" "%%a\OLD\" /ECIFHRY /D
)
it copy only the updated files (/D) to the OLD subfolder.
if you want to mantain ACLs of the files, use /ECIFHRKOXY
I suggest you to test it in a test folder, that you can generate with this commands directly in the shell
#mkdir c:\test76
cd c:\test76
For /L %a IN (1,1,10) DO #mkdir A%a && echo "the quick brown fox jumps over the lazy dog">A%a\file.txt

Xcopy copies contents of file but leaves the directory out

Need help for xcopy command,
I have a directory structure and wanted to copy few contents of this.
Iam using xcopy for the same but having trouble. My directory structure is below.
I:\Foo\
|
|--coo-->c1-->c2
|--goo-->g1-->g2
|--boo-->b1-->b2
|--fow-->f1-->f2
which in turn contains subfolders and also files.
I want to copy coo and all its subfolder to another location, my xcopy command for the same is,
xcopy I:\Foo\coo e:\temp /e/c/t/h/r/y /s
executing this command copies folders and subfolders from c1-->c2 but folder coo is missing.
-regards,
Manju
xcopy I:\Foo\coo e:\temp copies the contents of coo into temp. If you want to copy coo into e:\temp\coo, you want xcopy I:\Foo\coo e:\temp\coo (assuming temp already exists).
This is more of a user error. xcopy supports a source, a destination, some flags, and an exclude list.
When you use xcopy I:\foo\coo e:\temp, you say that the contents INSIDE the source(I:\foo\coo) should be equal to the contents INSIDE the destination (e:\temp). So you need to use something like:
xcopy I:\foo\coo e:\temp\coo
to make it include the coo folder.

Using Windows commandline, how can I completely replace one folder with another?

So far I have it to where I can copy all the files from c:\Users\John\Folder1 to c:\Users\John\Folder2.
But I am looking to completely swap the folders.
e.g. Replace c:\Users\John\Folder1 with c:\Users\John\SomeFolder\Folder1.
I have this right now: xcopy c:\Users\John\SomeFolder\* c:\Users\John\Folder1 /s /i
This just copies all the files from the c:\Users\John\SomeFolder\Folder1 to c:\Users\John\Folder1 but leaves the files that had been there prior. I want the entire folder to be replaced. If the new folder I am copying no longer has those files, I want them deleted.
Sorry if this is confusing - any help is greatly appreciated.
I think you can create a batch file to do this.
The pseudo-code:
Erase contents of directory 1
Copy the contents from directory 1 to directory 2
The code:
Create a file called swapFiles.bat in your notepad, and enter the following code:
rd /s %1
mkdir %1
xcopy /s /i %2\* %1
How to use it:
swapFiles c:\Users\directory1 c:\Users\directory2
directory1 is the old directory (i.e. the one that will be wiped out)
Hope this helps you
Maybe I'm completely missing your point, but would this not do the job? (example):
rename Folder1 transit
rename Folder2 Folder1
rename transit Folder2
This will mirror the first folder to the second.
Be very careful that the paths are correct.
#echo off
robocopy "c:\Users\John\SomeFolder\Folder1" "c:\Users\John\Folder1" /mir
Do you want to delete this folder c:\Users\John\SomeFolder before copying the folder1 if it is so this code may wok for you
#echo off
robocopy /s c:\Users\John\Folder1 c:\Users\John\SomeFolder\Folder1
rmdir /s /q c:\Users\John\Folder1

MSDOS command(s) to copy files matching pattern in directory structure to local directory

I have a job that periodically runs and archives files into a folder structure that looks like this:
ArchiveFolder
TimestampFolder
JobnameFolder
job<timestamp>.xml
For a given job, I'd like to collect all xml files in the archive folder into a flat directory (no subdirectories, just all the files) without having to drill down into each one, examine for the proper job, then copy the file.
It seems there should be a fairly straigtforward way of doing this. Any suggestions?
EDIT: I guess I wasn't clear here. The TimeStampFolder will have a name of something like 2011-07-24, the JobnameFolder will have a name like FooFeed or BarFeed, and the job file will have a name like job2011-07-24.xml. There are hundreds to thousands of TimeStampFolders, and each one may have one or more job folders in it. Given a specific job name, I want to collect all the files in all the directories that match that job type, and dump them into the local folder, with no subdirectories.
EDIT1:
SET JOB=JobName
SET OF=OutputFolder
START /wait NET USE Z: "\\ServerName\Sharename\ArchiveFolder" password password_here /USER:domainname\username /P:NO
PUSHD Z:\
FOR /F "USEBACKQ tokens=*" %%A IN (`DIR /b /a:d /s ^| FIND /I "%JOB%"`) DO (
FOR /R %%F IN (%%A) DO (
COPY /Y "%%~fF" "%OF%"
)
)
POPD
It basically locates each subdirectory of ArchiveFolder that includes the JobName in it, then digs into each one that it finds to copy the files out of them.
EDIT2:
Added NET USE to access your network share to perform tasks on the files. If your local machine already has the UNC assigned to a driveletter, you can remove the NET USE command line and change Z: to the assigned driveletter.
#ECHO OFF
FOR /R %%v IN (job*.xml) DO COPY "%%v" c:\out\

Having XCopy copy a file and not overwrite the previous one if it exists (without prompting)

I'm sending commands to a remote computer in order to have it copy a file.
I want the file to be copied, but not to overwrite the previous file with the same name (if it exists).
I also need the command to run without any prompts (xcopy likes to prompt whether the target name I've specified is file or directory, and it will also prompt about overwriting a file).
I have good results with xcopy /d.
It will copy NEWER files, and since we can assume that existing files have same time-stamp, you will copy only files that don't exist.
just in case anyone else finds this:
robocopy x:\sourcefolder Y:\destfolder /s /e /r:0 /z
much better than xcopy, even gives you a table at the end informing of any failed or skipped files. Doesn't prompt to not overwrite.
Well, there's a certain remedy! It has helped me with saving much of my effort and time on Win10 while writing a setup for our product demo.
Just try to use piping:
#ECHO N|COPY /-Y SourceFiles Destination
As an example I used this piece of code so that I would have a clean gentle quiet and safe copy!
#FOR /D %%F in ("FooPath") DO #(
#ECHO N|COPY /-Y ^"%%~npdxF\*.*^" ^"GooPath^" 3>NUL 2>NUL >NUL
)
where obviously FooPath is the source and GooPath is the destination.
Enjoy!
(main source: https://ss64.com/nt/copy.html)
Following command copy files and folder but not override file if already exist.
xcopy "*.*" "C:\test\" /s /y /d
No way to make it NOT overwrite as far as I know. but /Y will make it overwrite. and /I will get rid of the file/dict prompt. See xcopy /? for all options
You can also use the replace command. It has two modes: to add files that don't exist there or replace files that do exist. You want the previous mode:
replace <path1> <path2> /A
I had to copy AND rename files, so I got the prompt about creating a file or a directory.
This is the, rather "hackish" way I did it:
ECHO F | XCOPY /D "C:\install\dummy\dummy.pdf" "C:\Archive\fffc810e-f01a-47e8-a000-5903fc56f0ec.pdf"
XCOPY will use the "F" to indicate it should create the target as a file:
C:\install>ECHO F | XCOPY /D "C:\install\dummy\dummy.html" "C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html"
Does C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html specify a file name or directory name on the target
(F = file, D = directory)? F
C:\install\dummy\dummy.html
1 File(s) copied
I've also verified this command leaves existing files alone. (You should too :-)