[Solved]batch scripting help - act on location etc

Discussion in 'Scripting' started by l33tissw00t, Mar 15, 2017.

  1. l33tissw00t

    l33tissw00t MDL Addicted

    Dec 6, 2012
    819
    520
    30
    #1 l33tissw00t, Mar 15, 2017
    Last edited by a moderator: Apr 20, 2017
    So.. here's what im trying to do:

    Say I have a batch.cmd. When ran, it should check its location. If its in a specific folder (i.e. "%USERPROFILE%\Desktop"), it should copy itself to %USERPROFILE%, run the script from there, and exit. If it's not in "bad" folder, it should run.

    i've tried the following, but i must be doing something wrong..

    Code:
    if "%~dp0" == "%USERPROFILE%\Desktop" (
    copy /y "%~f0" %USERPROFILE% 2> NUL
    start "" "%USERPROFILE%\%~nx0"
    )
    p.s. the script is something i wrote up quick for a guy that does laptop rentals. small batch file that clears desktop/doc/music/vid/etc (most of the homedir folders). i obviously dont want the script ran from any of those folders (it gets deleted - tested that), so i wanted some sort of check that will confirm its not being ran from one of the "bad" folders.

    http://pastebin.com/1p7vb309

    EDIT:
    Changed method, works now.

    Code:
    if "%~dp0" == "%USERPROFILE%\Desktop\" goto :BadLocation
    if "%~dp0" == "%USERPROFILE%\Documents\" goto :BadLocation
    
    :BadLocation
    echo Bad location
    copy /y "%~f0" "%USERPROFILE%" 2> NUL
    start "" "%USERPROFILE%\%~nx0"
    
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #2 Compo, Mar 15, 2017
    Last edited by a moderator: Apr 20, 2017
    I'd use all of the locations in the script like this:
    Code:
    @Echo Off
    Set "T="
    For %%A In (
    "%USERPROFILE%\Desktop"
    "%USERPROFILE%\Desktop\BackedUpShortcuts"
    "%USERPROFILE%\Documents"
    "%USERPROFILE%\Downloads"
    "%USERPROFILE%\Dropbox"
    "%USERPROFILE%\Music"
    "%USERPROFILE%\OneDrive"
    "%USERPROFILE%\Pictures"
    "%USERPROFILE%\Videos"
    "%LOCALAPPDATA%\Google\Chrome\User Data"
    ) Do If /I "%~dp0"=="%%~A\" Set "_=T"
    If Defined _ (
    Copy "%~f0" "%USERPROFILE%"
    Start "" "%USERPROFILE%\%~nx0"
    Exit/B
    )
    REM Rest Of Code Here
    [Edit /]
    However since you are removing every single file in every subfolder below each of those locations you may need to cover even more bases by using find.exe
    Code:
    @Echo Off
    Set "T="
    For %%A In (
    "%USERPROFILE%\Desktop"
    "%USERPROFILE%\Documents"
    "%USERPROFILE%\Downloads"
    "%USERPROFILE%\Dropbox"
    "%USERPROFILE%\Music"
    "%USERPROFILE%\OneDrive"
    "%USERPROFILE%\Pictures"
    "%USERPROFILE%\Videos"
    "%LOCALAPPDATA%\Google\Chrome\User Data"
    ) Do Echo(%~dp0|Find /I "%%~A">Nul && Set "_=T"
    If Defined _ (
    Copy "%~f0" "%USERPROFILE%"
    Start "" "%USERPROFILE%\%~nx0"
    Exit/B
    )
    REM Rest Of Code Here