.bat file autocopy - how to only copy file once? - batch-file

I want a .bat file that will copy all files and folders from one folder to another folder automatically. For example:
robocopy "c:\source" "D:\target" /e /MON:1 /xc /xn /xo
However I need it so that once a file has been copied, that file will not be copied again, even if the copy has been moved to a different directory. Is there any way to do this? Is it possible for robocopy to create a log and check against that before copying the file?
If robocopy can't do it, what can?

Use additionally the option /M on your robocopy (SS64 article) command line as suggested by Stephan because this option results in copying only files with archive attribute set in source folder tree and removing archive attribute by robocopy after successful copying the file.
%SystemRoot%\System32\robocopy.exe "C:\source" "D:\target" /M /E /MON:1 /XC /XN /XO
The archive attribute is automatically set again on file modification.
You could perhaps also use xcopy (SS64 article):
%SystemRoot%\System32\xcopy.exe "C:\source" "D:\target\" /M /E /C /I /Q /G /H /R /K /Y >nul
Important for your task is again option /M for copying only files with archive attribute set in source folder tree and clearing the archive attribute after copying the file.
Note: /I works without user prompt only with target folder path ending with a backslash.
Run in a command prompt window robocopy /? respectively xcopy /? for details on the other options or read the Microsoft documentations for robocopy and xcopy.

Alright So, the easiest way to do this is to copy each
file and folder individually, to said folder.
This may not be what you are looking for, but
I hope it helps! Sadly there is no way to
copy l files folders with a single command.

Related

Batch script doesn't function properly

So this is what I have so far. The problem I'm having is it archives the file to 7zip and when I try to tell it to move this file it says it's not there. I'm not sure if there's an easier way to move, zip, and rename a file and move it again to another folder.
#Echo Off
xcopy "\\READYSHARE\USB_Storage\Address Book\Address Book" "C:\Users\Service Department\Desktop\Zip"
7za a –tzip "C:\Users\Service Department\Desktop\ZIpped" "C:\Users\Service Department\Desktop\Zip" /s /e
pause
It is always advisable to read the documentation of the commands to use before creating a batch file.
xcopy (Microsoft Windows XP documenation, easier to read) or xcopy (Microsoft TechNet article)
Documentation for 7-Zip standalone command line tool 7za is installed together with 7-Zip in program files folder of 7-Zip. The file to view is 7-zip.chm, a Windows help file.
The switches /s and /e belong to command xcopy, but are appended to call of 7za. /s is for copying also subdirectories, but not empty subdirectories. /e is for copying also subdirectories including empty subdirectories. It is possible to specify both, but usually just /e needs to be specified to copy a directory tree completely using xcopy.
To recursively archive all files and folders of a folder with 7za the switch -r must be used according to 7-Zip documentation.
#echo off
%SystemRoot%\System32\xcopy.exe "\\READYSHARE\USB_Storage\Address Book\Address Book" "%USERPROFILE%\Desktop\Zip" /E /I
"%ProgramFiles%\7-Zip\7za.exe" a –tzip "%USERPROFILE%\Desktop\Zipped.zip" -r "%USERPROFILE%\Desktop\Zip"
pause
I have not executed this batch file, but it should work if path to 7za.exe is correct on your computer.

how to copy files from one directory to other in batch command

I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"

Use batch script to copy directory

I want to copy a folder to the same directory and change its name. Let's say, if I have a folder "C:\Users\Desktop\A", how can I copy it, rename it as "B", and still put it at "C:\Users\Desktop"?
Thanks.
xcopy "C:\Users\Desktop\A" "C:\Users\Desktop\B" /e /i
From the command prompt
xcopy source target /E
/E recursivly copies files and directories.
So:
xcopy c:\users\Desktop\A c:\users\Desktop\B /E

Batch file to copy directories recursively

Is there a way to copy directories recursively inside a .bat file? Is an example of this available?
Look into xcopy, which will recursively copy files and subdirectories.
There are examples, 2/3 down the page. Of particular use is:
To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:
xcopy a: b: /s /e
After reading the accepted answer's comments, I tried the robocopy command, which worked for me (using the standard command prompt from Windows 7 64 bits SP 1):
robocopy source_dir dest_dir /s /e
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
copy *.* C:\dest\dir
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Windows Batch File Looping Through Directories to Process Files?
I wanted to replicate Unix/Linux's cp -r as closely as possible. I came up with the following:
xcopy /e /k /h /i srcdir destdir
Flag explanation:
/e Copies directories and subdirectories, including empty ones.
/k Copies attributes. Normal Xcopy will reset read-only attributes.
/h Copies hidden and system files also.
/i If destination does not exist and copying more than one file, assume destination is a directory.
I made the following into a batch file (cpr.bat) so that I didn't have to remember the flags:
xcopy /e /k /h /i %*
Usage: cpr srcdir destdir
You might also want to use the following flags, but I didn't:
/q Quiet. Do not display file names while copying.
/b Copies the Symbolic Link itself versus the target of the link. (requires UAC admin)
/o Copies directory and file ACLs. (requires UAC admin)

Directo copy batch file command

How can copy a folder(located in the location of batch file)containing another folders and files to another location using batch file...
i am trying using command below but not working..
xcopy /E /Y %~p\DagwoodLoki#hp.com\* C:\Loki
Please help me
I dont know what "%~p" does but i think that following should work
xcopy %~p\DagwoodLoki#hp.com\ C:\Loki /E /Y
the /E and /Y must be written later the source and the target.
Maybe this?
xcopy /E /Y "%~dp0\DagwoodLoki#hp.com\*" C:\Loki
%~dp0 is substituted with drive+path to the batch file, without quotes, if any.

Resources