Delete files do not exist in source folder in cmd? - cmd

I have two folders that I backup from source to destination folder using command:
xcopy /E /Y /I /D
Now I want to delete files in destination folder that do not exist in source folder.

There is no 'mirroring' option in xcopy. I have 2 suggestions:
1.
you could run xcopy dest source /L > todelete.txt to obtain a list of files which exist in dest but not in source. Then use a for loop to delete these files in dest.
or
2.
Use robocopy which was designed to use the same options as xcopy but has a lot more funtionality. For instance, a /MIR option to mirror one folder to the other. robocopy is included in all Windows versions from Vista on (the Win7 version might run under XP as well - not tested though).
Besides, it is way faster and and and...

Related

$(SolutionDir) in MS VisualStudio Build events

XCOPY /Y /E "$(SolutionDir)CopyOnBuild\*.*" ".\"
is the command right now. Which tell that the directory to copy files is at same level where the sln is.
My Question is: I want to copy the files to a directory which is one level above the solution directory. How the command should be?
If you are trying to change the destination dir, and make it one level up, try putting two dots instead of one:
XCOPY /Y /E "$(SolutionDir)CopyOnBuild\*.*" "..\"

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

xcopy syntax? (batch)

#echo off
xcopy /s/z/i Q:\U1210.exe C:\Users\jalozinski\Desktop\
START C:\Users\jalozinski\Desktop\U1210.exe
So this purpose of this code is to copy U1210.exe from the Q:\ drive to the desktop, then start the newly copied .exe. But for some reason it copies random folders and files from the Q:\ drive, and I don't know why. I have a feeling it has to do with the /s/z/i (i was trying these so it may be oen of them) or something to do with the filepath of the source. I feel like if I close the filepath it won't fix anything though.
This is batch, by the way. :I
Lets look at what you have for the XCOPY:
/S = Copies directories and subdirectories except empty ones.
/Z = Copies networked files in restartable mode.
/I = If destination does not exist and copying more than one file, assumes that destination must be a directory.
First I would try w/o the /S, because you don't need directories.
I would include /R (Overwrites read-only files.)
I would also include /Y (Suppresses prompting to confirm you want to overwrite an existing destination file.)
This is what I got working the way I think you want it:
#ECHO OFF
set source=Q:\U1210.exe
set dest=C:\Users\jalozinski\desktop\
xcopy %source% %dest% /Z /R /Y
start %dest%\U1210.exe
exit
#echo off
xcopy Q:\U1210.exe C:\Users\jalozinski\Desktop\
START C:\Users\jalozinski\Desktop\U1210.exe
that should be all you need
/s is copying all the subfolders
/i is assuming it is a directory (when in doubt)
/z is a preventitive measure if you have a very slow computer
so you should not need any of those commands

How to copy files using a batch file? [duplicate]

This question already has answers here:
How do I copy files using Windows Batch?
(6 answers)
Closed 4 years ago.
I need to create a batch file to copy certain files (e.g., 19_1_White.jpg) to a folder within the directory. How can I do this?
XCOPY F:\*.* G:\ /C /S /D /Y /I
XCOPY allows for copying of files, directories, and sub directories.
F:\*.* is our source location
G:\ is our destination location
/C tells XCOPY to ignore errors, and finish what can be done
/S tells XCOPY to copy everything including directories and
subdirectories
/D tells XCOPY to only copy source files that are newer than the
destination files (big time saver on the process)
/Y tells XCOPY to overwrite files without asking for confirmation
/I tells XCOPY to automatically create new directories on the
destination, if new directories were created in source.
you need to use the command XCOPY
eg XCOPY A.jpg C:\Folder\A.jpg
Take a look at https://support.microsoft.com/en-us/help/289483/switches-that-you-can-use-with-xcopy-and-xcopy32-commands for any other switches you may want to use too.

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