Split string in batch file


I am new to using batch files so could someone please help me split a string i am getting from a file.<br /> I am using %USERPROFILE% to get my string.<br /> The string is: "C:\Users\nicholas"<br /> I would like to keep the C:\ part, but get rid of the \Users\nicholas part.


for /f "tokens=2 delims=\" %A in ('set userprofile') do echo %A\

See for /?


echo %userprofile:~0,3%


If you carefully read the output of set /?, you'll find the answer:


May also specify substrings for an expansion.


would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result.


So, you can use something like this to get the first 3 characters of your string:

> echo %userprofile:~0,3% C:\


<strong>I</strong> As you need the drive where where the users are located you can use directly %systemdrive% variable - this is the drive where the windows is installed

<strong>II</strong> the easiest way to get a drive from path:

for %%a in ("%userprofile%") do echo %%~da\

%~da - expands a path to its drive only

<strong>III</strong> over-complicated but powerful way (split function that can be used for a different things):

@echo off call :split "%userprofile%" "\" 1 drive echo %drive%\ goto :eof :split [%1 - string to be splitted;%2 - split by;%3 - possition to get; %4 - if defined will store the result in variable with same name] ::http://ss64.org/viewtopic.php?id=1687 setlocal EnableDelayedExpansion set "string=%~2%~1" set "splitter=%~2" set /a position=%~3 set LF=^ rem ** Two empty lines are required echo off for %%L in ("!LF!") DO ( for /f "delims=" %%R in ("!splitter!") do ( set "var=!string:%%~R%%~R=%%~L!" set "var=!var:%%~R=%%~L!" if "!var!" EQU "!string!" ( echo "%~1" does not contain "!splitter!" >&2 exit /B 1 ) ) ) if "!var!" equ "" ( endlocal & if "%~4" NEQ "" ( set "%~4=") ) if !position! LEQ 0 ( set "_skip=" ) else (set "_skip=skip=%position%") for /f "eol= %_skip% delims=" %%P in (""!var!"") DO ( if "%%~P" neq "" ( set "part=%%~P" goto :end_for ) ) set "part=" :end_for if not defined part ( endlocal echo Index Out Of Bound >&2 exit /B 2 ) endlocal & if "%~4" NEQ "" (set %~4=%part%) else echo %part% exit /b 0


  • R: Source personal scripts keeping some functions hidden
  • dplyr do() without dot pronoun?
  • R gives strange error with ggplot2 expression: object 'rversion' not found
  • Assign to list index in specific environment using `get`
  • How to escape wildcard expansion in a variable in bash?
  • Bash : Adding extra single quotes to strings with spaces
  • How to get rid of enclosing double-quotes in the expanded `forfiles` variables?
  • Brackets ${}, $(), $[] difference and usage in bash [duplicate]
  • Event difference bootstrap collapse
  • Get text between two elements JQUERY
  • add new field to form with rvest
  • Change Divider Color Android DatePicker Dialog
  • Google Bigquery Command Line Return Limit
  • Change navbar in bootstrap if user login
  • Using HTML/CSS for UI in XNA?
  • custom string delimiters stringtemplate-4
  • div fade-in when window is scrolled a certain distance from the top
  • How to define an array of floats in Shader properties?
  • C function strchr - How to calculate the position of the character?
  • A new chart every sheet
  • Validate jQuery plugin, field not required
  • Trying to get the char code of ENTER key
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • When should I choose bucket sort over other sorting algorithms?
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • php design question - will a Helper help here?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • AngularJs get employee from factory
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How to Embed XSL into XML