Identify which partition is running the script from

Asked

Viewed 113 times

1

In the case of prompt command we can use Windows environment variables to advance paths, such as:

echo %homedrive%

Which will result in the attribute letter where the sistema operacional was installed.

But to tell you the truth, I never thought I’d need to know where Batch was running from as long as he got the right one. What I want is to prevent the Script to be run by the user accessing another machine (mapped drive) or direct from a pen drive.

IF %HOMEDRIVE% == LOCAL_DA_SCRIPT (
   goto INICIAR
) ELSE (
   echo NAO PERMITIDO, ENCERRE O SCRIPT OU PECA AJUDA
   echo PARA O SUPORTE TECNICO
   pause>nul
)
  • I think it might help: https://stackoverflow.com/a/24101703/4713574

  • 1

    I took a good look at it, but it’s not really what I need.

  • I thought I could run the script on secondary partitions for example, then I would have to check if the drive being run is of the type Local Fixed Disk independent of the letter or if it is in the same unit of the OS. But its solution is suitable, very good.

1 answer

2


I got it, the remote cd since it is also a variable:

echo %cd%
rem Exemplo Windows XP: C:\Document and Settings\{currentuser}
rem Exemplo Windows 7: C:\Users\{currentuser}

To get only the partition:

echo %cd:~0,2%
rem Exemplo: C:

So to make the comparison:

IF %HOMEDRIVE% EQU %cd:~0,2% ( goto INICIAR ) ELSE ( goto ERRO )

:INICIAR
rem INICIO DO SCRIPT
pause

:ERRO
echo VOCE NAO PODE INICIAR O SCRIPT DE DENTRO
echo DO PENDRIVE OU MAPA VIRTUAL COPIE OS ARQUIVOS
echo PARA O SEU DESKTOP OU ALGUMA PASTADO SEU SISTEMA
pause>nul
exit
  • 1

    You can also save the cd in an archive txt cd>drivecurrent.txt and then recover in a variable set /p atual=<drivecurrent.txt and then you share her content %:~0,2%. But your way became much simpler

Browser other questions tagged

You are not signed in. Login or sign up in order to post.