Robocopy: /move parameter not working correctly - windows

I'm using a robocopy batch script to move files from one server to another,
but I need it to delete the files on the original server after it is done
(which should happen when you use /move).
The copying works fine, but the files and folders aren't deleted afterwards.
Can anyone tell me what might be going wrong?
Command:
robocopy "\\Srv04\data\logs" "F:\Logs" /move /S /minage:8
thanks,

Gotcha:- Please note that using ROBOCOPY with the /MOVE command other than permission problems won’t remove source directories and files if those items already exists at the target because they will be skipped.
You can get around this by quoting a new target destination.

Question solved: It was a permission problem

Related

windows batch copy * but omit a single folder

How can I copy all files/folders in a directory using a batch file but omit one? I know I could always delete it after the fact but it is an extremely large folder and I don't want to waste the time copying it. There are numerous files in the location I want to copy so doing it one individually would be unrealistic. Thank yoU!
robocopy "source" "destination" files_to_copy.ext /e /xd "folder_to_exclude"
robocopy is an upgraded version of xcopy, thus, is more useful. You can check the complete usage by typing robocopy /?, or from the microsoft docs, or from ss64.
EDIT: Almost forgot, robocopy accepts wildcards, so you can type *.* to copy every files. The line I suggest already have the option /e, which will copy all subfolders, even the empty ones. /xf:file_to_exclude.ext can also be used to skip a specific file type, eg. /xf:"*.txt" to exclude all the .txt files.
xf is used to exclude files, xd to exclude folders.

Xcopy with excluding folders (sub-directories)

I want to copy files and folders in a directory to another folder excluding sub-folders with files contains it, as for example I have a large number of files for node_modules directory which like 100Mb with 50K+ files, that I don't need to copy.
I tried using xcopy like this :
xcopy . c:\inetpub\CIVEBuildCentral\UI\. /Y /S /EXCLUDE:CIVE\UI\elist.txt
and elist.txt contains :
\node_modules\
But no luck, and its really annoying syntax and I don't see its optimal to check-in such a useless file for this case.
Any idea how to solve this?
Well, after searching I found a similar question in StackOverflow but was not so helpful for my case:
Xcopy Command excluding files and folders ( its marked as duplicated but actually it's not really duplicated, it is the different case even if answer seemed the same way, and found no answer for my case anyway)
But I found that if you using Windows 7 or later, you can use robocopy instead, found its so powerful tool compared to old man xcopy, and no need to dirty work for exceptions, the command to achieve what I need replaced xcopy with :
robocopy . c:\inetpub\CIVEBuildCentral\UI\. /IS /S /XD node_modules
For full documentation for it, you can see this link: http://ss64.com/nt/robocopy.html
Solved my issue, and the output of the result was so nice, clear, and well formatted.

Overwrite a read-only file in batch with xcopy

We have an internal excel addon we deploy on a regular basis across a number of UNC directories. Each copy is set to read only so that the users can't alter it on accident. The "deployment" process involves going to each directory, and copying the file into the location with a click and drag. Since the file is read only there is no conflict, users shutdown the excel window and restart, they have the updates.
I've set out to replace this with a batch file that does it automatically as the number of directories continues to grow and there is occasionally an error such as forgetting to set the file to read only.
I'm using xcopy like so:
xcopy "%workingdir%%filename%" "%uncpath%%targetdirectory%" /y /k
And I'm getting access denied on writing over the file. Is there a way to achieve this functionality that we get from click and drag using Batch? I'm certain that there must be a way to do it but all the solutions we've seen so far involve code to momentarily remove "Read Only" and then copy the file. I don't believe that is a viable solution as it may lock access to the file if someone is loading it at that split second.
EDIT: Discovered moments after posting this that it is the xcopy flag /r
Not sure how I missed it, just one of those days I suppose. Thanks.
Adding OP's edit as an actual answer:
xcopy source [destination] /y /r
/y Suppress prompt to confirm overwriting a file.
/r Overwrite read-only files.
Sources: ss64.com, Microsoft Docs
If this is a read-only file that will not have the write flag enabled hence you cannot edit it. My idea / workaround for you is to make a file editable and then modify then convert back to read-only.
attrib -r file.txt
your code goes here
attrib +r file.txt
See if this works for you, let me know if you have any questions.

Batch file to copy directory if it has a later created date than the target directory

We have a file share that has a directory containing all of our build version direcotries named like this
WebApp_20140702.1 first number being date second number being the build count for that date
these are then contained in the following directory
\\server\share\product\
What i need to do from a virtual machine is create a batch file that can check the target location on my vm ie. c:\product\ see if it has the latest version from the network share either by comparing the file names or dates, then copy the new version and delete the old if necessary.
So far i can copy the folder over using xcopy but that's about the extent of my dos/batch file knowledge iv had a look around for a while but haven't been able to see anything that i can use
This is what i have so far, as you can see though i dont know how to do the comparison between the two directories as explained above.
xcopy "\\server\share\webapp" "c:\users\username\desktop\webapp" /E /K
I did try to use just /D at the end and just copying the directories from \\server\share\product\ that had a later date than the target but it ended up just copying the whole directory.
EDIT : to make my self clear
i need to find out if i have the latest sub directory but no matter what i do it always copies all the sub directories from \\server\share\product\
ie. the \\server\share\WebApp directory will have the following sub dirs
..\WebApp_20140628.1\
..\WebApp_20140628.2\
..\WebApp_20140703.1\
and my vm will have the directory
c:\product\WebApp_20140628.2\
Now i need to be able to go into the file share see that it has a more up to date subdirectory i need to copy that directory to my vm and then delete the older one from my vm so i would then have
c:\product\WebApp_20140703.1\
OK i eventually found another question that wanted to do a similar thing and the answer worked exactly as i wanted it
Question can be found here: How to get the most recent file using a batch script in windows
i had to use xcopy instead of the copy used in the answer for the above question. here is my solution as well in case anyone needs something similar
(z is the mapped version of the network share i talk about in my question)
pushd "z:\WebApp\"
for /f "tokens=*" %%a in ('dir/b /od') do set newest=%%a
xcopy /e /k "%newest%" "c:\product\"
popd
im not sure if i actually need the popd command as i believe it just goes back to the directory set in the pushd command

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