12

I need a batch files the launches all files in a given folder (in this case it is c:\macros\day).

I've tried the following, but it doesn't seem to do anything.

for /f %i in ('C:\macros\Day /b') DO command %i
Mat
  • 202,337
  • 40
  • 393
  • 406
sifuhall
  • 415
  • 2
  • 5
  • 9

6 Answers6

10

This works from my command line:

for /F "usebackq" %%i in (`dir /b C:\macros\Day\`) DO %%i

as does this:

for %%i in (C:\macros\Day\*) do %%i
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
6

You used the incorrect variant of for. simply do (pun intended) for %%i in (c:\macros\Day\*) do %%i

Edit: If you need to run a command on all files: for %%i in (c:\macros\Day\*) do command %%i

Steven
  • 1,365
  • 2
  • 13
  • 28
wmz
  • 3,645
  • 1
  • 14
  • 22
  • Thanks for the reply. However that give me: \macros\Day\*) was unexpected at this time. – sifuhall Feb 12 '12 at 17:00
  • 3
    It should be, `for %i in (c:\macros\Day\*) do COMMAND %i` (you need to specify the command that operates on %i) – René Nyffenegger Feb 12 '12 at 17:06
  • 1
    if run from batch file, you need to replace %whatever with %%whatever (so %i becames %%i and so forth). You've given your example with single %, so I did as well.. – wmz Feb 12 '12 at 17:07
  • @RenéNyffenegger yes you're right I will update the answer (reading question I was under the impression OP wants to fire something directly executable) – wmz Feb 12 '12 at 17:11
2

You should use dir /b to list all files, so it becomes

for /f %i in ('dir /b c:\macros\Day') do command %i
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
0

Also, make sure that you make variables inside batch files %%i rather than %i otherwise you get an error in the form "i was unexpected at this time."

user1695505
  • 265
  • 2
  • 13
0

Running all files in directory via batch file with appended text on each line

An alternative is posted above - leave off the trailing "/Z /U" if you wish.

Community
  • 1
  • 1
0

This is how I could run all powershell files in same directory as the batch file

@ECHO OFF

SET PowerShellExe=%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe

for %%i in (*.ps1) do (

%PowerShellExe% -NoProfile -ExecutionPolicy Bypass -Command %cd%\%%i    

)
Pavenhimself
  • 527
  • 1
  • 5
  • 18