1

I have this long path of the temporary folder of a project: c:\\Users\\user\\source\\repos\\src\\etc\\even\\longer

I stored the string of text of the path in the file "path.txt" for the time being.

I would like to change directory from the windows command line like I would in Bash

$ cd `cat path.txt`

I tried with cd < type path.txt but I got.

The system cannot find the file specified.

Rubén
  • 11
  • 3

1 Answers1

2

You try to redirect the contents of a file named type to the cd command. The correct syntax would be cd < path.txt or type path.txt | cd - if cd would take input from STDIN, which it does not.

You have to read the content of path.txt to a variable and use cd with that variable.

There are two methods to read from a file: set /p and a for /f loop.

A

<path.txt set /p folder=
cd /d "%folder%"

or B

for /f "usebackq delims=" %%a in ("path.txt") do set folder=%%a
cd /d "%folder%"

or (directly processing) C

for /f "usebackq delims=" %%a in ("path.txt") do cd /d "%%a"

(All methods assuming there is just one line in the file - method A reads the first line only, method B reads all (non-empty) lines, keeping the last one in the variable and method C would cd in every folder listed in the file.)

Stephan
  • 53,940
  • 10
  • 58
  • 91