Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For /f processing filename with spaces

I want to do some processing on each line of a file.

for /f "delims=" %%i in ("%RF_PROPERTIES%") do (          
   echo %%i
)

My RF_PROPERTIES points to a file path with spaces (c:\program files\Arcot systems\conf\rf.properties). It is complaining saying Environment variable C:\Program Files\Arcot not defined, even though I have provided quotes. How to get it working?

like image 898
Hugh Darling Avatar asked Feb 15 '11 16:02

Hugh Darling


People also ask

Can a filename include spaces?

Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.

How do you handle spaces in filename?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified.

How do you handle a filename with space in Unix?

There are two main ways to handle such files or directories; one uses escape characters, i.e., backslash (\<space>), and the second is using apostrophes or quotation marks. Using backslash can be confusing; it's easy and better to use quotation marks or apostrophes.

Can Java file names have spaces?

Java per se handles spaces in filenames just fine with no escaping or anything. You may be trying to access a file the user running the jvm don't have access to.


1 Answers

Try adding the usebackq option:

for /f "usebackq delims=" %%i in ("%RF_PROPERTIES%") do (          
  echo %%i
)

Explanation from the output of for /?:

usebackq - specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in file-set.

like image 75
Blorgbeard Avatar answered Sep 18 '22 18:09

Blorgbeard