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

Author Topic: % % for USB Thumb Drive for correct path  (Read 7859 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
% % for USB Thumb Drive for correct path
« on: March 13, 2008, 12:27:34 PM »
Trying to figure out the correct % % so that a batch program will run correct on any mounted drive letter for the thumb device. Currently I am using Autorun to trigger the batch with thumb device, but was wondering the % % call like %root% which will pick up the correct path of the thumb drive when the batch is started from the thumb drive manually, the batch changes its focus on to the C: drive, then runs a routine at the end on the thumb drive where if you dont know the drive letter mapped for the thumb drive, it will not work.

Thanks,

Dave

Dias de verano

  • Guest
Re: % % for USB Thumb Drive for correct path
« Reply #1 on: March 13, 2008, 01:57:12 PM »
for any batch file, %0 is its own name. Using the standard variable modifiers, you can extract the various elements: drive, path, name.

I have a pen drive which is assigned letter E, and here is a batch file I have placed in a folder \batch\ on that drive.

Notice how the first %0 name is different when you run it from a different directory. However the drive and path stay the same.

Code: [Select]
@echo off
echo My filename is  %0
echo My full path is %~dpnx0
echo I am in folder  %~dp0
echo My drive is     %~d0

set dletter=%~d0
set bareletter=%dletter:~0,1%

echo My drive letter %bareletter%

output:

Code: [Select]
C:\>E:\batch\self-ID.bat
My filename is  E:\batch\self-ID.bat
My full path is E:\batch\self-ID.bat
I am in folder  E:\batch\
My drive is     E:
My drive letter E


E:\batch>self-ID.bat
My filename is  self-ID.bat
My full path is E:\batch\self-ID.bat
I am in folder  E:\batch\
My drive is     E:
My drive letter E

And if I double click on it in Explorer

Code: [Select]
My filename is  "E:\batch\self-ID.bat"
My full path is E:\batch\self-ID.bat
I am in folder  E:\batch\
My drive is     E:
My drive letter E



llmeyer1000



    Intermediate

    Thanked: 1
    Re: % % for USB Thumb Drive for correct path
    « Reply #2 on: March 13, 2008, 11:14:08 PM »
    Awesome!
    Where do you find the "standard variable modifiers" you demonstrated listed & explained?

    Dias de verano

    • Guest
    Re: % % for USB Thumb Drive for correct path
    « Reply #3 on: March 14, 2008, 01:29:07 AM »
    Awesome!
    Where do you find the "standard variable modifiers" you demonstrated listed & explained?


    They are included in the help for FOR so type FOR /? at the prompt but I will copy them here. They work not just on FOR loop variables but the batch variables %0 (own name) and %1 to %9 (passed parameters).

    note that the variable %I used is just an example

    Code: [Select]

        %~I         - expands %I removing any surrounding quotes (")
        %~fI        - expands %I to a fully qualified path name
        %~dI        - expands %I to a drive letter only
        %~pI        - expands %I to a path only
        %~nI        - expands %I to a file name only
        %~xI        - expands %I to a file extension only
        %~sI        - expanded path contains short names only
        %~aI        - expands %I to file attributes of file
        %~tI        - expands %I to date/time of file
        %~zI        - expands %I to size of file
        %~$PATH:I   - searches the directories listed in the PATH
                       environment variable and expands %I to the
                       fully qualified name of the first one found.
                       If the environment variable name is not
                       defined or the file is not found by the
                       search, then this modifier expands to the
                       empty string

    The modifiers can be combined to get compound results:

        %~dpI       - expands %I to a drive letter and path only
        %~nxI       - expands %I to a file name and extension only
        %~fsI       - expands %I to a full path name with short names only
        %~dp$PATH:I - searches the directories listed in the PATH
                       environment variable for %I and expands to the
                       drive letter and path of the first one found.
        %~ftzaI     - expands %I to a DIR like output line



    llmeyer1000



      Intermediate

      Thanked: 1
      Re: % % for USB Thumb Drive for correct path
      « Reply #4 on: March 14, 2008, 06:21:21 AM »
      Dias de verano, using the variable modifiers as you demonstrated will no doubt work very handily in most cases. I am anxious to hear from DaveLembke to see if he got his script working. Please let us know Dave!


      I had a similar problem that requires a bit more complicated approach.  I'm not sure from DaveLembke's post whether he needs the straight forward approach you suggested, or if his situation is more like mine.

      I have a program that I want to run from the usb drive on several different machines. The problem I run into, occurs because the program I am running "remembers" numerous previous settings relating to drive & path locations. Since the lowest available drive letter is different on each machine, the program would get lost every time I switched machines. Then I had to change numerous settings each time. A real pain!

      It seemed like a good idea to find a way to search for the thumb drive and substitute the drive path with the letter Z:

      I have resolved the problem with a very long & cumbersome routine.  Although my routine works well, it is very long!!!

      I am hoping that one of you guys would have a simpler approach. I've been waiting for the right opportunity to ask, and right now seems right.

      Here is a portion of the routine, that I came up with, so you can see what I am doing. (In order for this to work, the folder being searched for must have a very unique name that won't be found accidentally anywhere else.)

      Code: [Select]

      :FIND_DRIVE
      :: Find your drive letter, then ...
      :: substitute the drive and path with "Z:"
      :: [Substitute the "User Program Files" folder(and sub-folders) to a virtual drive "Z:"]

      :IF_Z_DRIVE
      if not exist "Z:\User Program Files\Unique Folder\." goto IF_Y_DRIVE
      goto Z_DRV_ERROR

      :IF_Y_DRIVE
      if not exist "Y:\User Program Files\Unique Folder\." goto IF_X_DRIVE
      subst Z: "Y:\User Program Files"
      goto PROGRAM

      :IF_X_DRIVE
      if not exist "X:\User Program Files\Unique Folder\." goto IF_W_DRIVE
      subst Z: "X:\User Program Files"
      goto PROGRAM

      :IF_W_DRIVE
      ......
      ......
      ......

      :: NOTE: The four lines were repeated 21 more times to repeat the search for every drive letter through C:.

      :: All sections were similar except that the first and last section returns an error, rather than substituting a drive letter.

      :: In this example I wanted to see only the "User Program Files" folder in the virtual drive. If you want to see the entire drive, the subst line would be changed to:
      subst Z: "Y:\."   ...   subst Z: "X:\."   and so on to indicate the root of the usb drive.

      ......
      ......
      ......

      :IF_D_DRIVE
      if not exist "D:\User Program Files\Unique Folder\." goto IF_C_DRIVE
      subst Z: "D:\User Program Files"
      goto PROGRAM

      :IF_C_DRIVE
      if not exist "C:\User Program Files\Unique Folder\." goto NODRV_ERROR
      subst Z: "C:\User Program Files"
      goto PROGRAM


      By the way, this code does not require that the batch file be located on the usb drive to work. It runs the same located there as when run from a copy on the desktop.

      If you want to see the whole thing, error messages and all, I could either post it or attach it as a text file. But...

      what I am hoping instead is that one of you guys will have a simpler approach.
      « Last Edit: March 15, 2008, 10:34:16 AM by llmeyer1000 »

      DaveLembke

        Topic Starter


        Sage
      • Thanked: 662
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: % % for USB Thumb Drive for correct path
      « Reply #5 on: March 14, 2008, 11:05:07 AM »
      Hello... Thanks for the posted fix ( proper way to accomplish this path issue )...This will work very well for my batch. Unfortunately I am at work and my batch is at home, and I will try to remember to post it over the weekend, so I can show what I am doing to help others who need to do similar routines, who can copy/paste and edit my batch to use as well.

      And  in regards to the last post of "I have a program that I want to run from the usb drive on several different machines. The problem I run into, occurs because the program I am running "remembers" numerous previous settings relating to drive & path locations. Since the lowest available drive letter is different on each machine, the program would get lost every time I switched machines. Then I had to change numerous settings each time. A real pain!"

      This is exactly the problem I was having. With various systems and one system might mount it as G: and the other F: etc, I tried a band aid approach of finding the route back to the thumb drive by IF EXIST routines for D: thru Z: to find the thumb drives contents to pick back up from, but this was extremely hokey, and the fix that was posted by Dias de verano, which is the better way to accomplish getting back to the source thumb drive will work perfect!!!

      THANKS SO MUCH Dias de verano

      Dave