patronu Posted July 24, 2010 Share Posted July 24, 2010 (edited) I have a script for unattended setup and I have a problem. I'm using for command to find the path for software folder and used a variable for this. But, later, in the same script I need just the drive letter. how can I do this without using another for command and to use the same variable?I'm trying to find something like %software_path:~d% but I don't know the correct syntax. can anyone help me?for %%i IN (C D E F G H I J K L M N O P Q R S T U V W) DO if exist %%i:\install_usb.txt set software_path=%%i:\software\)rem the echo shoud return just the drive letter using the above variable. but how?echo %software_path:~d% Edited July 24, 2010 by patronu Link to comment Share on other sites More sharing options...
jaclaz Posted July 24, 2010 Share Posted July 24, 2010 (edited) Maybe you want the first (or first two) characters of the contents of variable %software_path%, like:SET Path_letter=%software_path:~0,1%orSET Path_letter=%software_path:~0,2%Or:ECHO %software_path:~0,1%etc...jaclaz Edited July 24, 2010 by jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted July 25, 2010 Share Posted July 25, 2010 You've already set up the environment you need, just set the additional varaible within it!FOR %%# IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO ( IF EXIST %%#:\install_usb.txt ( SET drive_path=%%#:\ SET software_path=%%#:\software\)) Link to comment Share on other sites More sharing options...
patronu Posted July 25, 2010 Author Share Posted July 25, 2010 thank you. both solutions work. but I didn't know the syntax that jaclaz wrote. the other solutions with 2 variables I already use it but I want a way to use just one variable.Also I have a similar problem with something similar Maybe jaclaz can help.for /f "tokens=2* delims=:" %%a in ('ipconfig /all ^| find "DHCP Server"') do set dhcpserver=%%aset dhcpserver=%dhcpserver:~1,12%how to write this on single line? and without using & operator (something to make %%a to use just the first 12 characters) Link to comment Share on other sites More sharing options...
Yzöwl Posted July 25, 2010 Share Posted July 25, 2010 thank you. both solutions work. but I didn't know the syntax that jaclaz wrote. the other solutions with 2 variables I already use it but I want a way to use just one variable.It still isn't necessary to use such variable expansion techniques, just do it the other way around!FOR %%# IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO ( IF EXIST %%#:\install_usb.txt SET drive_path=%%#:\)ECHO=software_path=%drive_path%software\For your additional question, let me just ask two thingsWhy don't you want to capture all of the non-whitespace characters?How do you know that the DHCP Server will contain that specific number of characters? Link to comment Share on other sites More sharing options...
jaclaz Posted July 25, 2010 Share Posted July 25, 2010 for /f "tokens=2* delims=:" %%a in ('ipconfig /all ^| find "DHCP Server"') do set dhcpserver=%%aset dhcpserver=%dhcpserver:~1,12%how to write this on single line? and without using & operator (something to make %%a to use just the first 12 characters)I don't get it. Why 12 characters?Isn't the line that you get something like:DHCP Server . . . . . . . . : 255.255.255.255or also (example):DHCP Server . . . . . . . . : 10.2.7.9I.e. an IP address can be anything between 7 (4x1 number+ 3 dots) and 15 (4x3 numbers + 3 dots).Copy and paste the output of IPCONFIG /ALL | FIND "DHCP Server"and the actual thing that you want %dhcpserver% value to be....jaclaz Link to comment Share on other sites More sharing options...
patronu Posted July 25, 2010 Author Share Posted July 25, 2010 15 not 12 my mistake. Link to comment Share on other sites More sharing options...
jaclaz Posted July 25, 2010 Share Posted July 25, 2010 15 not 12 my mistake.Still it makes no sense to me.The output of the FOR loop you posted will get in %%a everything to the right of the colon so what is the problem?jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted July 25, 2010 Share Posted July 25, 2010 (edited) Still you're not looking at the problem correctly, as stated by jaclaz and myself, the assigned dynamic address can have a differing number of characters.You can of course try something different with your for loop if you are trying to capture all the numbers and dots which make up the assigned address but without any whitespace.Try this:@Echo offFor /F "tokens=14 delims=cdehprsv: " %%# In ( 'IPConfig /all^|Find "DHCP Server"') Do Set "_=%%#"Echo=net use x: \\%_%\testMake sure that the delims ends with <colon><tab><space>. Edited July 25, 2010 by Yzöwl Link to comment Share on other sites More sharing options...
patronu Posted July 25, 2010 Author Share Posted July 25, 2010 (edited) it's because the for loop gets that ip address with a space in front of it and I want to use that ip address with net use and I get something like net use x: \\ 192.168.1.1\test which gives an error.@Yzöwlwhat is the purpose of delims=abcdefghijklmnopqrstuvwxyz: ?I removed the [] from @Echo=[%#] because I get ] in front of the ip address. It seems that it works now.thank you. Edited July 25, 2010 by patronu Link to comment Share on other sites More sharing options...
Yzöwl Posted July 25, 2010 Share Posted July 25, 2010 It seems like a problem caused by the OS I used, I've narrowed it down to be more specific and ran it on a Win2k system.Please see my previous edited post! Link to comment Share on other sites More sharing options...
jaclaz Posted July 25, 2010 Share Posted July 25, 2010 Also, you can simply COUNT the spaces (including the one after the colon) and use SPACE as a delimiter.Something like:for /f "tokens=12 delims= " %%a in ('ipconfig /all ^| find "DHCP Server"') do set dhcpserver=%%aChange the 12 to the number of actual SPACES.jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted July 26, 2010 Share Posted July 26, 2010 @jaclaz I think that number would be 15@patronu, I would suggest that you forget about trying to make short or single line scripts and try one of these ideas:@Echo off & SetLocal EnableExtensionsFor /F "tokens=2 delims=:" %%# In ('IPConfig /all^|Find "DHCP Server"') Do ( Call :Addy %%#)Pause & Goto :Eof:AddySet DHCPServer=%1Echo=[%DHCPServer%]@Echo off & SetLocal EnableExtensionsFor /F "tokens=2 delims=:" %%# In ('IPConfig /all^|Find "DHCP Server"') Do ( For %%$ In (%%#) Do Set DHCPServer=%%$)Echo=[%DHCPServer%]Pause Link to comment Share on other sites More sharing options...
patronu Posted July 26, 2010 Author Share Posted July 26, 2010 Thank for your support.I found something that works for /f "tokens=15 delims=abcdefghijklmnopqrstuvwxyz: " %%a in ('ipconfig /all^|find "DHCP Server"') do set dhcpserver=%%aand also I like short single line scripts. the scrip looks more easy to read and it's simple that way. because I have many lines in it.right now I have more than 1600 lines. previously there were almost 2500 lines Link to comment Share on other sites More sharing options...
Yzöwl Posted July 26, 2010 Share Posted July 26, 2010 Just as long as you are aware that shorter scripts are not necessarily quicker/less resource intensive.BTW on Win2k, XP and Vista, in a console window the code you've chosen, which can be shortened as I had earlier, pre-edit, to delims=cdehprsv: ", uses "tokens=14, not 15. The results are not guaranteed to work in a manner I'd accept as safe for general use since there are 'hidden' characters in the ipconfig /all output. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now