preethi
preethi

Reputation: 905

Run batch file on usb from different drive

I am trying to run a batch file on USB (G:\lock.bat) from e:\min.bat which creates a folder lock on usb. My issue is the folder has been created but on E drive instead of the USB drive.

min.bat

   @echo off
IF EXIST %SYSTEMROOT%\System32\TSKILL (
  TSKILL RunSanDiskSecure*
  ) ELSE (
TaskKill /IM firefox* /F
TaskKill /IM RunSanDiskSecure* /F )
    setLocal Enabledelayedexpansion

    set "myDrive="

    for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
        if not defined myDrive vol %%a: >nul 2>nul && ( 
            if exist "%%a:\RunSanDiskSecureAccess_Win.exe" set "myDrive=%%a:"
        )
    )

    if defined myDrive (
        echo drive found [%myDrive%]
    ) else (
        echo USB drive has not been found
) 

%myDrive%
pushd %~d0
Call lock.bat 
pause

    endlocal
exit

lock.bat

cls
@ECHO OFF
title IMAGES LOCKED
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST IMAGES goto MDIMAGES
ren IMAGES "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock Your Secure Folder
set/p "pass=>"
if NOT %pass%== pass123 goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" IMAGES
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDIMAGES
md IMAGES
echo IMAGES created successfully
goto End
:End

I can see the drive of my usb but when called to activate the lock.bat it still creating the folder on E: drive not on USB drive.

Upvotes: 2

Views: 684

Answers (2)

Stephan
Stephan

Reputation: 56155

Your problem is, you call g:\lock.bat, but you don't change the working directory.

So as one of the first lines in g:\lock.bat, use cd /d g:\ or pushd g:\. If you use pushd, you can restore the previous working directory with popd just before you exit `g:\lock.bat``

edit to include ìndiv`s comment:

to set the working Directory to the same path where the (currently running) batchfile resides, use pushd "%~dp0" (or cd /d "%~dp0") (or %~d0:\ if that suits better), so don't have to change the file, if you move it from G: to another drive.

related question

As an alternative, you could set the new working Directory in the calling batch just before you call the second one, but then, you'll have to adapt two lines instead of just the drive letter when you use another usb drive letter.

Maybe you are interested how to automatically detect your USB drive letter.

Upvotes: 2

jonathan x
jonathan x

Reputation: 141

The commsnd cd /d changes the drive so to change the drive put this line at the top of the file cd /d Drive:\ And replace the word Drive with the drive you want the folder to be made in.

Upvotes: 0

Related Questions