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

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.

Related

Copy all desktop.ini using xcopy

I use this command in order to copy all .ini files (with recursion):
xcopy c:\folder1\folder1A\*.ini c:\mybackup /sy It doesn't copy any file.
There exist desktop.ini files (including comments) within folders and subfolders of folder1A that I want to get a copy of with recursion.
I have tried running CMD as administrator, but it tells 0 File(s) copied. How could I get this to work?
Add the /h option:
/H Copies hidden and system files also. (more info)
This works:
xcopy c:\folder1\folder1A\*.ini c:\mybackup /syh
Thanks to Marged's comment mentioning this question.

How to delete 4,600 embedded folders? [duplicate]

This question already has answers here:
Delete files or folder recursively on Windows CMD
(14 answers)
Closed 6 years ago.
Trying to get this directory off my computer and can't seem to get it off. Tried creating this bat file to delete them and can't seem to get it correct.
:LoopStart
REN "C:\Users\David\Desktop\*" del
MOVE "C:\Users\David\Desktop\del\ "C:\"
RMDIR /S /Q "C:\Users\David\Desktop\del"
GOTO LoopStart
:LoopEnd
file path that is continuous is this :
C:\Users\David\Desktop\com\example\Lab2-1starter\app\src\main\java
Best way to do it easily without any for loops is to use Robocopy
first create a empty directory [say C:\empty] This will be your source directory now, now the idea is to overwrite destination directory .
command to delete the entire content of destination directory :
robocopy c:\empty c:\yourfolder /purge
/PURGE :: delete dest files/dirs that no longer exist in source
Now you can simply run
del /s /q c:\yourfolder
you can use all above command in a bat file to reuse it in a better way.
This will also delete path with long length , make sure you run from admin command prompt.

Delete files do not exist in source folder in 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...

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

Resources