Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: need currently running batch file to self-replicate itself to another location..  (Read 7231 times)

0 Members and 1 Guest are viewing this topic.

gumbaz

    Topic Starter


    Intermediate

    is there a code to copy a currently running batch file to another location no matter what directory its being ran from.??

    contrex

    • Guest
    When a batch file is running, it can "see" the system variable %0 (percent zero) which holds the batch file's own file name. By using the XP variable substitution, we can do what you want. Let's take an imaginary batch file called specimen.bat which is in folder c:\batch\test. In the batch, when it is running, %0 would give specimen.bat. The option ~f gets the full path so %~f0 would give c:\batch\test\specimen.bat.

    Putting it all together, we get this line which will make the batch file copy itself to where you want it to go, presuming that drive and folder already exist! (If they don't, I'm sure you know how to test for that and create the folder if necessary). Using quotes around the file/path names for safety, and using the COPY /Y switch to avoid a prompt if it already exists. Of course D: stands for whatever drive letter you want to actually put in there. If you want the copied batch to have a different filename from the original, replace the final %0 with the filename or modify it somehow. eg "D:\where\you\want\it\to\go\Copy Of %0"

    copy /Y "%~f0" "D:\where\you\want\it\to\go\%0"

    All the modifiers (the capital I means whatever variable you are using, which is replaced by zero (0) in this solution above)

      %~fI        - expands %I to a fully qualified path name
      %~dI        - expands %I to a drive letter only
      %~pI        - expands %I to a path only
      %~nI        - expands %I to a file name only
      %~xI        - expands %I to a file extension only
      %~sI        - expanded path contains short names only
      %~aI        - expands %I to file attributes of file
      %~tI        - expands %I to date/time of file
      %~zI        - expands %I to size of file

    The modifiers can be combined to get compound results:

      %~dpI       - expands %I to a drive letter and path only
      %~nxI       - expands %I to a file name and extension only
      %~fsI       - expands %I to a full path name with short names only
     
    « Last Edit: August 05, 2007, 04:48:31 AM by contrex »