| 1. |
Solve : For Loop Token - How to use as variable? |
|
Answer» Hello, If I remember correctly, I think you need SetLocal enabledelayedexpansion on the first line. Then you access the vars with !G! instead of %%G or something. I haven't used batch in a while so I may be wrong.You'd still use %%G with delayed expansion enabled, but you'd use !newname! instead of %newname%.Thanks Linux711 and Helpmeh, though still need more help. I added the setlocal enabledelayedexpansion and changed % to !. Now the echo commands show the variable name, not it's value. I've tried this with many variation, but no luck. I finally tried to save the prefix and suffix to individual variables and then concatenate them. Still no go. Would you have any other ideas on where my error is? I've searched looking for clues but have found none. Thanks a lot. Diane CURRENT CODE: set outputfile=PharmElog.csv set count=pharme if exist %outputfile% del %outputfile% setlocal enabledelayedexpansion FOR /F "tokens=1,2,3 delims=," %%G IN (spectrumnoformat.txt) DO If "%count%"=="pharme" ( set prefix=%%G set suffix=%%H echo !prefix! echo !suffix! set NewName=!prefix! !suffix! echo !NewName! [regaining space - attachment deleted by admin]Why are you setting %count% equal to "pharme" and then testing if %count% is equal to "pharme"? And why are you doing that weird DO IF? And where is the final parenthesis? Quote Now the echo commands show the variable name, not it's value. It looks OK from here... you might see this better by putting ECHO OFF at the top of your file. Your screenshot shows Code: [SELECT]1 ".pdf" 1 ".pdf" These are the values of prefix, suffix, and newname when your echo commands run. Just because it shows "ECHO !prefix!" in the output doesn't make it incorrect, that's just the way delayed expansion works. I would however suggest removing the double quotes in your suffix, and removing the space in your NewName. The first is accomplished by Code: [Select]set suffix=%%~H And the second by simply removing the space Code: [Select]set NewName=!prefix!!suffix!SalmonTrout, I agree the count is weird. I just started working with DOS and really want to have a record count and test the value of the string %%G but didn't know how do it (at least yet). Would appreciate a tip on that. Sort of ditto for the For command. Going through forums that's method that was presented for reading a text file. Are there other methods? My previous DOS experience is just a few system commands, so this is proving to be a good learning experience. Thanks for your comments and would appreciate alternatives. DianeYou didn't answer my question. I don't see why you are setting x=y and then straight away checking if x=y? Did you think it might have changed? Quote from: DianeM on November 09, 2011, 06:32:47 PM I would also appreciate help understanding how to use the For loop tokens. I understand they are only "valid" in the loop itself but - are they always prefixed with two %% when using them, can they be assigned to variables? Check out this post for a good explanation from Salmon Trout on the use of tokens in a FOR loop. I don't understand exactly on the count either. Are you hoping to have numbered lines that you are going through and want to ensure that the lines match up correctly? I guess the whole thing is that if you have no immediate need for the count variable now, lets dismiss it and concentrate on exactly what you are needing help with. Then later we can help explain some other things as you get a better understanding how all of the commands work.I was looking for a way to read a .txt file and then complete several steps - download a file and write a log to excel. I understood the FOR statement allowed one command after the DO. In looking for a way to have multiple steps I found the current method of FOR DO IF. I couldn't figure out how to deal with the field (via my prior response on testing the value) so I put in something that would be "true" to enable me to test the following steps and come back to that later. Given how things have progressed, I don't need that IF statement and would prefer to just have multiple steps after the DO including a record count. Prior to adding the DO IF, I tried using the DO with a call to a label but could not get the %%G, etc. values to be present in the subroutine. Does that help explain? It's been a crooked learning path. DianeNow that we've figured that out, let's move on to solving this problem. Quickly, yes, the FOR loop can perform multiple commands, it does not require a DO IF to do so, only an open parenthesis at the end of the same line as the FOR command. If you think your subroutine will work, you can call the routine after setting some of the variables in the FOR loop. Example: Code: [Select]for /f "tokens=1,2* delims=," %%G in (spectrumnoformat.txt) do ( set prefix=%%G set suffix=%%H call :subroutine ) I would really only suggest a subroutine if you are trying to setup a loop within the FOR command. There are a few times this is legitimately the case (many times when errorchecking, for example) but in this case, if you think the subroutine will work, go ahead and use it. To add a record count, simply add set /a count+=1 somewhere in the for loop (be sure to set count to a number somewhere before the for loop starts though.) Please post if you are needing further explanation of things. Try to be as specific as you can, so that we can answer the correct question.Thanks a lot for clarifying the FOR statement. I'll probably end up simplifying a lot when I get a better understanding of how they actually work. Another gentleman pointed out I was missing an end bracket via email, so I'll spend some time going back over this routine to clean it up. I really appreciate your help and interest. I'll post back hopefully with news of success. Thanks to all DianeYes! The routine is now humming along. I had to put my concatention in quotes as NewName only contained the suffix - set "NewName=!prefix! !suffix!" Raven19528, I did not use a subroutine as it was not needed to accomplish the task. I also checked on the post you suggested by SalmonTrout but did not see any entries by him. Is there another entry? Thank you for getting me on the right track. Diane Quote from: DianeM on November 10, 2011, 06:18:36 PM I also checked on the post you suggested by SalmonTrout but did not see any entries by him. Is there another entry? I think he means Sidewinder. Quote from: Salmon Trout on November 10, 2011, 11:55:06 PM I think he means Sidewinder. I did. It was late. It was my Friday. Both your names start with "S". I'm too poor to pay attention. (I've got plenty of excuses... ) In any case, sorry for confusion, and sorry to Sidewinder and Salmon Trout for the mix up. |
|