Batch program to copy file when there is a change in source folder - windows

I need a batch program that will copy files from source folder to destination folder whenever there is a change(new files added or files modified) in source folder and restart a specific service.

Refer to the help of xcopy /? using the switch /D
/D : Copy files changed on or after the specified date.
If no date is given, copy only files whose
source date/time is newer than the destination time.
Syntax :
XCOPY source [destination] [options]
Remark :
So, in this batch file below, if you have any spaces in your directory names, you need to enclose them in double quotes.
#echo off
Set SourceDir="C\My Source\stuff"
Set TargetDir="D:\My Backup\stuff"
xcopy %SourceDir% %TargetDir% /i /d /y /e
/D : copy only files whose source date/time is newer than the destination time.
/E : To copy everything, including new directories, you should add the /e switch
/Y : Suppress prompt to confirm overwriting a file.
/I : If in doubt always assume the destination is a folder e.g. when the destination does not exist.

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

copy entire folder on postbuild to a specific folder

Lets say ,
I have xyz.dll in the debug folder, along with resource files folders , de, da ,etc.
F be the project name .
Folder Structure as mentioned below
d:A\B\C\D\E\F\bin\debug
I have to copy the dll and resource file folders from debug folder to C1 folder .
Folder structure as shown below
d:A\B\C1.
available macros are
TargetDir : d:A\B\C\D\E\F\bin\debug
ProjectDir: d:A\B\C\D\E\F\
I tried this but its not working
xcopy $(TargetDir ) $(ProjectDir)........\c1 /R /Y
This copies only the dll not the resources folder .
Any suggestions on how I should do this ?
Use the /S switch to xcopy to include subdirectories recursively
/? is your friend..
C:\>xcopy /?
Copies files and directory trees.
XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
[/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B] [/J]
[/EXCLUDE:file1[+file2][+file3]...]
source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set,
doesn't change the attribute.
/M Copies only files with the archive attribute set,
turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/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.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/V Verifies the size of each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/G Allows the copying of encrypted files to destination that does
not support encryption.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attributes.
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/B Copies the Symbolic Link itself versus the target of the link.
/J Copies using unbuffered I/O. Recommended for very large files.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
finally I found a way ,
xcopy $(TargetDir).$(ProjectDir)..\..\..\C1 /R /Y /E .
There is an * operators surrounding the dot(.) after the $(TargetDir) ,As its not visible when i post it , I'm mentioning it here .

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

Remove Directory Command is not working as expected

I have following batch script. It takes data from C:\Source into C:\MyTEST\A\webroot\payrollservice.The source folder has two files 1. Web.config and 2. Web_PROD backup.
In the roll script there is a command to rename the config file. When I run the script first time, it is working fine. But when I run the script again , the renaming of file is not working. The root cause is that the remove directory command is not removing the folder. What need to be corrected here?
rem * STEP 1 taking backup of the folder
mkdir "C:\MyTEST\A\webroot\backup\b05232013v1\payrollservice"
xcopy "C:\MyTEST\A\webroot\payrollservice" "C:\MyTEST\A\webroot\backup\b05232013v1\payrollservice" /E /y /H
rem * STEP 2 remove physical folder
rmdir "C:\MyTEST\A\webroot\payrollservice"
rem * STEP 3 create physical folder
mkdir "C:\MyTEST\A\webroot\payrollservice"
rem * STEP 4 Copy sourcecode to Production boxes
xcopy "C:\Source" "C:\MyTEST\A\webroot\payrollservice" /E /y /H
rem * STEP 5 Rename teh config file
ren C:\MyTEST\A\webroot\payrollservice\Web.config WebLabbackup.config
ren C:\MyTEST\A\webroot\payrollservice\Web_PROD.config Web.config
pause
From Xcopy
/e : Copies all subdirectories, even if they are empty.
/y : Suppresses prompting to confirm that you want to overwrite an existing destination file.
/h : Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files.
Reference
Windows batch files: .bat vs .cmd?
From rmdir
/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.
/q : Runs rmdir in quiet mode. Deletes directories without confirmation.
Figured it out. I need to use /s /q at the end of rmdir command.

How can I move the contents of one directory tree into another?

I have a directory which contains files and a number of levels of subdirectories:
C:\Source
I would like to move the contents of C:\Source into:
C:\Destination
Requirements:
All files and all subdirectories
within C:\SourceData must be moved
I will be running the command in a
batch file
I can't use Powershell or
any other scripting languages
Attempt 0
XCOPY /E "C:\Source" "C:\Destination"
This works perfectly, but it copies instead of moves. I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time.
Attempt 1
MOVE "C:\Source" "C:\Destination"
This moves the entire C:\Source directory into C:\Destination so I end up with:
C:\Destination\Source
Attempt 2
With some help from this question and accepted answer I came up with:
for /r "C:\Source" %%x in (*) do move "%%x" "C:\Destination"
This moves the files within C:\Source but not the subdirectories or their contents. Note that I used %%x instead of %x as I'm using it in a batch file.
Using FOR seems promising but I'm not sure I've used the right syntax? Have I missed something?
Attempt 3
As suggested by Nick D, I tried rename:
RENAME "C:\Source" Destination
For the example scenario I gave this works fine. Unfortunately my real Destination directory is at a different level to the Source directory and this doesn't seem to be supported:
C:\>REN /?
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
I get "The syntax of the command is incorrect." errors if I try to specify a more complex destination path, for example:
RENAME "C:\Source" "C:\MyOtherDirectory\Destination"
RENAME "C:\Source" "MyOtherDirectory\Destination"
Undoubtedly use robocopy. It is a simple but brilliantly useful tool.
robocopy /move /e sourcedir destdir
This will move all the files and folders, including empty ones, deleting each original file after it has moved it.
If you don't have robocopy installed you can download it by itself or as part of a Microsoft resource kit.
Update:
Adjusted code to a. check whether folders already exist at the destination, in which case move files in that folder over (and continue traversing the source directory structure), otherwise move the folder wholesale.
At the end of the script the source folder is removed altogether to eliminate these folders which have had their files moved over to an already existent folder at the destination (meaning these folders have been emptied but not deleted at the source).
Additionally we check whether a folder is both empty and already exists at the destination in which case we do nothing (and leave the source folder to be deleted to the last line of the script). Not doing this results in "The filename, directory name, or volume label syntax is incorrect." errors.
Phew! Please let me know how you get on with this! I have tested this and it seems to be working well.
for /d /r "c:\source" %%i in (*) do if exist "c:\destination\%%~ni" (dir "%%i" | find "0 File(s)" > NUL & if errorlevel 1 move /y "%%i\*.*" "c:\destination\%%~ni") else (move /y "%%i" "c:\destination")
move /y c:\source\*.* c:\destination
rd /s /q c:\source
In response to ufukgun's answer:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
/s - Copies directories and subdirectories except empty ones.
/e - Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/i - If destination does not exist and copying more than one file, assumes that destination must be a directory.
/y - Suppresses prompting to confirm you want to overwrite an existing destination file.
As stated in another answer, using xcopy may be not as efficient as it would be a native move command that only changes pointers in the filesystem.
Since XCOPY works, you could use XCOPY and DELETE, it's a workaround, but should work?
on Vista use
robocopy source destination /MIR
/MIR .. mirror a complete directory tree (also deletes files in the destination)
else
xcopy
Of course you have to delete the source afterwards :)
I have a directory which contains
files and a number of levels of
subdirectories:
C:\Source
I would like to move the contents of
C:\Source into:
C:\Destination
Maybe I'm missing something, but can't you just rename the folder?
As sent on twitter:
Try combining attempt 3 with attempt 1. Rename to get the destination folder correct, then move "Destination" to the correct place.
#echo on
set SOURCE=C:\Source
set DESTINATION=C:\Destination
xcopy %SOURCE%\* %DESTINATION%\* /s /e /i /Y
PAUSE
i use batch file like this...
or simply call:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
I use a utility called xxcopy. It can move files and have many useful options, you can use it like this :
xxcopy C:\Source C:\Destination /E /RC
the options :
/E to copy everything even empty folders
/RC to remove every source file after each successful copy
you can download free copy of xxcopy for personal use from :
http://www.xxcopy.com/xcpydnld.htm
Maybe a little off-topic, but still usefull:
Some people suggest to just copy + delete the source files, but
moving files is not the same as copying + deleting!
When using the (x)copy function, you allocate new space on the same volume on a harddisk. After the copying the files, the old files (the old allocated space which was required for the files) are beign marked as deleted. While you will achieve the same result as an end-user, moving does something diffrent and more efficient.
Moving files actually only changes some records in the MFT (master file table). These changes only consist of a different path for the user to locate its files. Physically the files still remain in the same sectors on the harddisk.
If all your trying to do is move a directory and the content up one level:
MOVE folder_you_wan_to_move ..
Note that .. refers to the next directory up.
I leave this code I have written based on ljs's attempt,
It moves directory trees as Windows does, overwriting the files that already exists in destination with those in source, and avoiding the slow copy and erase method except if the destination is in a different drive.
If your S.O is not in english must change line 6 with the text used by the Dir command when it finds 0 files.
movedir.bat source_dir destination_dir
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
if %1.==. echo movedir dir1[\*] dir2 & echo move dir1 inside dir2 overwriting coincident files & echo option \* moves only dir1 content & goto end
set S=%~f1
set D=%~f2
set "noFiles= 0 File"
set "R=0" rem R is option [\*] flag
if %S:~-2%.==\.. set "S=%S:~,-2%" & set "R=1"
if not exist "%S%" goto paramERR
if not exist "%D%" goto paramERR
set "Trim=0" & set "Sc=%S%"
:LP
set /A Trim+=1 & (set "Sc=!Sc:~1!") & if NOT !Sc!.==. goto LP
if %R%==0 (if exist "%D%\%~n1%~x1" (set "D=%D%\%~n1%~x1")) else move /y "%S%" "%D%" & goto end
CALL:movefiles "%S%" "%D%"
for /D /R "%S%" %%I in (*) do (set "Way=%%~fI") & (set "Way=!Way:~%Trim%!") & if exist "%D%!Way!" (CALL:movefiles "%%I" "%D%!Way!") else (move /y "%%I" "%D%!Way!\.." > NUL)
rd /s/q "%S%" & if %R%==1 md "%S%"
goto end
:movefiles
dir %1 | find "%noFiles%" > NUL & if ERRORLEVEL 1 move /y "%~1\*.*" %2 > NUL
goto :eof
:paramERR
echo Source or Destination does not exist
:end
I think the easiest thing you could do is to modify your Attempt 2
from
MOVE "C:\Source" "C:\Destination"
to
MOVE "C:\Source\*" "C:\Destination"
KISS ;-)
Edit: this seem not to work, so my advice is to trash away the crappy DOS command line and use Cygwin with BASH as a shell! (or just add the cygwin binaries to the path so you can use mv within DOS, thus not having to change the shell as your requirements state).

Resources