user2148537
user2148537

Reputation: 1

Batch script to open text file

I have a batch script to read a text file. It's working fine if the file name doesn't contain special characters, but it's failing if the file name contains special characters.

Ex file name: localhost_access_log.2013-03-08

Code:

for /f "skip=%* tokens=*" %%b in (Y:\Program Files\Apache Software Foundation\Tomcat 7.0\logs\localhost_access_log.2013-03-08.txt) do (
    echo "Data Copied"
)

Please help me how to read these kind of files.

Upvotes: 0

Views: 2100

Answers (1)

Nate Hekman
Nate Hekman

Reputation: 6657

Type help for at the command line to get the documentation on the for command (which I admit is rather hard to read), and you get this buried about halfway down:

For file names that contain spaces, you need to quote the filenames with
double quotes.  In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.

So your code should change to this:

for /f "usebackq skip=%* tokens=*" %%b in ("Y:\Program Files\Apache Software Foundation\Tomcat 7.0\logs\localhost_access_log.2013-03-08.txt") do (
    echo "Data Copied"
)

Upvotes: 1

Related Questions