3

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.

4
  • I tried this exact script with dir instead of someprocess and it worked fine. Does the path you typed include the drive letter and no trailing slash? Commented Jun 30, 2016 at 12:01
  • I edited my question. Commented Jun 30, 2016 at 12:12
  • I think I got it: The folder is created by the process. And the process takes a little time to create the folder. And when I do > folder/log.txt, the folder hasn't been created yet... That's probably it. Commented Jun 30, 2016 at 12:18
  • You cannot redirect into a folder that does not yet exist, so you need to do mkdir "%outputFolder%" before redirecting like > "%outputFolder%\log.txt". (Put quotes around all paths to avoid trouble with potential white-spaces or special characters.) Commented Jun 30, 2016 at 22:28

2 Answers 2

2

Use quotes in case you have spaces in path.

someprocess -p1 blabla -p2 blabla -p3 "%outputFolder%" > "%outputFolder%\log.txt"
Sign up to request clarification or add additional context in comments.

2 Comments

This didn't work. I still get the same error. And I can't have spaces in the path because the filename won't have spaces. And I am using the filename as the foldername.
Most likely the folder does not exist. What does 'SomeProcess do? Is it supposed to create the folder? If not do MD "%OutFolder%"
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.