Jump to content

script-expanding variable to drive letter


Recommended Posts

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 by patronu
Link to comment
Share on other sites


Maybe you want the first (or first two) characters of the contents of variable %software_path%, like:

SET Path_letter=%software_path:~0,1%

or

SET Path_letter=%software_path:~0,2%

Or:

ECHO %software_path:~0,1%

etc...

;)

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

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

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 :D Maybe jaclaz can help.

for /f "tokens=2* delims=:" %%a in ('ipconfig /all ^| find "DHCP Server"') do set dhcpserver=%%a

set 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

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 things

  1. Why don't you want to capture all of the non-whitespace characters?
  2. How do you know that the DHCP Server will contain that specific number of characters?

Link to comment
Share on other sites

for /f "tokens=2* delims=:" %%a in ('ipconfig /all ^| find "DHCP Server"') do set dhcpserver=%%a

set 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. :unsure:

Why 12 characters?

Isn't the line that you get something like:

DHCP Server . . . . . . . . : 255.255.255.255

or also (example):

DHCP Server . . . . . . . . : 10.2.7.9

I.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

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 off
For /F "tokens=14 delims=cdehprsv: " %%# In (
'IPConfig /all^|Find "DHCP Server"') Do Set "_=%%#"
Echo=net use x: \\%_%\test

Make sure that the delims ends with <colon><tab><space>.

Edited by Yzöwl
Link to comment
Share on other sites

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öwl

what 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 by patronu
Link to comment
Share on other sites

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=%%a

Change the 12 to the number of actual SPACES.

jaclaz

Link to comment
Share on other sites

@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 EnableExtensions
For /F "tokens=2 delims=:" %%# In ('IPConfig /all^|Find "DHCP Server"') Do (
Call :Addy %%#)
Pause & Goto :Eof
:Addy
Set DHCPServer=%1
Echo=[%DHCPServer%]

@Echo off & SetLocal EnableExtensions
For /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

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=%%a

and 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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...