[Batch] root

Discussion in 'Scripting' started by thethingy, Feb 21, 2011.

  1. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #21 thethingy, Feb 22, 2011
    Last edited: Feb 22, 2011
    (OP)
    @alphawaves, it works perfectly now, fresh eyes helped me see I was missing a space between one of the folder names, its now integrated into the main script so do you know how to first rename the searched for file to "backup" then do the copy?, I think I can mod this script to do the restore of the backup.
     
  2. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    #22 Alphawaves, Feb 22, 2011
    Last edited: Feb 22, 2011
    Is this what you mean:

     
  3. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    yip, your good you are :) , for the last part of the tool I need to replace a file that's the same file name as the last but 64bit, different size, what would be the best way to do this as the 32bit one sits in the same folder as the script but the 64 being of the same name needs to go in its own folder?
     
  4. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    Hi you could name the 32Bit file "32Test.txt" and the 64Bit file "64Test.txt", then using the environment variable %processor_architecture% to define if your in 32Bit or 64Bit:

     
  5. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    ^^^thanks, couldn't quite get my head around that so I just renamed the 64dll in my cmd folder then copied it over then renamed it again, crude but work's, now I've got things nearly done is there anything that can be done to hide the workings of the console from the user, say instead of having line after line of text scroll down for 5 minutes while things are working instead have just a window saying "working, please wait"?
     
  6. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    #26 Alphawaves, Feb 22, 2011
    Last edited by a moderator: Apr 20, 2017
    You could use "echo Working, please wait..." at start of cmd,
    use >nul to hide output.. at end of each todo commandline

    EDIT:
    Have you thought about using hstart to call a batch file displaying a working cmd:

    Code:
    @echo off
    echo Working, please wait...
    "%~dp0hstart.exe" /NOCONSOLE /WAIT "%~dp0Test.cmd"
    echo complete.
    exit
    OR messagebox:
    Code:
    "%~dp0hstart.exe" /MSG="Patching in progress, please wait." /TITLE="Patch by thethingy"
    
    hstart source
     
  7. searchengine

    searchengine Guest

    InstallTips executes commands and displays message while the process is still active RyanVM

    The chosen message displays in middle of desktop.

    [​IMG]

    :bye:
     
  8. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    ^^^Thanks, I'm going to try both, its for aesthetics really as I thought I'd learn batches first before vb, thought I was about done with the app then I found this;

    http://ss64.com/nt/cmd.html

    its letting me split my really long batch into small batches that are task specific that run from the one window so should a task error out the main "menu" window is always there, got the batch unpacking a rar/exe silently to the temp dir but can the cmd do anything other than call an app?
     
  9. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    what's the additional script to force this;

    to stay as the default action for the current session of Windows, so no matter what batch is called after that cmd window has closed the 32/64 setting will remain?

    Also personalising the .cmd shell was pointless as there are limited options so I got a custom portable one that I want to set as the default cmd.exe for all .cmd actions for that session too, I'm assuming that a script similar to the above would work?, the custom portable is 32bit only so am I going to have issues running a 32 cmd.exe on 64bit to copy/rename/delete/replace files and reg keys?
     
  10. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    #30 Alphawaves, Feb 25, 2011
    Last edited by a moderator: Apr 20, 2017
    Yeh you can call other batch files from first keeping the "set ???"

    Test.cmd:
    Code:
    @echo off
    
    IF '%processor_architecture%'=='x86' (
     Set File=32Test.txt
     Set Hstart=Hstart32.exe
    ) ELSE (
     Set File=64Test.txt
     Set Hstart=Hstart64.exe
    )
    
    CALL :START
    CALL NEXT.cmd
    CALL DONE.cmd
    "%~dp0%hstart%" /MSG="Complete." /TITLE="Patch by thethingy"
    exit
    
    :START
    "%~dp0%hstart%" /MSG="Patching in progress. This will take a minute, please wait." /TITLE="Patch by thethingy"
    IF EXIST "%programfiles%\blah\blah\blahl\Test.BACKUP" (
       TAKEOWN /F "%programfiles%\blah\blah\blahl\Test.BACKUP" >nul
       ICACLS "%programfiles%\blah\blah\blahl\Test.BACKUP" /GRANT *S-1-1-0:F >nul
       DEL /F /Q "%programfiles%\blah\blah\blahl\Test.BACKUP" >nul
    )
    
    IF EXIST "%programfiles%\blah\blah\blahl\Test.txt" (
       TAKEOWN /F "%programfiles%\blah\blah\blahl\Test.txt" >nul
       ICACLS "%programfiles%\blah\blah\blahl\Test.txt" /GRANT *S-1-1-0:F >nul
       REN "%programfiles%\blah\blah\blahl\Test.txt" "Test.BACKUP"
       COPY /Y "%~dp0%File%" "%programfiles%\blah\blah\blahl\Test.txt" >nul
       ping -n 7 127.0.0.1 >nul
     ) else (
       ping -n 7 127.0.0.1 >nul
       "%~dp0%hstart%" /MSG="Patching has failed." /TITLE="Patch by thethingy"
    )
    GOTO :eof 
    
    NEXT.cmd
    Code:
    "%~dp0%hstart%" /MSG="Called from first batch file." /TITLE="Patch by thethingy"
    GOTO :eof
    DONE.cmd
    Code:
    "%~dp0%hstart%" /MSG="Called from second batch file." /TITLE="Patch by thethingy"
    GOTO :eof
    You can run a 32Bit cmd from 64Bit windows as follows:

    Code:
    %windir%\SysWoW64\cmd.exe
    How to Open and Run 32-bit Command Prompt in 64-bit (x64) Windows
     
  11. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #31 thethingy, Feb 26, 2011
    Last edited by a moderator: Apr 20, 2017
    (OP)
    ^^^thanks, so now I want to take full control of a reg key but I don't know the user name;

    Code:
     /subkeyreg HKEY_CURRENT_USER /grant=administrators=f /grant=system=f /grant=restricted=r /grant=YOURUSERNAME=f /setowner=administrators > 
    
    manually I would ask the user to edit the command but this is to be automated so what's the perimeters to change YOURUSERNAME to the user who happens to be logged in?
     
  12. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    422
    199
    10
    I think what you want is the %username% variable :)
     
  13. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #33 thethingy, Mar 1, 2011
    Last edited by a moderator: Apr 20, 2017
    (OP)
    ^^thanks, now I have several batches (to always keep the menu as the fake gui needs consistency) and url's and text files needed by my app but I got to thinking that I don't need to package them all as my main bat can make the dependency's for the main bat;

    Code:
    ECHO [InternetShortcut] >"%~dp0blah.url"
    ECHO URL="http://blahblah.blah.com" >> "%~dp0blah.url"
    ECHO  >"%~dp0blah.txt"Please leave this file in place, you will need it should you wish to restore your..... >> "%~dp0blah.txt"
    
    url's and texts work no problem but how can I modify the text creation cmd so that it sets the correct line spacing for eventually renaming the text to a cmd for use?, this I would like to do for xml files also so that config settings can be tailored for 32/64bit and if possible tailored to windows xp/vista/7 settings <- is that even possible?.
     
  14. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    #34 Alphawaves, Mar 1, 2011
    Last edited by a moderator: Apr 20, 2017
    I may have missunderstood you are you saying you want to create a batch file from cmd ?

    Code:
    echo @echo off >"%~dp0blah.bat"
    echo. >>"%~dp0blah.bat"
    echo MD "%~dp0Test" >>"%~dp0blah.bat"
    echo IF EXIST "%~dp0Test" ( >>"%~dp0blah.bat"
    echo echo Directory created >>"%~dp0blah.bat"
    echo ) >>"%~dp0blah.bat"
    echo. >>"%~dp0blah.bat"
    echo pause >>"%~dp0blah.bat"
    http://www.petri.co.il/quickly_create_txt_file_from_cmd.htm
     
  15. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    yes, but I can only get it to write text to one very long line, say this is the bat I want to create from the cmd;

    when added to the text file it all comes out on one line and the cmd then fails :confused: I've tried it by trying to make the cmd as a one line cmd by using the "echo &" but that fails also..........
     
  16. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    Are you using >> to goto next line:

    firstline >
    secondline >>
    thirdline >>
    fourth line >>

    and so on ?
     
  17. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    NO! :) , so would that work like this;

    ECHO >"%~dp0ai.txt"@echo off first line>>IF '%processor_architecture%'=='x86' secondline >>Set File=32Test.txt thirdline >>..........("%~dp0ai.txt"

    or;

    ECHO >"%~dp0ai.txt"@ first line>>echo off
    secondline >>IF '%processor_architecture%'=='x86'
    thirdline >>Set File=32Test.txt
    ("%~dp0ai.txt"

    ???, is the first line and second replaceable with 1 & 2?
     
  18. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    #38 Alphawaves, Mar 1, 2011
    Last edited by a moderator: Apr 20, 2017
    Like this:

    Code:
    echo @echo off >"%~dp0ai.txt" 
    echo. >>"%~dp0ai.txt" 
    echo IF '%processor_architecture%'=='x86' ( >>"%~dp0ai.txt" 
    echo Set File=32Test.txt >>"%~dp0ai.txt" 
    echo ) ELSE ( >>"%~dp0ai.txt" 
    echo Set File=64Test.txt >>"%~dp0ai.txt" 
    echo ) >>"%~dp0ai.txt" 
     
  19. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #39 thethingy, Mar 1, 2011
    Last edited by a moderator: Apr 20, 2017
    (OP)
    thanks, that made this;

    Code:
    @echo off  
      
    IF 'x86'=='x86' (  
    Set File=32Test.txt  
    ) ELSE (  
    Set File=64Test.txt  
    ) 
    
    the spacing works but anything in %% is ignored, tried "" the ignored parts but you just get the "" with the %% and text removed :), in fact its not ignored the value of the %pro arc% is replaced with X86
     
  20. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,181
    21,957
    210
    #40 Alphawaves, Mar 1, 2011
    Last edited by a moderator: Apr 20, 2017
    lol sorry change :

    Code:
    echo @echo off >"%~dp0ai.txt" 
    echo. >>"%~dp0ai.txt" 
    echo IF '%processor_architecture%'=='x86' ( >>"%~dp0ai.txt" 
    echo Set File=32Test.txt >>"%~dp0ai.txt" 
    echo ) ELSE ( >>"%~dp0ai.txt" 
    echo Set File=64Test.txt >>"%~dp0ai.txt" 
    echo ) >>"%~dp0ai.txt"
    to this:

    Code:
    echo @echo off >"%~dp0ai.txt" 
    echo. >>"%~dp0ai.txt" 
    echo IF '%%processor_architecture%%'=='x86' ( >>"%~dp0ai.txt" 
    echo Set File=32Test.txt >>"%~dp0ai.txt" 
    echo ) ELSE ( >>"%~dp0ai.txt" 
    echo Set File=64Test.txt >>"%~dp0ai.txt" 
    echo ) >>"%~dp0ai.txt"
    it need doubles %% for processor_architecture..