5

I have received a warning that this is a too "subjective" question, but I can't really find another way to say this in a text as short as a title, so my apologies... any suggestions for a title are appreciated.

So on to my question, yesterday I tried to code something and I failed, simple as that... after several hours of searching for the problem, I still couldn't find it: so in hopes of finding a solution, I am asking, how would you have done it?

Goal:

There is a file called a.bat in the F:\ directory, that needs to be moved to the C:\Users directory when clicked upon, and since the act of moving would disrupt the file, it needs to be re-started from the C:\Users directory after the move. So we need to code a batch within a batch, right?

One that would form when a.bat is clicked, and do the move as well as launching a.bat when the move is finished. Now I can't seem to do it, the code just does not work. It creates a loop of CMDs that forces me to restart the computer, even though it seems like a simple task -- and yes I did if %cd% == C:\Users goto z, which would skip creating the batch that would do the move, if the file is already in that directory.

6

If I'm understanding the question you might try something like this:

@ECHO OFF

:: If the script is not running under "C:\Users" it will copy
:: itself there and then execute that copy which will remove
:: this copy (ie. the copy in F:\) and then perform the normal
::script operations.

IF /I NOT "%~dp0"=="C:\Users\" (
  IF NOT EXIST "C:\Users\%~nx0" COPY "%~0" C:\Users
  IF EXIST "C:\Users\%~nx0" (
    "C:\Users\%~nx0" "%~0"
    GOTO END
  ) ELSE (
    ECHO.
    ECHO Unable to copy the script to "C:\Users".
    ECHO.
    ECHO Ensure you are running the script as an administrator and try again.
    ECHO.
    ECHO Press any key to close this window ...
    PAUSE > NUL
    GOTO END
  )
)


:: The first parameter of the script (%~1) is the path to the old
:: location of the script that needs to be removed.  The parameter
:: is used recursively (above) whenever the script is executed from
:: a location other than "C:\Users".

IF NOT "%~1"=="" IF EXIST "%~1" DEL /F "%~1"


::
:: Put your script code here
::

:END

NOTE:
The batch file will have to run as an admin in order to make a copy in C:\Users unless the permissions of C:\Users have been changed to allow standard users to write to the folder.

  • 1
    The original question can be found here. – Don't Root here plz... Oct 5 '16 at 17:45
  • I looked at it briefly and I think the solution I've suggested here (just updated with more comments and clarification) should work. Let me know if I'm missing something. – David Woodward Oct 5 '16 at 18:00
2

You don't need to move the .bat file. Just call cd to change the directory of the batch instance. Create a new .bat file with the following contents, and run it from any location.

cd C:\Users
dir
pause

This file should output the files and directories in C:\Users

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.