windowsbatch-filecmd

Windows command script to copy to a directory that has changing version number


In a Windows OS and using batch file commands, how can I program a script to copy a file into a directory location that changes with unpredictable version numbers?

Say today I have this C:\Program Files\SomeProgram\Proggy5.4.243\,

But tomorrow it might be C:\Program Files\SomeProgram\Proggy5.4.252\

Presuming the process removes the old one (which it does), I am able to write this command to find the current location:

dir "c:\Program Files\SomeProgram\Proggy*" /b

and today that produces:

C:\Program Files\SomeProgram\Proggy5.4.243\

Now I need to use that to copy a file into that location. How is that done?


Solution

  • you want to create the batch script to have that command output as a variable, then use the variable in your copy command.

    FOR /F "tokens=*" %%G IN ('your-command-that-finds-path-here') DO (
        SET "programPath=%%G"
    )
    

    the for /F allows you to set a vairable in the batch script. Set the variable and then call it in the next command where you want to use that path in the destination for your copy.