Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#1
Post
by shadeclan » 02 Nov 2011 06:55
Is there a way to use the full path in a FOR loop? For example, the following code works if the current directory is where the file resides:
Code: Select all
cd /d "C:\Program Files\My Apps\IDDTextDirectory"
for /f "tokens=1-8,*" %%a in (iDD.txt) do (
@echo.%%i
)
However, I don't seem to be able to do this:
Code: Select all
for /f "tokens=1-8,*" %%a in ("C:\Program Files\My Apps\IDDTextDirectory\iDD.txt") do (
@echo.%%i
)
Is there a way to do that?
Edit 9:31 am EST - Sorry: the FOR loop doesn't like double quotes. I changed the first code to reflect this. However, in the second code, it can't find the file without the quotes and fails with the quotes. Single quotes cause the file to come up in the default application.
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#2
Post
by dbenham » 02 Nov 2011 07:38
That is why the "USEBACKQ" option support was added to FOR /F. Use HELP FOR to see all the ways USEBACKQ impacts FOR /F behavior.
Code: Select all
for /f "usebackq tokens=1-8,*" %%a in ("C:\Program Files\My Apps\IDDTextDirectory\iDD.txt") do (
@echo.%%i
)
It is possible to get your result without using "USEBACKQ" if you escape any problem characters (
<space> in your case - other problem characters are
, ; = ^ & )
Code: Select all
for /f "tokens=1-8,*" %%a in (C:\Program^ Files\My^ Apps\IDDTextDirectory\iDD.txt) do (
@echo.%%i
)
Dave Benham
Last edited by
dbenham on 02 Nov 2011 07:48, edited 2 times in total.
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#3
Post
by shadeclan » 02 Nov 2011 07:44
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#4
Post
by dbenham » 02 Nov 2011 07:47
Look at my edited response above as well - you can escape problem characters like <space>
Dave Benham
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#5
Post
by shadeclan » 02 Nov 2011 07:56
dbenham wrote:Look at my edited response above as well - you can escape problem characters like <space>
Dave Benham
Thanks for the info - I'll tuck that away for later use. In real life, I'm passing a full path back from a custom DOS function I created, so escaping spaces would prove difficult.
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#6
Post
by shadeclan » 02 Nov 2011 13:01
Here's a related question. I need to use a FIND statement in a FOR loop. I'm having the same problem - the loop is blowing up. Even with the usebackq option, it still blows up. Just for giggles, I tried pasting the path into the FIND statement and escaping the spaces - no luck.
Code: Select all
for /f "usebackq tokens=1-8,* skip=2" %%a in ('find "Mozilla Firefox" "C:\Program Files\My Apps\FileLocation\firefox.txt"') do (
@echo.%%i
)
The find statement does work fine outside the loop context by the way. It also works inside the loop just fine if I change the current directory first and just use the filename.
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#7
Post
by shadeclan » 02 Nov 2011 13:25
Got It! Twas a combination of PEBKAC error and the fact that the FIND function doesn't like USEBACKQ.
Sorry.
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#8
Post
by dbenham » 02 Nov 2011 13:48
Actually FIND does not care about USEBACKQ - It is the FOR /F syntax that changes with the USEBACKQ option:
Code: Select all
for /f %%A in (file1 file2...) --> for /f "usebackq" %%A in ("file1" "file2"...)
for /f %%A in ("string") --> for /f "usebackq" %%A in ('string')
for /f %%A in ('command') --> for /f "usebackq" %%A in (`command`)
You can either preserve the USEBACKQ option and change the quotes from ('command') to (`command`), or you can drop the USEBACKQ option and leave the ('command') alone.
Dave Benham
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#9
Post
by shadeclan » 03 Nov 2011 06:50
dbenham wrote:Actually FIND does not care about USEBACKQ - It is the FOR /F syntax that changes with the USEBACKQ option:
Code: Select all
for /f %%A in (file1 file2...) --> for /f "usebackq" %%A in ("file1" "file2"...)
for /f %%A in ("string") --> for /f "usebackq" %%A in ('string')
for /f %%A in ('command') --> for /f "usebackq" %%A in (`command`)
You can either preserve the USEBACKQ option and change the quotes from ('command') to (`command`), or you can drop the USEBACKQ option and leave the ('command') alone.
Wow! You can use accent marks with USEBACKQ in a FOR statement? That's heavy!
DOS Batch seems to have all these little undocumented features that only the truly initiated may know ...
Thanks for the help!
-
alan_b
- Expert
- Posts: 357
- Joined: 04 Oct 2008 09:49
#10
Post
by alan_b » 03 Nov 2011 09:29
Actually you get a ton of documentation by typing
Most DOS commands give help via /?
-
shadeclan
- Posts: 61
- Joined: 02 Jun 2011 11:29
- Location: USA - Somewhere between Albany NY and Bennington VT
#11
Post
by shadeclan » 03 Nov 2011 10:03
alan_b wrote:Actually you get a ton of documentation by typing
Most DOS commands give help via /?
Namaste!
I actually have a link to DOS commands in my Windows XP help file. The explanation tends to be a little bit more thorough than the help parameter.