user3813234
user3813234

Reputation: 1682

Batch: concatenate variable and string to form output path

I have a batch file where a user enters a path to a file and I extract the filename form that path.

I use that filename to create a folder of the same name.

In that folder, I want to create a log.txt file that the process I am calling in my batch file can write its log messages to.

Here's the code:

set /p pathFoFile="Enter path fo file: "
FOR %%i IN ("%pathFoFile%") DO (
    set outputFolder=%%~ni
)

someprocess -p1 blabla -p2 blabla -p3 %outputFolder% > %outputFolder%\log.txt

The last part, %outputFolder%\log.txt, seems to be the problem, since it works when I just put > log.txt

However, when I echo this:

echo %outputFolder%\log.txt

it prints the correct path.

How can I use the foldername and create this log.txt file?

EDIT

The path I get may look like this:

..\some\folder\thefile.egf

or just

thefile.egf

or an absolute path.

I extract the

thefile

and would like to call:

someprocess -p1 blabla -p2 blabla -p3 thefile > thefile\log.txt

the folder thefile containing log.txt should be created relative to the batch file. There are no drive letters in the path.

The error I get when using %outputFolder%\log.txt or "%outputFolder%\log.txt" is that the system cannot find the path.

Upvotes: 3

Views: 3432

Answers (2)

leetibbett
leetibbett

Reputation: 843

%%~dpni will expand the variable to include the drive, path, and filename.

Upvotes: 0

RGuggisberg
RGuggisberg

Reputation: 4750

Use quotes in case you have spaces in path.

someprocess -p1 blabla -p2 blabla -p3 "%outputFolder%" > "%outputFolder%\log.txt"

Upvotes: 2

Related Questions