Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Writing a bat file to replace a shortcut on a flash drive  (Read 5597 times)

0 Members and 1 Guest are viewing this topic.

mike1980

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Windows 8
    Writing a bat file to replace a shortcut on a flash drive
    « on: April 02, 2015, 06:55:54 AM »
    Hello,

    I have zero experience writing bat files, but was told on the hope chat to come here and ask as it was the way to do it.

    I can make a shortcut on a flashdrive pointing to a local .exe file on the same drive. However, the shortcut breaks if the drive letter changes when it's plugged into a different machine.

    Any help would be appreciated.

    THanks

    strollin



      Adviser
    • Thanked: 84
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Guru
    • OS: Windows 10
    Re: Writing a bat file to replace a shortcut on a flash drive
    « Reply #1 on: April 02, 2015, 08:28:10 AM »
    You say the exe is on the flashdrive?  If so, you may only need to eliminate the drive letter from the line that invokes the exe.

    Can you post the bat file?

    mike1980

      Topic Starter


      Starter

      • Experience: Experienced
      • OS: Windows 8
      Re: Writing a bat file to replace a shortcut on a flash drive
      « Reply #2 on: April 02, 2015, 10:52:57 AM »
      I don't have a bat and wouldn't have the faintest idea how to write one. A loooong way from my area of expertise!

       I asked in the live chat how I could create a shortcut for an exe on a flashdrive where the drive letter can (and does) change when it's used on different PCs.

      Someone said to use a bat file. I couldn't write one.



      Thanks

      Salmon Trout

      • Guest
      Re: Writing a bat file to replace a shortcut on a flash drive
      « Reply #3 on: April 02, 2015, 01:39:11 PM »
      How is the exe file being started? Why can't the user just double click on the exe?


      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Writing a bat file to replace a shortcut on a flash drive
      « Reply #4 on: April 02, 2015, 02:22:36 PM »
      Perhaps this is what the Original Poster wants.

      He has a .EXE file on a flash drive that he wants to run other PCs in his group. He would like a shortcut or batch file that would include the best set of parameters for the .EXE file.  And shorten the name of the shortcut or batch.

      The problem is that the drive letter changes when the drive is moved to another PC.
      Poor example:
      try.bat is:
      F:\tryprogram.exe  -a -b -c
      So he puts try.bat on F:\  and he then takes it to another computer whee the drive letter for the flash drive i G:  and the program fails to start.
      Does this describe the problem?  :)


      strollin



        Adviser
      • Thanked: 84
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Guru
      • OS: Windows 10
      Re: Writing a bat file to replace a shortcut on a flash drive
      « Reply #5 on: April 02, 2015, 03:45:20 PM »
      If the exe is on the flashdrive then the invocation doesn't need to include the drive letter.

      try.bat:
      tryprogram.exe -a -b -c

      This would work if try.bat and tryprogram.exe are both in the same folder on the flashdrive and the user invokes try.bat from that folder.  If try.bat was in the root folder of the flashdrive and tryprogram.exe was in a folder called tryme, then try.bat would look like this:

      \tryme\tryprogram.exe -a -b -c

      The problem would come into play if try.bat is on the user's C: drive and was trying to invoke tryprogram on the flashdrive, in that case the drive letter would be required.

      mike1980

        Topic Starter


        Starter

        • Experience: Experienced
        • OS: Windows 8
        Re: Writing a bat file to replace a shortcut on a flash drive
        « Reply #6 on: April 02, 2015, 06:40:17 PM »
        Thanks for all replies.

        Salmon Trout - when I plug my USB in, I  want to be able to run 6 programs from my USB. Each exe is several folders deep - that's a lot of clicking.

        I right clicked > send to > desktop (create shortcut).

        That's fine until the drive letter changes.



        Yes, that describes my problem Geek-9pm.


        Strollin - so the code should be (for one of the programs)

        \mikejava\109\IDE\eclipse\eclipse.exe -a -b -c

        Thank you




        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Writing a bat file to replace a shortcut on a flash drive
        « Reply #7 on: April 02, 2015, 07:12:15 PM »
        Rather than send a shortcut to the desktop, one must create a shortcut or batch on the target drive.
        The the user must navigate to the target drive and start the batch or shortcut.
        Otherwise, this is, to my knowledge, no DOS thing that finds a drive letter for you.
        If thee was, the pseudo code would be:
        Code: [Select]
        For N=1 to 26
         Type=FindDrive(n)
          IF Type =="USB" set Gotdit = N
        That is not real code.
        It kinda represent the idea if there was a FindDrive thing is DOS.

        PowewrShell has something like that.
        Code: [Select]
        GET-WMIOBJECT –query “SELECT * from win32_logicaldisk where DriveType = ‘3’” Also in C++ there is a thing. Somewhere.

        strollin



          Adviser
        • Thanked: 84
          • Yes
        • Certifications: List
        • Computer: Specs
        • Experience: Guru
        • OS: Windows 10
        Re: Writing a bat file to replace a shortcut on a flash drive
        « Reply #8 on: April 02, 2015, 10:48:37 PM »
        Thanks for all replies.  ...

        Strollin - so the code should be (for one of the programs)

        \mikejava\109\IDE\eclipse\eclipse.exe -a -b -c

        Thank you
        Yes, pretty much.  If you put the batch file in the root of the flashdrive then change to the flashdrive before inoking the batch file.

        Salmon Trout

        • Guest
        Re: Writing a bat file to replace a shortcut on a flash drive
        « Reply #9 on: April 03, 2015, 01:24:03 AM »
        A batch file knows its own drive letter, it is given by %~d0 which expands to a letter and colon thus D: E: F: or whatever. A batch file in the root of a removable drive could have a line pointing to an exe anywhere else on that drive for example "%~d0\path\to\program file\program.exe" (quotes needed if path has spaces, does no harm to always use therm). If you wanted this program to run when a flash drive is inserted you are getting into autorun territory and will run into security implications.
        « Last Edit: April 03, 2015, 01:36:50 AM by Salmon Trout »

        strollin



          Adviser
        • Thanked: 84
          • Yes
        • Certifications: List
        • Computer: Specs
        • Experience: Guru
        • OS: Windows 10
        Re: Writing a bat file to replace a shortcut on a flash drive
        « Reply #10 on: April 03, 2015, 09:25:15 AM »
        That's a good tip Salmonthe drive letter is not necessary as long as the batch file and the exe are on the same drive but it certainly doesn't hurt to include it.