| 1. |
Solve : String Manipulation Batch? |
|
Answer» I am making a batch file where the user needs to drag a file into the command window. Both the filename and the path need to be stored in two separate variables (when the file is dragged into the window, the full path and filename is retrieved and stored in one variable). I found soooo many examples of this on google EXCEPT they all assumed that you would be using %1 and for me it's %arpath%. I can't figure out how to make the thing work with a variable instead of %1. So the answer is there is no answer You waited under 9 minutes before making this petulant bump. This is not really acceptable. I wish now that I had waited 12 hours to tell you about FOR. Quote This is not really acceptable. I wish now that I had waited 12 hours to tell you about FOR. I apologize. I waited so little time after that first response because the way he said it, it sounded like there was no solution and I thought if I left it that way everyone would just read his post and think "up. not possible. moving on." But you didn't wait until 12 hours, so either you answered it anyway (in which case I appreciate it) or you just found out that it had been 9 minutes now.My bad anyways. The Majority of my batch files that we use here at work are drag and drop. The ones that are drag and drop the user drags and drops all the files they need to process onto the batch file. Everyone in my department knows that is how they work. So if you really wanted to do it that way you could just inform your users that all they need to do is drag a file or folder on top of the batch file and that will launch the batch file as well. Otherwise your users are doing 3 steps just to get the file path into the batch file. 1) They double click to launch the batch file 2) They drag and drop the file into the cmd window 3) They then need to hit enter for the SET /P to take the input. If you just have your batch file accept the %1 as the input all they have to do is 1) drag the file onto the batch file What I usually do at the top of all my scripts is check to see if %1 is not defined. If it isn't I GOTO a label at the bottom of my script called USAGE. It explains to the user that they MUST drag and drop the files they want to process onto the batch file. You could in theory have your batch file setup both ways. You could do an IF statement after your first QUESTION to see if %1 is defined and if it is SET it to your Path variable and GOTO a label after after your FOR LOOP. Quote from: Squashman on February 22, 2012, 05:11:05 AM What I usually do at the top of all my scripts is check to see if %1 is not defined. Is the coding for this simply if defined %1? I'm having some issues with a called script and I'm trying to break it down to it's basic parts and troubleshoot individually, but still running into snags. Also, when a script is called from cmd line or batch, does it see something in QUOTES as one variable to be passed? I.e. program.exe 2 3 "Hello all" would pass three variables to the program, not 4? I'm pretty sure this is the case, but looking to verify it before troubleshooting further. Quote from: Raven19528 on February 22, 2012, 12:44:48 PM Is the coding for this simply if defined %1? if [not] defined only works with regular %var% type variables (those which normally have a percent sign before and after) not the single-percent sign-and-number replaceable parameters, so you'd have to do this (notice there aren't any percent signs surrounding the variable name after the defined keyword) Code: [Select]set var=%1 if defined var echo YES if not defined var echo NO Code: [Select]set var=%1 if defined var ( echo YES ) else ( echo NO ) Or you can work directly with %1 to %9 Code: [Select]if not "%1"=="" echo YES if "%1"=="" echo NO Code: [Select]if not "%1"=="" ( echo YES ) else ( echo NO ) Moving on... Quote program.exe 2 3 "Hello all" would pass three variables to the program, not 4? test.bat Code: [Select]echo off echo parameter 1 is [%1] parameter 2 is [%2] parameter 3 is [%3] parameter 4 is [%4] test.bat 2 3 "Hello all" See the result... I shouldn't have said defined. I just do a comparison with an IF statement. IF "%1"=="" goto usage |
|