2

My faithful path traversal method no longer works - it sees spaces as delimiters. It's been a while since I needed to do batch programming.
When using the FOR loop, the only one that allows delimiters are the FOR /F option.
I don't want to create a temporary file containing the path, was hoping to do something like below:

C:\Users>for /f "delims=;" %i in %path% do echo %i
C:\Program was unexpected at this time.

C:\Users>for /f "delims=;" %i in (%path%) do echo %i
\Common was unexpected at this time.

C:\Users>for /f "delims=;" %i in 'foo;bar' do echo %i
'foo was unexpected at this time.
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Clothahump
  • 47
  • 5
  • Please do not insert your chosen solution into the question! I did a [rollback](http://stackoverflow.com/revisions/38913232/4). Accepting an answer clearly shows which solution worked out best for you. Thank you! – aschipfl Aug 25 '16 at 15:19
  • Closely related: ['Pretty print' windows %PATH% variable - how to split on ';' in CMD shell](https://stackoverflow.com/q/5471556). – aschipfl Oct 28 '20 at 03:12

4 Answers4

2
@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (^"!path:^;^=^
% Do NOT remove this line %
!^") do echo %%a

Output example:

C:\Program Files (x86)\AMD APP\bin\x86_64
C:\Program Files (x86)\AMD APP\bin\x86
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Windows Live\Shared
C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static
C:\Program Files\Bandizip\7z
Aacini
  • 65,180
  • 12
  • 72
  • 108
1

Use a standard for loop rather than for /F "delims=;" with some smart sub-string replacement:

for %I in ("%PATH:;=";"%") do @echo %~I

Supposing PATH contains C:\Program Files\someapp1\app1.exe;C:\Program Files\someapp2\app2.exe;C:\Program Files\someapp3\app3.exe, you will receive the following output:

C:\Program Files\someapp1\app1.exe
C:\Program Files\someapp2\app2.exe
C:\Program Files\someapp3\app3.exe

This works only in case none of the paths are surrounded by " and none of them contains ;.

In a batch script the code must be changed to this:

for %%I in ("%PATH:;=";"%") do @echo %%~I
aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

A perfect solution must be able to differentiate between ; delimiters and quoted ; literals.

Here is a very robust solution that always works using JREPL.BAT - a regular expression find/replace utility

for /f "eol=: delims=" %%F in (
  'jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path'
) do echo(%%F

If all you want to do is list each folder, one line per folder, then simply remove the FOR /F loop

call jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path

If you want a pure batch solution that always works, then you can use:

@echo off
setlocal DisableDelayedExpansion
set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
set var=%var:""="%
set "var=%var:"=""Q%"
set "var=%var:;;="S"S%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
setlocal EnableDelayedExpansion
set "var=!var:"Q=!"
for %%a in ("!var:"S"S=";"!") do (
  if "!!"=="" endlocal
  if %%a neq "" echo %%~a
)

The above is a slightly improved version of jeb's original solution

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Follow these Instructions from Mongodb .

cd C:\
md "\data\db"

After this step, you could once check in "C:\Program Files\MongoDB\Server\<Version Number>\bin\mongod.cfg"

there you could see, a default option of DBPath

# Where and how to store data.
storage:
  dbPath: %MONGO_DATA_PATH%
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

Now add the %MONGO_DATA_PATH% variable in Environment variables of your system as key, where value is the file path ( where you have created the data/db folder)

now you could start mongod process without passing an argument of dbPath.

5warag
  • 46
  • 2