Why does xcopy behave strange in makefile - makefile

I am working on Windows. I have a directory structure like the following:
source
anotherFolder
folder
subFolder
1.txt
1.txt
1.bak
destination
folder
Makefile
In the makefile I want to copy the directory structure and the .txt files from source\folder to destination\folder. I am using GNU make. The Makefile looks like this:
target:
xcopy source\folder\*.txt destination\folder /s /e /y
When I execute the same command line on the shell it works. But from the makefile it doesn't copy any file and copies the directory structure of source instead of source\folder. The result looks like this:
source
anotherFolder
folder
subFolder
1.txt
1.txt
1.bak
destination
folder
anotherFolder
folder
subFolder
Makefile
If I add quotes around the paths in the command line, it works:
target:
xcopy "source\folder\*.txt" "destination\folder" /s /e /y
But why? What is the difference?

Related

XCOPY with pattern matching

I am very new to Windows batch scripting so please ignore if it is stupid question, I have a requirement of copying set of files from source to destination only if source file is modified after certain date_time, I managed to do it using XCOPY command.
XCOPY C:\Src\*.txt C:\Target /D /S /Y
This runs fine for first run, now the twist is once destination folder file is processed it will be renamed to some other name and extension, so when next time my script runs it does not find same file name as source in the destination folder because it is processed and renamed. So is there a way in XCOPY or any other Windows command to do the pattern match for the file name int the destination folder and if match found then proceed with date_time check and copy or else ignore?
Example Source Dir Files:
a.txt
b.txt
c.txt
Destination Dir Files After first script run:
a.txt
b.txt
c.txt
Destination Dir Files after processing and before running the script second time:
a_201603071130.ok
b_201603071130.ok
c_201603071130.ok
For second run of the script with XCOPY it does not find the file a.txt in destination as it is processed but the requirement is to copy only if a.txt file is modified after last run.
I can do this by storing the last run time and checking that for next run etc. but I wanted to find out if there is any other way of doing it.
XCOPY C:\Src\*.txt C:\Target /D /S /Y /M
The /M switch changes the A flag for the file.

Batch file to copy and rename files

I need a batch file that:
Copies all items (directories and subdirectories) from folder a to folder b
If I overwrite a file name/folder in folder a then it will correctly rename file/folder in folder b
I have accomplished the first task:
#echo off
xcopy "D:\...\abc\*.*" "D:\...\def" /E
pause
Above copies all the content in the folder to by destination folder i.e.
Folder a:
-folder1, folder2
-fileA, fileB
Is correctly present in folder b. However, if I rename a file in folder a and run the script:
rename folder1 -> folder0
Then this is how file structure looks:
Folder b:
-folder1, folder2, folder0
-fileA, fileB
I want folder1 to be renamed to folder0, how do I do this?
I could overwrite everything (not sure how with xcopy) but is there another solution to this?
I've looked at the various flags, but can't find what I'm after.
I think what you want it robocopy
You can mirror your folderA to folderB on execution of this batch file:
robocopy "D:\...\abc" "D:\...\def" /MIR /FFT /Z /XA:H /W:5
A great tutorial can be found here, explaining the various params

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

copy of files in windows cmd batch

I really cannot find anything to this problem:
I have a folder C:\dir1\dir2 I get this directory via reading a file so I don't know the name of dir1 or dir2 upfront. I want to copy dir2 into a target directory C:\target. At the end I want to have C:\target\dir2 and that dir2 in target shall have all the files which have been in the source of dir2. When I try xcopy with /s switch it copies the files in the source dir2 directly into target without creating dir2 in target. How can I make sure that this directory is created?
I must achieve that dir2 is created automatically.
EDIT:
for /F "tokens=*" %%i in (%maindir%\mydir.txt) do call :process2 %%i
:process2
set sourcefile=%*
xcopy /s "%sourcefile%" target
is basically the part of my batch file in question. Where in mydir.txt I get some directory like C:\dir1\dir2 which contains files file1 file2 etc. In target I have now
target\file1
target\file2
but I want target\dir2\file1 and target\dir2\file2
Hope that makes it a bit clearer.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir\with space"
SET "destdir=c:\destdir"
FOR /f "delims=" %%a IN ("%sourcedir%") DO (
XCOPY /s "%sourcedir%" "%destdir%\%%~nxa\"
)
GOTO :EOF
This should get the job done for you. I'll presume you've already got sourcedir set from your file – I just used a directory I had which happens to contain spaces in its name. Possibly you'd want to append >nul to the XCOPY line to suppress messages.
Your question is a little unclear, but if I understand it correctly, you should have a look at the mkdir command. That will make your directory - only if the directory doesn't already exist. The syntax is:
MKDIR [drive:]path
For example, mkdir \2014\photos\beach makes the directory "2014" in the current directory ( use CD) with the sub folders "photos" and "beach".
That being said, if you have proper syntax for xcopy, it should create the new directory for you IF it doesn't already exist. Have a look again at xcopy.
If the directory does exist, this is a pretty "sure-fire" way to remove it before trying to make it again:
if exist {directory} rd /s /q {directory}

Windows Command Copy all files recursively to a Main Folder

I was wondering if there is a quick and dirty command that can copy all files in a directory and it's subdirectory to another folder. The destination folder will have no subdirectories just all the source files.
Also as an added bit of fun, in case there is a file name conflict, not to overwrite but to rename the destination file with something unique, maybe append _1 to the filename?
This will copy the files and prompt if there is a filename conflict.
The third party tool XXcopy has the ability to flatten a directory tree and handle filename conflicts.
#echo off
for /r "d:\folder" %%a in (*) do copy "%%a" "E:\target folder"
To copy for instance all files from the current folder and its sub directories to the parent folder of the current folder you can use:
for /r . %a in (*) do copy %a ..

Resources