I am trying to use a batch file to determine the currently installed drive letter and to replace a keyword in the ini file with the drive letter.

Basically the ini file contains:


And so on.

What I'm trying to do is to put the bat file into the directory containing this app.ini file to detect the current drive. Some users might have this directory in D: , E: , F: and then to rewrite this app.ini file, replacing the word "Drive" into the drive letter D: , E: or F:

I've thought of using Tokens and Delims but I'm not exactly sure how to work it out. Any expert advice? I'm only looking for batch method because a batch file will cover all I need (detect Windows XP or 7, x86 or x64 and eventually copying this ini file to the correct folder on the OS to run).


In your batch file, use %CD:~0,2% to replace Drive

CD is the DOS equivalent for the Unix command pwd. Both commands output the current path. Since you only want the drive letter, we want to take the first two letters of the output only.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\me>echo %CD%

C:\Users\me>echo %CD:~0,2%

Source: Windows Batch Script Get Current Drive name

| improve this answer | |

if you have vim installed (fully, with command-line mode)

vim app.ini -c "%s/Drive/%cd:~0,2%/g|wq"

it will replace the word Drive inside the app.ini file with the drive (letter and colon, eg c:) you're in

| improve this answer | |
  • 2
    John gave you the complete answer. %cd:~0,2% will get you the C: text if you are in the C drive. His answer will actually replace within your app.ini file all the occurrences of the word Drive into C:. For example, "dir=Drive\ApplicationPath" will be converted to "dir=C:\ApplicationPath" – Sun Oct 22 '14 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.