How to robocopy subfolders with content and files with a specific prefix - windows

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

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.

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

converting xcopy to robocopy

Tried to use xcopy and excluding a folder and all its subfolders.
C:\Merged\org\a>xcopy /I /E /Y C:\Merged\org\*.* C:\Merged\dest /exclude:"C:\Mer
ged\org\a\*.*"
Can't read file: "C:\Merged\org\a\*.*"
I guess I cannot exclude folder, but only file with xcopy.
So I think of moving to robocopy, but I'm not sure which flags to use.
What is the equivalent robocopy to my above xcopy ?
Well, you can. Read up on XCOPY /?:
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
The problem is you specify the path you want to exclude to /EXCLUDE when you should specify the name of a file, which contains the "paths" to exclude (it is actually more flexible see above). The error message you get even hints at that.
Create a file, e.g. C:\ignore.txt, that contains the line "C:\Merged\orga\a\", then invoke XCOPY as follows:
xcopy.exe C:\Merged\org\*.* C:\Merged\dest /exclude:C:\ignore.txt /I /E /Y
If you want to use robocopy nevertheless, you can do so like:
robocopy.exe c:\merged\org c:\merged\dest /xd c:\merged\org\a /IS /E
I has been wrote seemless wrappers from xcopy command line to the robocopy. You can find it from here: https://github.com/andry81/contools.
The scripts:
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_dir.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_file.bat

How to use a batch command to copy all folders to a destination location

I have the following line in a bat file:
xcopy script_temp\* \\CHU-Computer-Science\CHU\scripts\ /S /E /H
however, this will only copy files, how can i also have it copy the folder?
The options you used copied empty folders for me on Vista. I don't know if there are differences in versions of Windows.
The /S and /E are mutually exclusive. /E copies subfolders, including empty ones. /S copies subfolders but ignores empty ones.
I thought that the last option specified wins, so the command you used should work. You could try eliminating the /S option and see if that helps on your system.
Or possibly I don't understand your question?

Windows batch command to move all folders in a directory with exceptions

I am trying to write a Windows Batch file that will allow me to move all directories within a given source directory into a target directory that exists within that source directory.
Obviously my move command with need to only apply to directories and also exclude the target directory from being processed.
Is this possible with a Windows batch command?
Robocopy (present in recent versions of windows or downloadable from the WRK) can do this, just use the /xd switch to exclude the target directory from the copy;
robocopy c:\source\ c:\source\target\ *.* /E /XD c:\source\target\ /move
FOR /d %%i IN (*) DO IF NOT "%%i"=="target" move "%%i" target
That won't work - you'll get an error telling you the target directory is inside the source directory or so, even if you explicitly exclude the target directory. What you can do is move the directories to a temporary location which is not under the source, and then move them into the target.
BTW, using the move command won't let you specify folders to exclude. For that you can use xcopy, but note that it will copy the folders, as opposed to move them. If that matters, you can delete whatever you want afterwards, just make sure you don't delete the target dir, which is in the source dir...
Using robocopy included with Windows 7, I found the /XD option did not prevent the source folder from also being moved.
Solution:
SET MoveDirSource=\\Server\Folder
SET MoveDirDestination=Z:\Folder
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E "%%i" "%MoveDirDestination%\%%~nxi"
This loops through the top level folders and runs robocopy for each.
NB: Robocopy mentioned above using the /move flag will copy the files and then delete them from the source folder rather than moving the files. This may be critical if moving large numbers of files from one location to another on the same disk (because move is virtually instantaneous, while copying is a much slower operation)
On windows batch:
FOR /d %%i IN (MySourceDirectory\*) DO move "%%i" MyTargetDirectory\%%~ni
The above command moves all directories found in MySourceDirectory (/d) to MyTargetDirectory using the original directory name
(~ni) Robocopy's move first does a copy, then delete, so it is slower.
This works for me:
move c:\fromDir\*.* c:\toDir\

Resources