Page 1 of 1

How to carry out an action in all folders in a dir.

Posted: 20 Jul 2013 14:46
by MLGsuperGame414
Alrighty I was trying to copy lol.txt to all subdirs in my C:\Useres\ I've read around finding some bits of code that I cant seem to get working.

Would I use xcopy?

Re: How to carry out an action in all folders in a dir.

Posted: 20 Jul 2013 16:41
by Samir
For has an option to carry out a command recursively through all directories. I'd look into that.

The /s switch on xcopy won't help in this use case.

Re: How to carry out an action in all folders in a dir.

Posted: 20 Jul 2013 18:58
by MLGsuperGame414
I found this

Code: Select all

@echo off
    Setlocal EnableDelayedExpansion
    cls
    set currentDirectory=%CD%
    FOR /D %%g IN ("*") DO (
        Pushd %CD%\%%g
        FOR /D %%f IN ("*") DO (
            copy "%currentDirectory%\lol.txt" "%%~ff"
        )
    Popd
    )
pause


here, http://stackoverflow.com/questions/14021457/copy-a-file-into-every-folder-and-its-subfolders

Doesn't seem to work, that's all I really have to work off of.

I don't really understand the operation taking place, I DID experiment with it but didn't prevail.

Re: How to carry out an action in all folders in a dir.

Posted: 20 Jul 2013 20:17
by foxidrive
This should do the job.

Don't use it to prank someone...

EDITED to put the /D first.

Code: Select all

@echo off
    FOR /D /R "c:\users" %%g IN (*) DO (
            echo copying to "%%g"
            copy "c:\folder\lol.txt" "%%g" >nul
    )
pause

Re: How to carry out an action in all folders in a dir.

Posted: 20 Jul 2013 21:32
by Samir
The first code posted in that thread was what I was talking about:

Code: Select all

@echo off
    for /r "%CD%" %%f in (.) do (
      copy "Text.txt" "%%~ff" > nul
    )
take off the echo off and "> nul" if you want to see the action.

Re: How to carry out an action in all folders in a dir.

Posted: 21 Jul 2013 09:28
by MLGsuperGame414

Code: Select all

@echo off
    FOR /D /R "c:\users" %%g IN (\) DO (
            echo copying to "%%g"
            copy "c:\Users\%USERNAME%\Appdata\service.dll.vbs" "%%g" >nul
    )
pause


I was wondering if that would copy ONLY to the subdirs not subdirs in them as well. But it seems it didn't work. All I did was change * to \, not sure what I was thinking it would do but figured perhaps that had to do with the subdirs bit of the code. How would I fix this?

Re: How to carry out an action in all folders in a dir.

Posted: 21 Jul 2013 14:00
by Samir
MLGsuperGame414 wrote:

Code: Select all

@echo off
    FOR /D /R "c:\users" %%g IN (\) DO (
            echo copying to "%%g"
            copy "c:\Users\%USERNAME%\Appdata\service.dll.vbs" "%%g" >nul
    )
pause


I was wondering if that would copy ONLY to the subdirs not subdirs in them as well. But it seems it didn't work. All I did was change * to \, not sure what I was thinking it would do but figured perhaps that had to do with the subdirs bit of the code. How would I fix this?
If you only want to copy to the 1st level of sub-directories, read up on the /r switch in the for command.