[Batch] root

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

  1. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #1 thethingy, Feb 21, 2011
    Last edited by a moderator: Apr 20, 2017
    Code:
    echo.
    FOR /F "tokens=*" %%D IN ('DIR "C:\Program Files\thefilel" /S /B') DO copy /Y "thefile" "%%D"
    pause
    
    I want this to auto check that "c" is the root and if its not put the file in say D or whatever.
     
  2. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #2 CODYQX4, Feb 21, 2011
    Last edited: Apr 15, 2019
    .
     
  3. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    Thanks man
     
  4. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    still stuck;

    all I want it to do is check in the root drive for a file and if its not there then pop up "fail" and if it is there replace it, so far it always "fails" even though the file is in the directory.

    Ive tried it with %systemroot\Program Files\blah\blah\% >nul and %systemroot%\Program Files\blah\blah\% >nul
     
  5. searchengine

    searchengine Guest

    %systemdrive% (not %systemroot%) variable equals whatever drive letter the running OS is on.

    make sure "%systemdrive%\Program Files\blah\blah\blahl" on script
     
  6. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    thanks people, but I can get it to work either way, before I pull any more hair out what my plan was is to create a .cmd that checks for a file then if it is there replace it with a file of the same name, that I know can be done but can I wrap the .cmd with the file to be copied in to an exe with Bat_To_Exe_Converter and the cmd when launched as exe will still be able to access the file to be copied from within the exe?
     
  7. searchengine

    searchengine Guest

    #7 searchengine, Feb 21, 2011
    Last edited by a moderator: Apr 20, 2017
    Bat_To_Exe_Converter presumably allows you to insert the "replacement" file beside the ".cmd" file, so upon extraction it should extract both files to the same temp folder, thus file should copy without problem. (The "cmd when launched as exe" is not really launched as exe ... it is just extracted to temp folder).

    personally, I think bat to exe software is a waste of time, as I think they just extract files to users temp folder, the same as a Winrar sfx installer can do, only many trigger av alarms.

    EXAMPLE
    Code:
    @echo off
    
    if exist "%systemdrive%\Program Files\blah\blah\blahl\Test.txt" (
       takeown /F "%systemdrive%\Program Files\blah\blah\blahl\Test.txt" >nul
       icacls "%systemdrive%\Program Files\blah\blah\blahl\Test.txt" /GRANT *S-1-1-0:F >nul
       del /F /Q "%systemdrive%\Program Files\blah\blah\blahl\Test.txt" >nul
       copy /Y "%~dp0Test.txt" "%systemdrive%\Program Files\blah\blah\blahl\" >nul
       echo.
       echo Successfully Patched
       ping -n 7 127.0.0.1 >nul
       goto :end
    ) else (
    echo.
    echo Failed to Find File to Patch
    ping -n 7 127.0.0.1 >nul
    )
    :end
    exit
    change Test.txt to name of file you wish to check|replace.
     
  8. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    ^^Well that just closes the cmd window (without coping) and I cant figure out why, nevertheless I think a better approach would be to 1) check for the file 2) if present rename to "backup" if not there then display message saying so 3) copy replacement file to the directory then display conformation message 4) from a new menu delete the copied file and rename backup to original,

    anyone????????
     
  9. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,223
    22,281
    210
    It does work, did you make sure the file Test.txt was in the correct locations for testing this ?
     
  10. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    yeh, wondering if its to do with where the copied file is?, its in a folder with the cmd, the window just closes with no done/not done msg..................
     
  11. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,223
    22,281
    210
    Have you got the file Test.txt in program files\blah\blah\bahl folder, and also alongside the batch file your running ?
     
  12. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    yeh, tried it with the file removed from the destination as it should just do nothing in that instance and tried it with the file in the destination, both times (well multiple times) the file to be copied was in the cmd folder, its strange as there is no error at all just an immediate close, with the other parts of the app I got "file" errors and echo errors till it was debugged.
     
  13. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,223
    22,281
    210
    #13 Alphawaves, Feb 21, 2011
    Last edited by a moderator: Apr 20, 2017
    How about using %programfiles% instead of %systemdrive%\program files

    ie:

    Code:
    @echo off
    
    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
       del /F /Q "%programfiles%\blah\blah\blahl\Test.txt" >nul
       copy /Y "%~dp0Test.txt" "%programfiles%\blah\blah\blahl\" >nul
       echo.
       echo Successfully Patched
       ping -n 7 127.0.0.1 >nul
       goto :end
    ) else (
    echo.
    echo Failed to Find File to Patch
    ping -n 7 127.0.0.1 >nul
    )
    :end
    exit
     
  14. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    ^^nop, just shuts down with no explanation............................
     
  15. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,223
    22,281
    210
    Try it without the >nul and replace exit with pause, to help see where its failing..
     
  16. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    #16 thethingy, Feb 21, 2011
    Last edited: Feb 21, 2011
    (OP)
    Still just dies, tried first removing all >nul's then with just 1 >nul at a time, tried the script as is and as a :thing in my app.........

    edit, put a @color 90 under the echo and I get a flash of blue now but nothing else..............
     
  17. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,223
    22,281
    210
    Do you have problems running other cmd files or is it this only ?
     
  18. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10

    just this one, the app that I want this for already has a few things in it, this is the last part but I need to duplicate this about 40 times for the various dll files and locations.....................
     
  19. searchengine

    searchengine Guest

    works fine for me ....

    Click Image to see Test of Example code
    (when at web page, click "View full screen")

    [​IMG]

    are you adding code to an "existing" script with improper instruction, causing no message ?

    ie. "FSUTIL /i "blah" %systemroot\Program Files\blah\blah\% >nul" post #4
     
  20. thethingy

    thethingy MDL Senior Member

    Sep 7, 2010
    301
    41
    10
    I am adding it as a choice to do from a previous menu;

    :blah
    CLS
    if exist "%program.........

    so picking "A. blah" from a sub menu gives the copy over action.