Script: How to copy/move folders dynamically in Windows using commands - windows

I am having a source folder which has folders (say A,B,C) that i should copy to my destination folder, But while copying if my destination folder has a folder say A, then that folder should not be copied, other two folders should be copied to the destination folder.

I myself found a solution for this.
I use XCOPY with the following parameters: /D /Y /R /H /s /i.
E.g.:
xcopy source destination /D /Y /R /H /s /i

Related

Copy files from a source folder that contains the same filenames as the destination folder

I have a folder of music files that I have added/updated ID3 tags (the source folder). I also have another folder containing some of these files (the destination folder), and I want to overwrite/update these by copying only files from the source folder that exists in the destination folder.
I tried using this xcopy command, which does the job it seems as it copies only files that exist in destination folder. But upon inspection, the relevant files in the destination folder are still the old ones which do not have ID3 tags. I cannot figure out why the copied files did not overwrite the old files:
cd /d "C:\Users\lenovo\Desktop\source"
for %x in (*) do xcopy "%x" "C:\Users\lenovo\Desktop\destination" /L /U /Y /I
xcopy /? says about the /L switch:
/L Displays files that would be copied.
However, by https://ss64.com/nt/xcopy.html:
/L List only - Display files that would be copied.
The latter is right!
Removing the /L option weirdly fixed the problem. The files were now correctly copied:
cd /d "C:\Users\lenovo\Desktop\source"
for %x in (*) do xcopy "%x" "C:\Users\lenovo\Desktop\destination" /U /Y /I

xcopy batch folder tree

S:
cd \newclients
xcopy "s:\clients\*\MER" . /s /d
pause
This is my batch file. In my folder tree we have clients then names folders then MER folder. I want to be able to search the directory for the MER folder and copy that folder along with the client name before it. Is there a way to do this with batch files?
Using if exist should do the thing.
pushd "s:\newclients"
for /f "delims=" %%f in ('dir /b /ad "s:\clients\*"') do (
if exist "s:\clients\%%f\MER\nul" ( xcopy "s:\clients\%%f\MER" "s:\newclients" /s /d )
)
popd
pause
Note: Add a slash if it's to check folder ie. if exist "s:\clients\%%f\MER\" ... or if exist "s:\clients\%%f\MER\nul" ...
To suppress prompt about destination file/folder add /I in xcopy :
/I If in doubt always assume the destination is a folder
e.g. when the destination does not exist.
https://stackoverflow.com/a/33445866/

How can I copy tree folders but keep only folder named "dist" from each?

How can I copy, using Windows cmd (or bat file), a tree of folders with the following structure:
Source/Folder 1/dist
Source/Folder 1/other files.. (multiple)
Source/Folder 2/dist
Source/Folder 2/other files.. (multiple)
...
The thing is I only want to keep the dist folder from each "Folder #", for example the final form to be:
Destination/Folder 1/dist
Destination/Folder 2/dist
Thanks!
ukw.
To use from command line
for /d %a in ("x:\source\*") do xcopy /s /e /y /i "%~fa\dist" "x:\destination\%~nxa\dist"
To use inside a batch file, percent signs in for replaceable parameter needs to be doubled
for /d %%a in ("x:\source\*") do xcopy /s /e /y /i "%%~fa\dist" "x:\destination\%%~nxa\dist"

Copy only directories from a command prompt

I want to copy only directories (it's files and sub directories) within the current directory from a command prompt.
I've already copied all the files in the current directory using this command.
copy * d:\copyfolder
I've tried these for individual folders. To copy a folder:
XCOPY C:\utils D:\Backup\utils /i
To copy a folder including all subfolders.
XCOPY C:\utils* D:\Backup\utils /s /i
But couldn't find a way to copy only directories.
From the command line this should work to copy all folders with subdirectories and files:
for /d %a in (*) do xcopy "%a\*.*" "d:\copyfolder\%a\" /s/h/e/k/f/c
I've tried these for individual folders.
To copy a folder:
XCOPY C:\utils D:\Backup\utils /i
To copy a folder including all subfolders.
XCOPY C:\utils\* D:\Backup\utils /s /i
But this command can help, not overwriting any files.
XCOPY C:\utils\* D:\Backup\utils /s /i /d

Copying files from a CD to hard drive, then run Java

I'm trying to have a bat inside the CD that will copy the Folder 'src' to desktop.
How can I get the files to copy to a directory, like the desktop, and then run the Java command?
xcopy /s/e /y c:\Desktop\Game \src
cd c:\Desktop\Game
java com/brackeen/javagamebook/tilegame/GameManager
#SET $source=*Your source path*
#SET $destination=*Your destination path*
#xcopy "%$source%" "%$destination%" /E /I /R /Y /Z
#IF ERRORLEVEL 1 ECHO Copy failed. && GOTO :EOF
#CD /D "%$destination%"
#*Start your java command*
Xcopy parameters used :
/E Copy folders and subfolders, including Empty folders
/I Assume the destination is a folder when the destination does not exist
/R Overwrite read-only files.
/Y Suppress prompt to confirm overwriting a file.
/Z Copy files in restartable mode.
More at SS64.com

Resources