| 1. |
Solve : % % for USB Thumb Drive for correct path? |
|
Answer» 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. Awesome! 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 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. 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 |
|