-1

I am trying to "deploy" word templates with this script:

if not exist "%USERPROFILE%\Documents\Custom Office Templates" (
mkdir "%USERPROFILE%\Documents\Custom Office Templates" 2>nul
if not errorlevel 1 (
    xcopy /y /f /S "\\server\share" "%userprofile%\Documents\Custom Office Templates"
)

But i hit a snag, some installations of Office makes the folder "Custom Office Templates" on its own. But my script in its current state only copys files if the folder does not exist. So i got curious.

Question: Is it possible to make the script copy files if the folder exist? and if it doesn't exist my script proceeds with making the dir and copying files.

I know there are many other ways to deploy these things, and some would maybe be easier, but i want to try with this method. Hope you can help.

Thanks a bunch in advance.

  • 1
    There is no need to check. `mkdir "%USERPROFILE%\Documents\Custom Office Templates"` and `xcopy /y /f /S "\\server\share" "%userprofile%\Documents\Custom Office Templates"` is all that is needed. You are thinking about process not about the desired final state. – CatCat Oct 10 '18 at 07:55
  • 1
    So if the dir is already there because of office its self, the mkdir wont create problems? – Christopher S Oct 10 '18 at 08:47
  • 1
    Nope. Also it is probably not needed as well - See `xcopy /?` and see `/i`. In programming we usually attempt to do something. If it matters, and here it doesn't as it shouldn't fail , we test the result of that attempt. By testing, especially the hard disk, if takes a disk access to check if a file/folder exists. If it doesn't it takes a second access to create. Creating it as the first step is always one disk access whether it exists or not. This is faster, uses less power so longer battery life, and uses less resources so other programs can run faster, especially if using the disk. – CatCat Oct 10 '18 at 08:59
  • It works beautifully. I tend to over exaggerate these things, because like you said, im thinking about the process too much. Thanks a lot for your help. <3 – Christopher S Oct 10 '18 at 09:44
  • If you happened to read the help file for the IF command you might have noticed the ELSE clause that can be used with IF. – Squashman Oct 10 '18 at 12:15
  • 1
    This is exactly what the `/I` option of `xcopy` is for. Taks also a look at [this answer](https://stackoverflow.com/a/33770152) (shameless self-promotion ;-))... – aschipfl Oct 10 '18 at 13:28

1 Answers1

1

You should be able to do it all as a single line command:

XCopy "\\server\share" "%UserProfile%\Documents\Custom Office Templates\" /S /F /Y`

The backslash to the destination will cause the directory to be created if it doesn't already exist.

Compo
  • 36,585
  • 5
  • 27
  • 39