How to copy a subfolder into a subfolder using xcopy? - windows

Let's say I'm in C:\test directory, where I have C:\test\myHugeFolder directory and a C:\test\backup directory - and I'd like to copy myHugeFolder into backup from cmd.exe Command Prompt.
So, I thought this usage of xcopy is correct, by using relative paths:
C:\test> xcopy myHugeFolder backup\ /s /e
The thing is, xcopy here was churning for like 15 minutes, also listing each file in myHugeFolder, so I thought all was fine - then when it finished, I look into backup, and there no myHugeFolder; in fact when I search for myHugeFolder, there's only the original:
C:\test>dir myHugeFolder* /s
Volume in drive C has no label.
Volume Serial Number is FFFF-FFFF
Directory of C:\test
18-10-2015 16:26 <DIR> myHugeFolder
0 File(s) 0 bytes
Total Files Listed:
0 File(s) 0 bytes
1 Dir(s) 2.419.708.346.368 bytes free
So, obviously that is not the right command line - where am I going wrong, and what is the right invocation of xcopy to do this kind of a copy?

The test in the question is wrong.
C:\test> dir myHugeFolder
This command will not list anything copied to the C:\test\backup folder.
A correct test is more like this:
C:\test> dir backup
It would show that the contents of C:\test\myHugeFolder was copied into C:\backup, not C:\test\backup\myHugeFolder.
If one wanted a duplicate of C:\test\myHugeFolder in C:\test\backup\myHugeFolder, one way to do that would be:
C:\test> XCOPY myHugeFolder backup\myHugeFolder /E /I
After which the following command would show the desired copy of the myHugeFolder container:
C:\test dir backup\myHugeFolder

Related

Windows xcopy 0 File(s) copied

I am trying to copy a directory but get the following:
xcopy ..\node_modules\ \node_modules\
0 File(s) copied
I run as administrator, but still get the error. Any ideas please?
p.s I actually use the following to stipulate it's a directory. But the above also fails:
echo d | xcopy /d /y ..\node_modules\ \node_modules\
Thanks
You can use the xcopy /E flag to copy the entire directory and subdirectories. Also remove the starting \ of the destination. The trailing slash should prevent the file or directory prompt.
xcopy /E ..\node_modules node_modules\
xcopy ..\node_modules\* \node_modules\
You are specifying the source directory, but not which files to copy.
To copy all the contents like folder, sub-folder or chain of folders we can use below command:
xcopy file-to-copy-path\ where-to-copy-path\ /s /i
/s copies folders and sub-folders
/i If in doubt always assume the destination is a folder e.g. when the destination does not exist.

How to delete some words in a specific line without removing the line completely?

I am getting the path of a file named curl_for_32bit. Now it replies with this:
Volume in drive C has no label.
Volume Serial Number is 901C-31D8
Directory of C:\Users\onlYUs\Documents\jezreel\32BIT
08/24/2016 05:24 PM 1,768,448 curl_for_32bit.exe
1 File(s) 1,768,448 bytes
Total Files Listed:
1 File(s) 1,768,448 bytes
0 Dir(s) 126,746,275,840 bytes free
Now what I want is to get only the line starting with Directory of.. and then, remove the word directory of.. so that what is left would be a path.
Edit:
To output only the line with directory.. is done. Now my question is how do I remove the words "Directory of" so that a path will remain?
Directory of C:\Users\onlYUs\Documents\jezreel\32BIT
Looking at the other linked questions on the same issue, I'm assuming you want the path to the directory as a variable:
#Echo Off
For /R \ %%I In ("curl_for_32bit.exe") Do Set var=%%~dpI
or if you only wanted to move to that directory:
#Echo Off
For /R \ %%I In (curl_for_32bit.exe) Do CD /D %%~dpI

Remove Directory Command is not working as expected

I have following batch script. It takes data from C:\Source into C:\MyTEST\A\webroot\payrollservice.The source folder has two files 1. Web.config and 2. Web_PROD backup.
In the roll script there is a command to rename the config file. When I run the script first time, it is working fine. But when I run the script again , the renaming of file is not working. The root cause is that the remove directory command is not removing the folder. What need to be corrected here?
rem * STEP 1 taking backup of the folder
mkdir "C:\MyTEST\A\webroot\backup\b05232013v1\payrollservice"
xcopy "C:\MyTEST\A\webroot\payrollservice" "C:\MyTEST\A\webroot\backup\b05232013v1\payrollservice" /E /y /H
rem * STEP 2 remove physical folder
rmdir "C:\MyTEST\A\webroot\payrollservice"
rem * STEP 3 create physical folder
mkdir "C:\MyTEST\A\webroot\payrollservice"
rem * STEP 4 Copy sourcecode to Production boxes
xcopy "C:\Source" "C:\MyTEST\A\webroot\payrollservice" /E /y /H
rem * STEP 5 Rename teh config file
ren C:\MyTEST\A\webroot\payrollservice\Web.config WebLabbackup.config
ren C:\MyTEST\A\webroot\payrollservice\Web_PROD.config Web.config
pause
From Xcopy
/e : Copies all subdirectories, even if they are empty.
/y : Suppresses prompting to confirm that you want to overwrite an existing destination file.
/h : Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files.
Reference
Windows batch files: .bat vs .cmd?
From rmdir
/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.
/q : Runs rmdir in quiet mode. Deletes directories without confirmation.
Figured it out. I need to use /s /q at the end of rmdir command.

In DOS how to copy one level of directory structory but not subdirectory structure?

In DOS I run this command to copy just the directory structure (including subdirectories):
xcopy c:\sourcedir d:\destdir /T /E
This will copy all subdirectories under sourcedir.
Example these directories exist:
c:\sourcedir
c:\sourcedir\level1
c:\sourcedir\level2
c:\sourcedir\level2\levelA
c:\sourcedir\level3
c:\sourcedir\level3\levelB
c:\sourcedir\level3\levelC
In above command it would create:
d:\destdir
d:\destdir\level1
d:\destdir\level2
d:\destdir\level2\levelA
d:\destdir\level3
d:\destdir\level3\levelB
d:\destdir\level3\levelC
What I only want created is these:
d:\destdir
d:\destdir\level1
d:\destdir\level2
d:\destdir\level3
I am really just looking for a one-liner command. If that can't be done, I can live with one-liner I have, it just takes 5 minutes instead of the few seconds I was looking for.
Thanks for any help...
for /d %F in ("c:\sourcedir\*") do md "d:\destdir\%~nxF"
If used in a batch file then double up percents (%%F, %%~nxF)

batch/bat to copy folder and content at once

I'm writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this
copy %~dp0file.txt file.txt
If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.
mainfolder/
file1.txt
file2.txt
insidefolder/
file3.txt
file4.txt
file5.txt
if you have xcopy, you can use the /E param, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories
xcopy [source] [destination] /E
The old way:
xcopy [source] [destination] /E
xcopy is deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.
robocopy [source] [destination] /E
robocopy has several advantages:
copy paths exceeding 259 characters
multithreaded copying
More details here.
I suspect that the xcopy command is the magic bullet you're looking for.
It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copy command.
Check out the documentation here.
If your batch file only needs to run on Windows Vista or later, you can use robocopy instead, which is an even more powerful tool than xcopy, and is now built into the operating system. It's documentation is available here.
For Folder Copy You can Use
robocopy C:\Source D:\Destination /E
For File Copy
copy D:\Sourcefile.txt D:\backup\Destinationfile.txt /Y
Delete file in some folder last modify date more than some day
forfiles -p "D:\FolderPath" -s -m *.[Filetype eg-->.txt] -d -[Numberof dates] -c "cmd /c del #PATH"
And you can Shedule task in windows perform this task automatically in specific time.
I've been interested in the original
question here and related ones.
For an answer, this week I did some
experiments with XCOPY.
To help answer the original question, here
I post the results of my experiments.
I did the experiments on Windows 7 64 bit
Professional SP1 with the copy of XCOPY
that came with the operating system.
For the experiments, I wrote some code in
the scripting language Open Object Rexx
and the editor macro language Kexx with
the text editor KEdit.
XCOPY was called from the Rexx code. The
Kexx code edited the screen output of
XCOPY to focus on the crucial results.
The experiments all had to do with using
XCOPY to copy one directory with several
files and subdirectories.
The experiments consisted of 10 cases.
Each case adjusted the arguments to XCOPY
and called XCOPY once. All 10 cases were
attempting to do the same copying
operation.
Here are the main results:
(1) Of the 10 cases, only three did
copying. The other 7 cases right away,
just from processing the arguments to
XCOPY, gave error messages, e.g.,
Invalid path
Access denied
with no files copied.
Of the three cases that did copying, they
all did the same copying, that is, gave
the same results.
(2) If want to copy a directory X and all
the files and directories in directory X,
in the hierarchical file system tree
rooted at directory X, then apparently
XCOPY -- and this appears to be much of
the original question -- just will NOT do
that.
One consequence is that if using XCOPY to
copy directory X and its contents, then
CAN copy the contents but CANNOT copy the
directory X itself; thus, lose the
time-date stamp on directory X, its
archive bit, data on ownership,
attributes, etc.
Of course if directory X is a subdirectory
of directory Y, an XCOPY of Y will copy
all of the contents of directory Y WITH
directory X. So in this way can get a copy
of directory X. However, the copy of
directory X will have its time-date stamp
of the time of the run of XCOPY and NOT
the time-date stamp of the original
directory X.
This change in time-date stamps can be
awkward for a copy of a directory with a
lot of downloaded Web pages: The HTML
file of the Web page will have its
original time-date stamp, but the
corresponding subdirectory for files used
by the HTML file will have the time-date
stamp of the run of XCOPY. So, when
sorting the copy on time date stamps, all
the subdirectories, the HTML files and the
corresponding subdirectories, e.g.,
x.htm
x_files
can appear far apart in the sort on
time-date.
Hierarchical file systems go way back,
IIRC to Multics at MIT in 1969, and since
then lots of people have recognized the
two cases, given a directory X, (i) copy
directory X and all its contents and (ii)
copy all the contents of X but not
directory X itself. Well, if only from
the experiments, XCOPY does only (ii).
So, the results of the 10 cases are below.
For each case, in the results the first
three lines have the first three arguments
to XCOPY. So, the first line has the tree
name of the directory to be copied, the
'source'; the second line has the tree
name of the directory to get the copies,
the 'destination', and the third line has
the options for XCOPY. The remaining 1-2
lines have the results of the run of
XCOPY.
One big point about the options is that
options /X and /O result in result
Access denied
To see this, compare case 8 with the other
cases that were the same, did not have /X
and /O, but did copy.
These experiments have me better
understand XCOPY and contribute an answer
to the original question.
======= case 1 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_1\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 2 ==================
"k:\software\dir_time-date\*"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_2\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 3 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_3\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 4 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_4\"
options = /E /F /G /H /K /R /V /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 5 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_5\"
options = /E /F /G /H /K /O /R /S /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 6 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_6\"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 7 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_7"
options = /E /F /G /H /I /K /R /S /Y
Result: 20 File(s) copied
======= case 8 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_8"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 9 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_9"
options = /I /S
Result: 20 File(s) copied
======= case 10 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_10"
options = /E /I /S
Result: 20 File(s) copied

Resources