Solved Forfiles execute a command for each file

September 26, 2012 at 22:39:24
Specs: Windows 7
This one is a challenge for me.
So we have:
- %CD% (where %CD% may or may not have spaces)
- file.sql
- I want to execute %CD%\sqlite.exe3 file.sql > file.dat
- file.dat exists on multiple locations, recursively

forfiles /m file.dat /s /C "cmd /c echo @path" <-- this one properly lists the location of file.dat.

forfiles /m file.dat /s /C ""cmd /c "%CD%\sqlite3.exe" file.sql > @path"" <-- this one doesn't work and I can't get it to work after a good day of Googling. It's probably something extremely stupid which I missed due to my lack of knowledge but hopefully you guys could help.


See More: Forfiles execute a command for each file

Report •


✔ Best Answer
September 27, 2012 at 10:48:23
I'm missing a set of double quotes. Sorry about that.
forfiles /m file.dat /s /C "cmd /c \"\"%CD%\sqlite3.exe\" file.sql > @path\""

Since you're not doing date math, I don't see a reason not to use FOR.
for /r %%a in (file.dat) do if exist "%%a" "%CD%\sqlite3.exe" file.sql > "%%a"

How To Ask Questions The Smart Way



#1
September 26, 2012 at 23:08:45
is there a pressing reason to use cmd, instead of just:
forfiles /m file.dat /s /C "%CD%\sqlite3.exe file.sql > @path"

i don't know, i am totally unfamiliar with forfiles, and my seven is down. I figure it has to do with those doubled double-quotes confusing the parser.

you might also get away with substit () for the outside quotes:
forfiles /m file.dat /s /C "cmd /c ("%CD%\sqlite3.exe" file.sql > @path)"


Report •

#2
September 26, 2012 at 23:55:02
Thanks for your input! Unfortunately, this doesn't work for me.

> forfiles /m file.dat /s /C "("C:\Prgram Files (x86)\Room\user\sqlite3.exe" @path)"
ERROR: Invalid argument/option - 'Files'.
Type "FORFILES /?" for usage.

There's no particular reason for me to use the command like that, it's just the thing I was closest to figuring out.

I need to locate a file recursively on multiple locations and execute the command for each file.


Report •

#3
September 27, 2012 at 06:25:11
FORFILES isn't exactly a good program, if not for its command parsing then for its date logic. Anyways, you can escape the double quotes with a back slash.
forfiles /m file.dat /s /C "cmd /c \"%CD%\sqlite3.exe\" file.sql > @path"

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
September 27, 2012 at 08:04:55
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
That's if the directory I'm trying to run it from has spaces :\

Can you suggest another command to do it if forfiles isn't appropriate?


Report •

#5
September 27, 2012 at 10:48:23
✔ Best Answer
I'm missing a set of double quotes. Sorry about that.
forfiles /m file.dat /s /C "cmd /c \"\"%CD%\sqlite3.exe\" file.sql > @path\""

Since you're not doing date math, I don't see a reason not to use FOR.
for /r %%a in (file.dat) do if exist "%%a" "%CD%\sqlite3.exe" file.sql > "%%a"

How To Ask Questions The Smart Way


Report •

#6
September 27, 2012 at 14:15:17
Thanks for the "for" method! I finally understood how it works, and it does work.

Except for one thing which is just absurd.

The file.sql > "%%a" part interprets as:
file.sql 1> file.dat

There is a "1" before the ">" symbol. I figured it might be because my file was a .cmd instead of .bat (I read the latter required %% instead of % for defined variables). I renamed it to .bat and there still is a "1" before the > symbol.

I tried escaping it with a ^ sign, and it worked.
So the final version would be:

for /r %%a in (file.dat) do"%CD%\sqlite3.exe" file.sql ^> "%%a"

Also, I removed the if exists "%%a" part because it cannot NOT exist in my case (also, it didn't work for some reason).

Thank you so, so much!


Report •

#7
September 27, 2012 at 14:31:51
There is a "1" before the ">" symbol.
This is normal. If you don't specify a stream, CMD assumes you're talking about stream number one, STDOUT. Two is STDERR, while zero is STDIN. Three though nine are available, but no one uses them.

How To Ask Questions The Smart Way


Report •


Ask Question