I am wanting to copy my .gitignore file into all projects in my directory.

I do not want to overwrite the file if a folder already has it (so flagging to request confirmation /-y)

for /d %a in (C:\WebDocs\wamp\www\) do copy /-y C:\WebDocs\wamp\www\FolderContainingFileIWantCopied\.gitignore %a

Can someone confirm this is accurate before I run it, and let me know what I should expect (there are over 200 folders, with approximately 10-20 having a .gitignore file currently.

While I was comfortable in the command line in the 90's, I am nervous about running a command that could touch so many projects

  • 2
    There are no wild-cards in the in clause, so the for loop will iterate only once. – AFH Jun 7 at 17:29
  • If you want to test a complex loop command, run the inner loop with echo in front of the command to verify the command that it would run. – Chloe Jun 11 at 1:08

For top-level subfolders only:

If you want to stick to command-line:

for /d %A in ("C:\WebDocs\wamp\www\*") do (if not exist "%A\file.gitignore" copy /y "C:\WebDocs\wamp\www\FolderContainingFileIWantCopied\file.gitignore" "%A\file.gitignore")

I generally prefer using variables where I can so I also wrote it out in batch - and also using if not exist in place of the copy /-y prompt to make it more automated, but still not copy unnecessarily (you can remove the if not exist in favor of /-y if you prefer the command prompts, but I feel like it just looks messy - especially when it may repeat a really long command every time).

@echo off

set "dir=C:\WebDocs\wamp\www\"
set "file=file.gitignore"
set "filepath=%dir%\FolderContainingFileIWantCopied\%file%"

for /d %%A in ("%dir%*") do (
    if not exist "%%A\%file%" copy /y "%filepath%" "%%A\%file%"

Either should accomplish what you want. The important pieces yours is missing are * in your for /d folder set and that the %A (command-line) or %%A (batch) at the end needs to also include the name of the file you've copied, which is why I have it written as %A\file.gitignore and %%A\file.gitignore, respectively (though in the batch it's represented as variables).

For all subfolders:

For command-line:

for /d /r "C:\WebDocs\wamp\www\" %A in (*) do (if not exist "%A\file.gitignore" copy /y "C:\WebDocs\wamp\www\FolderContainingFileIWantCopied\file.gitignore" "%A\file.gitignore")

For batch:

@echo off

set "dir=C:\WebDocs\wamp\www\"
set "file=file.gitignore"
set "filepath=%dir%\FolderContainingFileIWantCopied\%file%"

for /d /r "%dir%" %%A in (*) do (
    if not exist "%%A\%file%" copy /y "%filepath%" "%%A\%file%"

Reference: copy and for

  • "The system cannot find the file specified." (repeating loop - using batch processing) – Kender Jun 10 at 20:49
  • I removed the echo off and it looks like it is going into subfolders \wamp\www\SubFolder\.git\objects\56 for example – Kender Jun 10 at 20:52
  • 1
    ah I didn't know how far you needed to dive; I've added the batch for that to my answer and will post the command-line version when I have a chance to. – mael' Jun 10 at 21:19
  • added command-line as well; you'll see that the only difference between your initial script was the missing * in your for /d folder_set and the missing file name at the very end for the copy command. the if not exist is just personal preference, so as I said in the answer you can remove if not exist "%A\file.gitignore" and change the copy /y to copy /y`. – mael' Jun 10 at 21:28
  • Thank you. The filename is .gitignore there is nothing before the dot so I made that small edit and it worked fine. I want to thank you for your time in doing both the command line and batch file versions of the command, I learned a little today. – Kender Jun 12 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.