DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: DONE: Copy one particular file into every folder in a subdirectory  (Read 18,858 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Say we have a subdirectory -- c:\1 -- that contains 100 folders.  We have a file on our desktop -- file.txt -- that we want to go into every single one of those folders.

We could write a 100-line batch file to do this.

Is there an easier way?  A one-liner?

Thanks!



skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
This is easily accomplished with a bit of AutoHotkey script.  Are you open to that?

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Sure I'm open to an AutoHotkey solution.  Anything to easily get the job done.  And that might be useful to others.  Who knows.

If, though, the best bet would be to simply write a long batch file, I'll stick with that.

Thanks.

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
I know that skwire was going to make one in AutoHotkey, but this only took me a few to make. :-)

Easy to use, select the target folder and the source file. Press Go!

SubCopy Download

Ashampoo_Snap_2013.04.05_09h49m52s_001_SubCopy.png

If you find any bug or issues, please let me know.
« Last Edit: April 05, 2013, 02:58 PM by c.gingerich »

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Nicely done.  I sent my message last night and went to bed so it was nice to see this snack completed when I woke up.  =]  Nick, let us know if this will work for you and I'll mark this thread as done.

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Thanks!  :Thmbsup:

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
We could write a 100-line batch file to do this.
It could be made smaller than that with a "SWEEP" aka "GLOBAL" type utility that repeats a command in every directory matching a filespec.  Some can be found in the Fidonet batch programming echo's file depository, http://www.filegate.net/bfds/ ; These are old, though, and I don't know how well they'd work in current Windows consoles.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
You folks are amazing.  Another one DONE!

Thanks so much.

x16wda

  • what am I doing in this handbasket?
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
    • Participated in NANY Challenge Once
Oh my, let's do it even easier.  Open a command prompt in the top folder and try:

          for /r %I in (.) do copy c:\folder\source.txt %I\target.txt

If you run it from a batch file you'd need to double the % marks.  Or if you don't want to navigate to the top folder for your command prompt, just enter its path after the /r and before the %I.

Edit: looking back at the original post, you could open a command prompt on your desktop (very easy with ContextConsole shell extension by the way) and enter the command as:

          for /r c:\1 %I in (.) do copy file.txt %I\file.txt
vi vi vi - editor of the beast
« Last Edit: April 05, 2013, 11:46 PM by x16wda »

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
I've used c.gingerich's created program -- SubCopy -- a couple of times now, and it makes the task a piece of cake.

The great thing about these threads is that anyone searching the Internet trying to find a similar solution will probably be directed here.  Makes life a lot easier.

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • Participated in NANY Challenge Six Times
    • Coding Snacks
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #10 on: April 06, 2013, 11:53 AM »
I'm glad you like it and it does what you needed.  :D

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #11 on: December 19, 2013, 02:17 AM »
Two questions:

1.  SubCopy seems not to work with copying "shortcuts" into the target folders.  Can Chris or anyone explain why it's hard to handle shortcuts?

2.  SubCopy presently doesn't allow copying "folders."  While I have a small use for doing that, does such a request seem a bit over-the-top?

===

Yesterday using SubCopy I repeatedly copied various files into 75 different sub-folders.  SubCopy saved a huge amount of time, energy, tedium.  Great little gem.

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #12 on: December 19, 2013, 02:27 AM »
Can Chris or anyone explain why it's hard to handle shortcuts?

It's not a matter of difficulty.  The fact is, shortcuts are files, too (.LNK).  So, you can either move them like a normal file or you can extract information from them (target, working dir, etc.) and then use that information.  The problem is that, unless you write the code so that it extracts that information, it will be treated like a normal file.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #13 on: December 19, 2013, 02:55 AM »
I find that indeed since the shortcut is a file..., if I RENAME the extension, yes, I can use SubCopy to copy to the various sub-folders.

Example:  Do.lnk ---> Do.lnkz

But without such renaming the extension, SubCopy won't copy the shortcut itself.

Trouble with the renaming of extension, one has to recursively go in and change the extension back to what it should be.

Recursively changing filename extension creates its own issue.  In present case..., 75 nested instances of Do.lnkz.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #14 on: December 19, 2013, 03:44 AM »
Just in case I decide to recursively rename Do.lnkz back to Do.lnk...

...can you please remind me how to do that?

Do.lnkz ---> Do.lnk

In 75 sub-folders.

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #15 on: December 19, 2013, 04:13 AM »
Just in case I decide to recursively rename Do.lnkz back to Do.lnk...
...can you please remind me how to do that?

Any of the myriad of file renamers out there will do the trick.  Find one you like...

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: DONE: Copy one particular file into every folder in a subdirectory
« Reply #16 on: December 20, 2013, 07:54 AM »
Okay, Skwire, the renaming approach worked.

Chris, For some reason SubCopy doesn't allow directly inputting or even editing the field "Source file."  (Only option is Windows navigation and selecting.)  If user could do that, it may be possible to place the full path and name of the shortcut file.

Example:  c:\downloads\do.lnk

That would be easier than having to rename, copy to subfolders, and then recursively rename back again.

But, hey, at least the task can be accomplished.  I'm quite thankful for that.

Original address: https://www.donationcoder.com/forum/index.php?topic=34548.0