Robocopy in CMD Doesn't Copy the Parent Directory - cmd

For example
I use "robocopy /move /s /e"
to cut C:/folder1/folder2/folder3/
and paste into D:/library/
but I only get D:/library/folder4/folder5/folde6
Where did folder3 go?
edit 1: it's supposed that within folder3 pre-exist folder4, folder5 and folder6.
edit 2: Here's what I'm trying... robocopy /move /s /e "%1" "D:\library"
edit 3: Registry code
edit 4: Expected context menu in W10.

The basic syntax of the Robocopy command is
robocopy source-directory destination-directory [pattern...] [options]
If no pattern is given, the default pattern is *.*.
The querent probably said something like
robocopy C:\folder1\folder2\folder3 D:\library /move /s /e
^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
source directory destination options
This command tells RoboCopy to move all (the default pattern in *.*) the files and directories it finds in C:\folder1\folder2\folder3 to D:\library. Robocopy did that as expected.
To move the directory folder3 from C:\folder1\folder2 to D:\library, the command is
robocopy C:\folder1\folder2\folder3 D:\library\folder3 /move /e
^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^
source directory destination options
Robocopy will create the destination directory D:\library\folder3 if needed. Note that the option /e implies /s.
Since the question appears to refer to how to do it in batch file, and assuming that %1 does not end in a backslash, I suggest to replace robocopy /move /s /e "%1" "D:\library" with
robocopy /move /s /e "%1" "D:\library\%~nx1"

The /MOVE option deleted the source folder, folder3 in this case, after copying to the new destination.
I see you are using both /s and /e. I think you may want to simplify your command and choose either /s, which will not copy empty directories, or /e which WILL copy empty directories.

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 robocopy subfolders with content and files with a specific prefix

It is hard to believe but I seem to be not able to copy a folder with all its files (that begin with a certain character) and subfolders (beginning with the same character) to another folder in Windows 7. I used copy, xcopy and robocopy but all I do achieve is, that all files in the top level directory and all the subdirectories but without their content get copied. What am I doing wrong? I tried several ways, my last try was:
robocopy path\path\here x* path\path\there /E
I also tried
/COPYALL
/MIR
but with the same result.
Your robocopy syntax is incorrect. Is should be:
robocopy path\path\here path\path\there x* /E
ROBOCOPY path\path\here path\path\there \*.* /E

Windows cmd shell xcopy to network directory doesn't work

Im trying to make a batch file that will copy all new files and folders from a source folder to an network directory. All the new subdirectories and new files should be copied (backup).
My code:
xcopy "C:\Source" "T:\Backup" /d/i/s/q
(/d for only new files, /i because source is a dir, /s for all the subdirs and files, /q just to supress the copy text)
Source contains both subdirectories and files (.txt).
The first run it copies Everything as it should. When I add a new .txt file to one of the existing subdirectories and run it again I get the message:
"An error occured when the file The directory is not empty. was being created.
The folder "T:\Backup" could not be created.
0 files copied.
(Translated from Swedish so not 100% original)
The thing is when I try this command to a local source like e.g. "C:\test" and do the same procedure it works.
Anyone who can understand why this doesn't work for the network drive?
Should I try Another command such as robocopy?
Skip xcopy and use robocopy with the /E flag instead. It's built into all recent versions of Windows. Free download for XP.
Example:
robocopy c:\source T:\backup /E
That will copy all the files in the "source" folder to the "backup" folder that haven't been copied already.
And if you don't want to have the output shown on the console (equivalent to the /Q option in xcopy):
robocopy c:\source T:\backup /E /LOG:nul
Robocopy must be better because it should create directories with the \E switch. No overwrites for files, just adds a file with extra letters or extension <> command. Still must defrag.
XCOPY "DRIVE LETTER:\windows.old\USERS" "\computername\D\NAME\" /D /E /C /R /I /K /Y /f

How to replicate a directory structure using xcopy in windows

I have a text file containing a list of folders.My text file looks like this:
"D:\old\FOLDER1"
"D:\old\FOLDER2"
"D:\old\FOLDER3"
"D:\old\FOLDER4"
"D:\old\FOLDER5"
all these folders have subfolders and files under it
what I want to do is use xcopy to copy FOLDER1, FOLDER2 , FOLDER3 FOLDER4 and FOLDER5 replicate folders ,replicating the structure of those folders
so in output , I want to get
D:\output\bkup\FOLDER1\............Including all subfolders and files D:\output\bkup\FOLDER2\............Including all subfolders and files D:\output\bkup\FOLDER3\.......... Including all subfolders and files
D:\output\bkup\FOLDER4\............Including all subfolders and files
D:\output\bkup\FOLDER5\............ Including all subfolders and files
I have written below script which works fine for one folder
set sourceFolder="D:\old\FOLDER5"
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder% "D:\output\bkup%destinationFolder%"
but since # of directories to copy is 100+ ,I like to use a for loop or pass the list of directories to copy in a text file , that's what I don't know how to handle it.
please help me ,I'm not expert in batch file writing.
You can use for /f to read (parse if needed) a textfile line by line. Use "delims=" to read the line as a whole. Don't forget to add quotes to prevent having multiple arguments and strip the quotes in the subprocedure
for /f "delims=" %%a in (yourtextfile.txt) do call :docopy "%%a"
goto :eof
:docopy
set sourceFolder=%~1
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder% "D:\output\bkup%destinationFolder%"
goto :eof
try robocopy, it is more powerfull for your needs, available for XP Prof. or newer:
set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
robocopy "%sourceFolder%" "%destinationFolder%" /MIR
This makes a complete MIRror at "%destinationFolder%".
If you want to copy folders from a text file with xcopy, use the following code:
set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
for /f "usebackqdelims=" %%i in ("My File With Source Folders.txt") do xcopy /seihry "%%i" "D:\output\bkup%destinationFolder%"
Text file with source folders is "My File With Source Folders.txt".
Taking the solution from this Microsoft forum thread:
To copy folder structure of another without copying files.
Here "ProjectsX" folder is replaced into "ProjectsY" - within SVN directory
/E - include empty directories
/XF * - all files are excluded
/XD ".svn" "obj" - specified directories are excluded
D:\SVN>robocopy ProjectsX ProjectsY /E /XF * /XD ".svn" "obj"
Thanks to the hint to robocopy in the previous answer by Endoro. But you don't need mirroring for this really.

copying all contents of folder to another folder using batch file?

I have a folder: C:\Folder1
I want to copy all the contents of Folder1 to another location, D:\Folder2
How do I do this using a batch file?
xcopy.exe is the solution here. It's built into Windows.
xcopy /s c:\Folder1 d:\Folder2
You can find more options at http://www.computerhope.com/xcopyhlp.htm
If you have robocopy,
robocopy C:\Folder1 D:\Folder2 /COPYALL /E
otherwise,
xcopy /e /v C:\Folder1 D:\Folder2
I see a lot of answers suggesting the use of xcopy.
But this is unnecessary. As the question clearly mentions that the author wants the content in the folder not the folder itself to be copied in this case we can do
copy "C:\Folder1\*.*" "D:\Folder2"
Thats all xcopy can be used for if any subdirectory exists in C:\Folder1
if you want remove the message that tells if the destination is a file or folder you just add a slash:
xcopy /s c:\Folder1 d:\Folder2\
RoboCopy did not work for me, and there are some good solutions here, but none explained the XCopy switches and what they do. Also you need quotes in case your path has spaces in it.
xcopy /i /e "C:\temp\folder 1" "C:\temp\folder 2"
Here is the documentation from Microsoft:
XCopy MS Documentation
/s: Specifies to include subdirectories. Excludes empty subdirectories
/e: Copies all subdirectories, even if they are empty
/i: specifies the destination is a folder (Otherwise it prompts you)
Here's a solution with robocopy which copies the content of Folder1 into Folder2 going trough all subdirectories and automatically overwriting the files with the same name:
robocopy C:\Folder1 C:\Folder2 /COPYALL /E /IS /IT
Here:
/COPYALL copies all file information
/E copies subdirectories including empty directories
/IS includes the same files
/IT includes modified files with the same name
For more parameters see the official documentation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
Note: it can be necessary to run the command as administrator, because of the argument /COPYALL. If you can't: just get rid of it.
#echo off
::Ask
echo Your Source Path:
set INPUT1=
set /P INPUT1=Type input: %=%
echo Your Destination Path:
set INPUT2=
set /P INPUT2=Type input: %=%
xcopy %INPUT1% %INPUT2% /y /s
On my PC, xcopy and robocopy need also the path to them, i.e. C:\Windows\System32\xcopy.exe
That's why I use simply "copy":
copy /y ....\Folder1\File.txt ....\Folder2\
#echo off
xcopy /s C:\yourfile C:\anotherfile\
This is how it is done!
Simple, right?
FYI...if you use TortoiseSVN and you want to create a simple batch file to xcopy (or directory mirror) entire repositories into a "safe" location on a periodic basis, then this is the specific code that you might want to use. It copies over the hidden directories/files, maintains read-only attributes, and all subdirectories and best of all, doesn't prompt for input. Just make sure that you assign folder1 (safe repo) and folder2 (usable repo) correctly.
#echo off
echo "Setting variables..."
set folder1="Z:\Path\To\Backup\Repo\Directory"
set folder2="\\Path\To\Usable\Repo\Directory"
echo "Removing sandbox version..."
IF EXIST %folder1% (
rmdir %folder1% /s /q
)
echo "Copying official repository into backup location..."
xcopy /e /i /v /h /k %folder2% %folder1%
And, that's it folks!
Add to your scheduled tasks and never look back.
I have written a .bat file to copy and paste file to a temporary folder and make it zip and transfer into a smb mount point,
Hope this would help,
#echo off
if not exist "C:\Temp Backup\" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP"
if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
xcopy /s/e/q "C:\Source" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\TELIUM"
"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_Log_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
NET USE \\IP\IPC$ /u:IP\username password
ROBOCOPY "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" "\\IP\Backup Folder" /z /MIR /unilog+:"C:\backup_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log"
NET USE \\172.20.10.103\IPC$ /D
RMDIR /S /Q "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
#echo off
:: variables
echo Backing up file
set /P source=Enter source folder:
set /P destination=Enter Destination folder:
set xcopy=xcopy /S/E/V/Q/F/H/I/N
%xcopy% %source% %destination%
echo files will be copy press enter to proceed
pause

Resources