none
Batch File - Replace old version RRS feed

  • Question

  • I have a Word document that I have created which contains several macros. This file is copied by other employees into different folders on the file server for them to use.  I often make admentments to the master copy and have to email this to everyone asking them to replace their versions with the latest.  This often doesn't happen meaning they are using an old version which can cause issues.

    What I am trying to do is create a batch file that I can run overnight that will search the file server (basically every folder and subfolder where the batch file is created - nothing higher) looking for an old version and replace it with a new version.  The file name contains the version.

    I have been using the below but it does not seem to be working. 

    for /F "tokens=*" %%P in ('dir /b /s "Menu v1.9.docm') do copy /Y .\"Menu v1.10.docm" "%%P"
    @Pause

    This is not something I have written myself as I don't normally write batch files.

    Can anyone help?

    Monday, January 27, 2014 11:58 AM

Answers

  • Sorry for the delay in getting back, I've been off work for a while.

    I've done the above and the output is shown below.

    Sorry, this will not work. You're trying to use advanced scripting techniques without having a solid knowledge of the basics. I recommend that you pay a consultant to do the job for you.
    Tuesday, March 4, 2014 1:26 PM

All replies

  • What is your question?

    To replace or update files at a target location use the REPLACE command.

    At a command prompt type "REPLACE /?" and follow the instructions.

    In an organization we generally have a group or enterprise level templates folder.  This folder is where common code would be placed.  If we create a template with our macros (.dotm) and then reference this in a document the template can be updated centrally and all copies of the document will use it. This is the fundamental design of all MSOffice programs.  It is one of the main reasons we use Office.

    To learn how to use MSWord in a collaborative environment please post your questions in the MSWord forum.  They will show you where to find learning resources and tools used to manage Office documents.

    Learning how to correctly use Windows and MSOffice can save you a lot of headaches.  With Office products scritps and batch files are not needed when the tools are used correctly.


    ¯\_(ツ)_/¯

    Monday, January 27, 2014 12:25 PM
  • Since you did not formulate a question I'll have to guess but I assume that this batch file fill do the trick:

    @echo off
    set SourceFolder=S:\Masters
    set Shares=U:\Shares
    set Doc=Menu v1.9.docm
    for /F "tokens=*" %%P in ('dir /b /s "%Shares%\%Doc%"') do xcopy /d /y /c  "%SourceFolder%\%Doc%" "%%P"

    pause

    You must adjust lines 2 and 3 to reflect your environment.


    Monday, January 27, 2014 3:54 PM
  • Sorry, I suppose the question I am asking is how do I create a batch file to search all files on a share and replace any called 'menu v1.9docm' with 'menu v.10.docm'

    I tried your example Forest brook but it just closes instantly without replaing any files.  I'm not very familiar with this kind of scripting but I presume there is some kind of syntax error.

    Wednesday, January 29, 2014 10:57 AM
  • Sorry, I suppose the question I am asking is how do I create a batch file to search all files on a share and replace any called 'menu v1.9docm' with 'menu v.10.docm'

    I tried your example Forest brook but it just closes instantly without replaing any files.  I'm not very familiar with this kind of scripting but I presume there is some kind of syntax error.

    You do not need a batch file. Just type the command:

    replace "c:\temp\menu v1.9.docm" \\server\share /s /u 

    This will replace all files in all subfolders on the share with the source file.  THat is how we have been doing this since the very first version of DOS. We do not need to use scripts to do this.


    ¯\_(ツ)_/¯

    Wednesday, January 29, 2014 11:09 AM
  • Sorry, I suppose the question I am asking is how do I create a batch file to search all files on a share and replace any called 'menu v1.9docm' with 'menu v.10.docm'

    I tried your example Forest brook but it just closes instantly without replaing any files.  I'm not very familiar with this kind of scripting but I presume there is some kind of syntax error.

    When you test batch files then it is essential to run them from a Console (cmd.exe). If you don't then you will miss most error messages.

    If you have a file "menu v1.9.docm" then there is no point in copying the new file "menu v10.docm" over it. "menu v1.9.docm" would have the correct contents but the wrong name which would cause never-ending confusion. The file's name must match its contents! To do this you must take two steps:

    1. Delete the old file.
    2. Put the new file into the folder where the old file was.

    Try the code below. After setting the first few lines to match your environment, run it at the Console to see what it would do if it was active. After you remove the word "echo" from the last two lines it will actually do its job. Do not remove the "pause" command until you are absolutely sure that the script does what you think it should do!

    @echo off
    set Old=menu v1.9.docm
    set New=d:\My Data\menu v10.docm
    set SourceFolder=S:\User Data

    for /F %%a in ('dir /b /s "%SourceFolder%\%Old%"') do call :Sub %%a
    goto :eof

    :Sub
    for /F %%I in ('echo %*') do set Loc=%%~dI%%~pI
    echo del "%*"
    echo copy "%New%" "%Loc%"

    pause


    Wednesday, January 29, 2014 11:30 AM
  • Thanks for that Forest Brook.

    That all makes sence and I have given it ago.  It now runs but returns errors.

    The first error is 'The specified path is invalid', which I presume is when it is trying to delete the file.  The next error is 'Could not find D:\test\New', which I presume is when it is trying to copy the new file.

    I have checked the paths and file names and they are correct.  Any ideas what it could be?

    Friday, February 7, 2014 10:25 AM
  • You must replace the first line
    @echo off
    with
    rem @echo off
    so that you can see exactly where and why the error occurs.
    Friday, February 7, 2014 11:23 AM
  • Sorry for the delay in getting back, I've been off work for a while.

    I've done the above and the output is shown below.

    J:\DATA>rem @echo off
    
    J:\DATA>set Old=Menu.docm
    
    J:\DATA>set New=J:\DATA\Menu1.docm
    
    J:\DATA>set SourceFolder=J:\DATA\Test
    
    J:\DATA>for /F %a in ('dir /b /s "J:\DATA\Test\Menu.docm"') do call :Sub
    %a
    
    J:\DATA>call :Sub J:\DATA\Test\New
    
    J:\DATA>for /F %I in (' echoJ:\DATA\Test\New') do set Loc=%~dI%~pI
    The specified path is invalid.
    
    J:\DATA>del "J:\DATA\Test\New"
    Could Not Find J:\DATA\Test\New
    
    J:\DATA>copy "J:\DATA\Menu1.docm" ""
            0 file(s) copied.
    
    J:\DATA>PAUSE
    Press any key to continue . . .

    The thing that stands out to me is 'J:\DATA\Test\New'  surely it should be saying 'J:\DATA\Test\Menu1.docm?

    

    

    Tuesday, March 4, 2014 11:47 AM
  • I think somebody need to learn the basics of Windows.  You need to take some responsibility for understanding and writing your script.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 12:13 PM
  • Sorry for the delay in getting back, I've been off work for a while.

    I've done the above and the output is shown below.

    Sorry, this will not work. You're trying to use advanced scripting techniques without having a solid knowledge of the basics. I recommend that you pay a consultant to do the job for you.
    Tuesday, March 4, 2014 1:26 PM
  • I think somebody need to learn the basics of Windows.  You need to take some responsibility for understanding and writing your script.


    ¯\_(ツ)_/¯

    I'm not sure if i would call creating a batch file the basics of windows.

    I'm trying to understand it as best as I can.  I have changed it a bit to see if I can solve it but just can't, hence why I am asking for help.  I understand the process of it and most of the code but not things like Loc=%dI%~pI 

    Tuesday, March 4, 2014 1:37 PM
  • I think somebody need to learn the basics of Windows.  You need to take some responsibility for understanding and writing your script.


    ¯\_(ツ)_/¯

    I'm not sure if i would call creating a batch file the basics of windows.

    I'm trying to understand it as best as I can.  I have changed it a bit to see if I can solve it but just can't, hence why I am asking for help.  I understand the process of it and most of the code but not things like Loc=%dI%~pI 

    Yes - learning would be helpful. Try the help.  SET /?

    I also suggest that, since you are not a trained batch scripter, that you learn PowerShell as it will be easier to understand.  Batch is pretty much obsolete except for those of us that like to play with it.  Iztislike jugging with both hands tied behind your back.

    PowerShell just works and it is much easier to learn.

    But, as you wrote in your initial post, you are not a technician and don't care to learn.  You want someone to do it for you.  I recommend looking for a consultant as they will be able to work closely with you to try and get you what you want.

    My suggestion is to stop inventing things without a good understanding of how to manage these things in a computer environment.  This solution is built into MSWord if only you would learn how to use it.  Word has global and group templates that can be changed in one location and will effect every copy of word in the network.  A good office consultant can show you how to use MSWord correctly.  You are using it like a fancy copy of notepad.  Why spend 300+ dollars per copy if you are only going to use it like notepad.  Notepad and Wordpad are free.

    Office is used everywhere because it does all of this for you.  You do not need to "sneakernet" a solution.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 1:50 PM
  • This is why asking the right question is essential. This is a perfect example of an XY problem. This means you have problem X, but you think the answer is Y. But instead of asking about X, you ask about Y.

    As jrv said, I think the correct answer is something like this:

    Deploy your Word macros from a central location

    Bill

    Tuesday, March 4, 2014 5:26 PM
    Moderator