0

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?

1
  • 1
    A For loop with the /F option can be used with your Dir command to propagate a variable with your required directory name. For example: @For /F "Delims=" %%G In ('Dir "%ProgramFiles%\SomeProgram\Proggy*" /A:D /B 2^>NUL') Do @Copy "S:\ome\file.ext" "%%G" /Y. To learn more, please see the output from each of for /?, dir /?, and copy /? when Entered in a Command Prompt window. Commented May 20 at 11:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.