question about removable drive and a batch file

Discussion in 'Software' started by red death68, Sep 15, 2009.

  1. red death68

    red death68 Command Sergeant Major

    i have a usb drive with opera portable and a file i wrote in word doc that is in .rtf i want a batch file that will open the word document in wordpad (to lessen resources and it more convienent) and i want it to open the opera portable so i need a batch file that i can run off the drive to do this any and all help would be appreciated tyvm in advance for any help
     
  2. GermanOne

    GermanOne Guest

    Are drive and path allways the same or do you want to look for the files on all possible drives?
     
  3. red death68

    red death68 Command Sergeant Major

    no the program and file are on the same drive but differnt computers keep changing the disk letter
     
  4. GermanOne

    GermanOne Guest

    You could try this:
    Code:
    @echo off &setlocal
    :: write the pathes without drives:
    :: (for example in the next line instead of D:\somewere\yourFile.rtf)
    set path_1=somewere\yourFile.rtf
    set path_2=somewereElse\OperaUSB.exe
    
    set "DriveLetters=D E F G H I J K L M N O P Q R S T U V W X Y Z"
    
    :: loop each letter
    for %%i in (%DriveLetters%) do (
     :: find only mapped drives
     for /f "tokens=3" %%j in ('dir %%i:\ 2^>nul^|findstr "%%i:\\"') do (
      :: loop each mapped drive
      if defined found (goto process) else call :find %%j
     )
    )
    :process
    if not defined found goto :eof
    start "" /max "wordpad" "%found%%path_1%"
    start "" /max "%found%%path_2%"
    pause
    goto :eof
    
    :find
    if exist "%1%path_1%" set "found=%1"
    goto :eof
    
     
  5. red death68

    red death68 Command Sergeant Major

    well i actualy got it working first thing today but cant get rid of the cmd window for the browser. here is a copy of the code i used:

    i kept it simple but thats what i have
     
  6. GermanOne

    GermanOne Guest

    If you don't use START the batch file will wait untill the called application will be closed. Why don't you use my code? It's easy to substitute the pathes:
    Code:
    @echo off &setlocal
    set path_1=skcirt o gab\setis sptth.rtf
    set path_2=things that can wait\personal effects\browsers\opera portable\opera-portable-personal-en-9.64.exe
    set "DriveLetters=D E F G H I J K L M N O P Q R S T U V W X Y Z"
    
    for %%i in (%DriveLetters%) do (
     for /f "tokens=3" %%j in ('dir %%i:\ 2^>nul^|findstr "%%i:\\"') do (
      if defined found (goto process) else call :find %%j
     )
    )
    :process
    if not defined found goto :eof
    start "" /min "wordpad" "%found%%path_1%"
    start "" /max "%found%%path_2%"
    pause
    goto :eof
    
    :find
    if exist "%1%path_1%" set "found=%1"
    goto :eof
    
    Make sure that not more then drive letter, colon an backslash is truncated.

    PS: Sorry if I have written incorrectly. My nickname isn't a joke and my English is not the best.
     
  7. red death68

    red death68 Command Sergeant Major

    i hate to admit it but im a bit of a noob at batch files can you explain your code a bit for me so i know what may need to be changed
     
  8. GermanOne

    GermanOne Guest

    It's not easy for me because I'm a bit of noob in English ;)
    I'll try it anyway.
    Code:
    @echo off &setlocal
    
    The command line prompt is off and changes of the environment are restricted for this batch window.

    Code:
    set path_1=skcirt o gab\setis sptth.rtf
    
    Set a variable with the path to your *.rtf but without drive letter, colon and first backslash.

    Code:
    set path_2=things that can wait\personal effects\browsers\opera portable\opera-portable-personal-en-9.64.exe
    
    Just the same for the portable Opera browser.

    Code:
    set "DriveLetters=D E F G H I J K L M N O P Q R S T U V W X Y Z"
    
    Here we set a variable with all potential drive letters for removable drives. It's delimited by spaces, because space and tab are the standard delimiters for FOR-loops.

    Code:
    for %%i in (%DriveLetters%) do (
    
    The outer FOR-loop gets step by step each letter from variable %DriveLetters% and puts it in %%i.

    Code:
     for /f "tokens=3" %%j in ('dir %%i:\ 2^>nul^|findstr "%%i:\\"') do (
    
    Here we use the DIR-function to get the contents of each called drive. If we find "[drive letter]:\" on a defined position of the DIR standard output then we have [drive letter]:\ inside %%j.

    Code:
     
      if defined found (goto process) else call :find %%j
    
    Now check if the string "found" is defined as a variable. It is defined only if we've found the *.rtf file into the sub programm ":find".
    If is defined jump to label ": process" else call the subroutine ":find" using %%j as parameter.

    Code:
      
     )
    )
    
    Of cause we must close each opened parentheses (hope that's the right word?!)

    Code:
    :process
    
    The colon marks a jump label or a subroutine label. In this case it's the label we jump from the last FOR-loop.

    Code:
    if not defined found goto :eof
    
    If nowhere found your *.rtf file on this point the variable isn't defined. In this case jump to end of file (to close the batch file).
    Code:
    start "" /min "wordpad" "%found%%path_1%"
    
    Otherwise open wordpad with your *.rtf (in %found% we have got [drive letter]:\) ...

    Code:
    start "" /max "%found%%path_2%"
    
    ... and the same with Opera.

    Code:
    pause
    
    Hope that doesn't need any comment.

    Code:
    goto :eof
    
    Here we jump to end of file because thats the end of the "main code".


    Code:
    :find
    
    Here starts the subroutine called from the last loop in the main code.

    Code:
    if exist "%1%path_1%" set "found=%1"
    
    In %1 we find the parameter %%j. That means [drive letter]:\. What we do is checking the existence of your *.rtf file with each given drive letter. If we found it set the variable "found" with the value [drive letter]:\.

    Code:
    goto :eof
    
    In this case it marks the end of the subroutine and we jump back into the last FOR-loop of main code.

    Now I hope its a bit clearer because it's the best way I can explain you.
     
  9. red death68

    red death68 Command Sergeant Major

    ya it is all the pecents for variables kinda confused me for a minute so does that mean just past into notepad and save as .bat and im all set?

    and tyvm for all the help and fyi your english is about as good as mine and iv lived in usa my whole life so your doing great
     
  10. GermanOne

    GermanOne Guest

    Yes, of cause. That's what you should try to do.
     
  11. red death68

    red death68 Command Sergeant Major

    the code just opens a blank cmd window unless i missed somthing but i tryed copy and paste then save got just cmd window empty retried it ....same thing
     
    Last edited: Sep 18, 2009
  12. GermanOne

    GermanOne Guest

    Copy&paste the code of my 3rd post and check if the pathes are correctly.
     
  13. red death68

    red death68 Command Sergeant Major

    ok it worked it was just taking a while
     
  14. GermanOne

    GermanOne Guest

    In this case your usb is really a verry slow one rolleyes
     
  15. red death68

    red death68 Command Sergeant Major

    well that and the computer is old rofl but part of it might be the time it takes to check all of the possible drive letters or is that faster then im guessing?
     
  16. GermanOne

    GermanOne Guest

    This batch stops looping if the right drive letter was found.
    But maybe you are right. I wrote it to start it from the local drive (C:\). If the batch is located on your usb it would be much more short and quick using this code:
    Code:
    @echo off
    start "" /min "%~d0\skcirt o gab\setis sptth.rtf"
    start "" /max "%~d0\things that can wait\personal effects\browsers\opera portable\opera-portable-personal-en-9.64.exe"
    
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds