12

I'm trying to do some things during the pre-build phase of a visual studio project. Specifically, I'm trying to execute some commands on all *.resx files within the project. Here is what I have but it doesn't work when the files/directory path have a space in them. How do I get around these spaces?

for /f %%a in ('dir /B /S *.resx') do echo "%%a"
jarnosz
  • 243
  • 1
  • 18
bsh152s
  • 3,178
  • 6
  • 51
  • 77
  • Possible duplicate of [Iterate all files in a directory using a 'for' loop](https://stackoverflow.com/questions/138497/iterate-all-files-in-a-directory-using-a-for-loop) – phuclv Mar 01 '18 at 02:43

6 Answers6

23

You know that for can also run recursively over directories?

for /r %%x in (*.resx) do echo "%%x"

Much easier than fiddling with delimiters and saves you from running dir.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • This looks like the cleanest solution. I knew there had to be a trick. I'm just so used to *nix scripting where things are so much easier. Thanks you all for the info. – bsh152s Sep 19 '09 at 12:20
  • Well, it's hardly more convoluted than find/xargs, imho. – Joey Sep 19 '09 at 20:28
4

Stick with the For text parser of the shell

for /f "delims=|" %%a in ('dir /B /S *.resx') do echo "%%a"

just add a delims option (for a delim character which obviously couldn't exist), et voila!

In the absense of this delims option, /f will do what it is supposed to, i.e. parse the input by splitting it at every space or tabs sequence.

mjv
  • 73,152
  • 14
  • 113
  • 156
3

You can use findutils for Windows - it includes both "find" and "xargs"

DVK
  • 126,886
  • 32
  • 213
  • 327
3

You are running into inadvertant use of the default space delimeter. You can fix that by resetting the delims like so:

for /f "delims=" %%a in ('dir /B /S *.resx') do echo "%%a"
akf
  • 38,619
  • 8
  • 86
  • 96
2

You could also install cygwin to get a full-blown Unix-esque shell, which comes with the trusty old "find" command, plus a bunch of other tools. For example,

find . -name "*.resx" | xargs grep MyProjectName
Bugmaster
  • 1,048
  • 9
  • 18
  • And you make your whole build depend on the existence of cygwin. Not very nice. Let alone the fact that it's very un-needed for this task. – Joey Sep 19 '09 at 07:03
1

To generate a simple file list of all the relevant files for later processing

@echo create a results file…
if exist results.txt (del results.txt)
echo. >NUL 2>results.txt

@echo minimal recursive subdirectory search for filespec...
dir /s /a /b "*.resx" >>results.txt
Tom
  • 11
  • 1