0

I tried this code to import the full path from a text file:

for /F "tokens=2 delims==" %%a in ('findstr /I "makedir=" config.txt') do md "%%a"

The full path in txt file is:

makdir=%appdata%/test

My problem is the path returns as text, not as a directory path. Essentially I need %appdata% to expand as opposed to remain in its variable form.

Compo
  • 36,585
  • 5
  • 27
  • 39
G Zone
  • 1
  • 2
  • Hi G Zone ... welcome to stackoverflow! pls provide all of the relevant code. – Beel Jun 19 '23 at 15:36
  • @Beel, all the required code is there! THE OP has shown that the content of `%%a` will be the literal string `%appdata%/test`, _(i.e. the 2nd token of `makdir=%appdata%/test` when delimited by the `=` character)_. My assumption, as I edited the question, was that they wanted the expanded variable value string, not the variable. For example: `C:\Users\GZone\AppData\Roaming/test`, not `%appdata/test%`. It should be noted however that the code searches for a line containing the substring `makedir`, but the file itself shows a string `makdir=`. – Compo Jun 19 '23 at 15:57
  • @Compo ... so expectation is that `makdir=...` is the content of `config.txt`? and we realize that that text contains, `makdir` and not `makedir`, right? – Beel Jun 19 '23 at 16:02
  • My comment also mentions that, @Beel, but that would be irrelevant to your comment, as they've still shown "all of the relevant code", regardless of a probable typo. _At worst they've simply not provided a more complete representation of the possible entries in the text file `config.txt`._ Personally I would advise that they replace `tokens=2` with `tokens=1,*`, then replace the `%%a` in the `do` with `%%b`. – Compo Jun 19 '23 at 16:07
  • Suggest reviewing this answer ... https://stackoverflow.com/questions/1199931/how-to-expand-a-cmd-shell-variable-twice-recursively – Beel Jun 19 '23 at 16:14
  • i try this but i cant get full path for /F "tokens=1,* delims=rr=" %%b in ('findstr /I "rr=" config.txt') do md %%b – G Zone Jun 19 '23 at 17:13
  • `delims=rr=` delimits the string at each `r`, each `r`, and each `=`. Delimiters are one-char only (several chars are possible, but it splits at each occurrence of one or more of those). – Stephan Jun 19 '23 at 18:08
  • Change `makdir=%appdata%/test` in the file by `makdir=!appdata!/test` and `EnableDelayedExpansion` in your code. That is it... – Aacini Jun 19 '23 at 22:05

1 Answers1

0
@ECHO OFF
SETLOCAL
rem The following settings for the directory and filename are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q76507677.txt"

FOR /f "tokens=2delims==" %%e IN ('findstr /I "makedir=" "%filename1%"') DO (
 CALL md "%%e"
)
GOTO :EOF

Note that in the Windows world, \ is a directory-separator and / is a switch-indicator. Windows often, but not always, makes the translation. Best to use the correct form.

For instance, md creates the directory quite happily with / but dir will object.

Magoo
  • 77,302
  • 8
  • 62
  • 84