How to use xcopy to select specific folders(Not Files) within another folder - command-line

I am trying to xcopy specific folders from some directory to another folder, problem is i don't want to copy all the folders but i want specific folders. E.g. DirectorySource has folders (folderA,folderB,folderC) but i want to copy folderA and folderB only to DirectoryDestination.

Create a text file, say C:\excludes.txt with the following contents:
DirectorySource\folderC\
then you can copy:
xcopy DirectorySource DirectoryDestination /s /i /exclude:C:\excludes.txt
The file excludes.txt contains a list of strings, one per line. If a file or directory matches that string, then it will not be copied. If you want to exclude folder, it is safer to use the above string instead of simply folderC, which might skip a file called folderC_listing.txt

Related

Zip files using 7zip from command window

Using 7zip, I want to give a location of a folder of files something like...
D:\Home\files Then I want it to zip all the files in that folder and leave them there. So for example zipme.txt become zipme.zip, so all files would keep their name but just become a zip file. I have tried to use
FOR %i IN (D:\Home\files) DO 7z.exe a "%~ni.zip"
But when I do it it adds a zip file for the directory so my output would be in the correct folder but would contain
D:\Home\files\file.zip
D:\Home\files\zipme.zip
zipped files also all items in directory like..
zipme.txt
zipme2.txt
D:\Home\files/zipme2.zip
So how can I zip each file individual in a folder and have the new zipped name be the individual files name
Was able to get this to work.
FOR %i IN (D:\Home\files\*) DO 7z.exe a "%~ni.zip" "%i"

How to copy a file from source to a destination and recreate the folder structure

I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:
SOURCE:
c:\
folder1
sub1
subsub1
file1
file2
I need something where i can tell my "script" something like this
mycopy c:\folder1\sub1\subsub1\file1 h:\
That means that i dont want to
check if folder structure exist
provide on bot sides the complete structure for each file on destination side
I want to
provide the full path and filename on source side
create folder structure if not exist
overwrite file if exist
How can i achieve this?
See the robocopy help page on MS Technet
https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx
In your case you would do something like
robocopy c:\folder1\sub1\subsub1 h:\ file1 <options ...>
Notice that the first two parameters are the source and dest paths only, with no filename at the end
Read the linked help page and test on your desktop ...
Ok it works like this:
You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.
Lets Assume that we have the following source folder structure:
N:\source\a1\b1\c1\d1\e1\f1
and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:
robocopy N:\source h:\destination /e
The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example
file.txt
*.jpg
*.bkp
etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using
robocopy N:\source h:\destination file.txt /e

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.

Xcopy Command excluding files and folders [duplicate]

This question already has answers here:
/exclude in xcopy just for a file type
(5 answers)
Closed 8 years ago.
I want to copy files and folders in a directory to another folder excluding a list of files and folders.Is that possible.
I was just trying to copy excluding file :
xcopy c:\t1 c:\t2 /EXCLUDE:exclusion.txt
But it keeps saying read cant read file : exclusion.txt
Can anyone help me on this?
Thanks
Just give full path to exclusion file: eg..
-- no - - - - -xcopy c:\t1 c:\t2 /EXCLUDE:list-of-excluded-files.txt
correct - - - xcopy c:\t1 c:\t2 /EXCLUDE:C:\list-of-excluded-files.txt
In this example the file would be located " C:\list-of-excluded-files.txt "
or...
correct - - - xcopy c:\t1 c:\t2 /EXCLUDE:C:\mybatch\list-of-excluded-files.txt
In this example the file would be located " C:\mybatch\list-of-excluded-files.txt "
Full path fixes syntax error.
Like Andrew said /exclude parameter of xcopy should be existing file that has list of excludes.
Documentation of xcopy says:
Using /exclude
List each string in a separate line in each file. If any of the listed
strings match any part of the absolute path of the file to be copied,
that file is then excluded from the copying process. For example, if
you specify the string "\Obj\", you exclude all files underneath the
Obj directory. If you specify the string ".obj", you exclude all files
with the .obj extension.
Example:
xcopy c:\t1 c:\t2 /EXCLUDE:list-of-excluded-files.txt
and list-of-excluded-files.txt should exist in current folder (otherwise pass full path), with listing of files/folders to exclude - one file/folder per line. In your case that would be:
exclusion.txt
It is same as above answers, but is simple in steps
c:\SRC\folder1
c:\SRC\folder2
c:\SRC\folder3
c:\SRC\folder4
to copy all above folders to c:\DST\ except folder1 and folder2.
Step1:
create a file c:\list.txt with below content, one folder name per one line
folder1\
folder2\
Step2:
Go to command pompt and run as below
xcopy c:\SRC*.* c:\DST*.* /EXCLUDE:c:\list.txt

batch file to copy a folder containing the most recently created file

Hey guys,
I'm looking for a batch file which will copy a folder and all its contents containing the most recently created file.
I need it to preserve the folder name at the destination aswell.
For example:
If 'c:\test\source\inhere' contains the most recent file, then i would like the 'inhere' directory and all of its contents copied to the destination, c:\test\destination\inhere.
The .bat file would ideally sit in the 'source' folder in the example above.
Thanks!
xcopy and if exist not exist would do everything you need eg:-
#echo off
xcopy "C:\test\inhere\*.* " c:\mirror\test\inhere\ /c /s /r /d /y /i > c:\mirror\xcopy.log
this would copy from inhere (*.* means any file extensions could be .pdf or whatever) TO
IN HERE in a parent folder called mirror. The xcopy.log just writes a log file so you can check
all was successful