Overwrite a read-only file in batch with xcopy - windows

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.

Related

Program to move unlocked files to an new folder

i need something that will move files from one folder to another network location, but leave any locked files that are currently being written to!
any ideas?
apols i am no developer, but i have been trying to use
ROBOCOPY C:\test c:\test\q /move /R:0 /W:0
but even if i open a file, it still moves it to the new directory
mal
You can use for %%f in (*) do move %%f destination\%%f (in a batch file) and just allow each individual invocation of move to fail, watching the errors scroll by.
There are also options for making for %%f work recursively, though that's a bit involved.

How to move folders in command prompt merging folders with the same name and replacing files with the same name?

I'd like to move my program files and user documents from C:\Windows.old to C:\, completely replacing what's currently in C:. The old and new OSs on my computer are both windows 10, I just had to make a clean install because I couldn't get it to start after a restoration that went wrong. How can I do this operation using the command prompt at windows startup? Is there a command to do this : move, merge folders, replace files with the same name?
Thank you!
First, don't move program files just like that. Programs don't store data only here. Absence of this data might prevent some programs from running, especially licensed ones because they will think it's an attempt of running the executable directly on another computer. A better solution is to reinstall the programs, as it will put everything in the good place to make it is running OK.
Here's the command:
xcopy <Source> <Destination> /s /y
Replace <Source> and <Destination> with their appropriated values. Example:
xcopy C:\Windows.old\Users\David C:\Users\David /s /y
xcopy will ask if it is a file or folder destination. Answer that it is a folder by typing the appropriate letter.
Explanations:The /s command line switch means to copy the entire file and folder structure, while /y means to not ask when overriding files. If you want to look even more geeky, add /f.

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

Robocopy: /move parameter not working correctly

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

Incremetal backups of directories with a batch/shell script

I'm trying to create a script that will automatically backup a complete directory tree. I also want it to work for incremental backups. Basically, it wil work like this:
If file is in both source and destination and they are different, source file will be copied
If file is in both source and destination and they are the same, nothing will be copied
If file is only in the source, source file will be copied
If file is only in the destination, destination file will be deleted.
I'm still new to shell scripting and I'm not sure how I could implement this. Any ideas? Windows batch scripts would be better, but shell scripts that run on Cygwin are also fine.
Take a look at rsync tool - it was designed to minimize traffic during files synchronization.
Also, probably "cp" with "-u" argument will be useful:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
For windows batch scripting xcopy (native to windows) or robocopy (a free download in windows) both work extremely well.
An example xcopy script (in a .bat file):
#echo off
:: /L = list only.
:: /v=Verify, /y=No prompting /s=subdirs /z=Network mode (supports bad)
:: /i=Tells xcopy dest is folder /f=Display names /d=Copy only changed
echo Backing up projects...
xcopy e:\projects h:\projects /V /Y /S /Z /I /F /D
It will even support orphaned files (if you delete something from your source you no longer need a copy in the backup). Xcopy is typically fine for your needs until you deal with sync between NTFS and Fat32 file systems - the later only has a 2 second resolution and problems with daily savings time so you occasionally run into issues: (a) on time change day you might not get a backup of a changed file - depends on change-regularity of course or you might get a backup of all files even though none have changed (b) because of time resolution some files may backup even though they haven't changed.

Resources