Need help in writing a .bat file.

I'd like to write a .bat that would copy a file to every subdirectory of a given directory, because it's so annoying to do this manually. I did some google searches and apart from finding out about the cyclic operand for (I presume this is the one I should use) but I don't know how to point subdir1, and the end e.g. subdirN.

Could someone give me an idea? Or if you don't mind, could you please take the time and write the script for me? Thanks!
16 answers Last reply
More about need writing file
  1. What are you trying to do with this;
    1 file to all subdirectory?
    multible files to all subdirectory?
    files to a directory (and subdirectory) in a diffrent location?
    files to multible directory (from a list) in a number of locations?

    The more information you give the more help I can give. please feel free to PM me if you prefer.
  2. I would like when executed, the script, to copy file x.txt (located in x:\wherever\) to folder x:\something\ and all it's subfolders (e.g. x:\something\1, x:\something\2, x:\something\3). So eventually every subfolder of x:\something\ will have a copy of x.txt. But those subfolders vary in names so I can't just point them all as a destination. It'll take me more time to do that than manually copy+paste the file to each subdirectory of that directory. In essence as you mentioned it 'one file to every subdirectory', although I think the script will be quite similar if it were several files to every subfolder.

    Anyway, I hope I was clearer in my request this time.

    Thank you taking the time to reply!
  3. The command you need to use is the "FOR" command (check from command prompt for/?).

    But would be in the format of:
    FOR /R "DIRECTORY" %A IN <"FILENAME"> DO COPY "FILE LOCTION" %A
    Where;
    "DIRECTORY" = Directory to write to.
    "FILENAME" = Full filename (including ext but no path) of the file name to write as a copy.
    "FILE LOCATION"= Full path and name of file to copy.

    So if I wanted to copy a file called c:\MYTEXT.TXT to all subdiectory under D:\MYDIR\ the systax would be as follows:-

    FOR /R d:\mydir\ %A IN <mytext.txt> DO copy c:\mytext.txt %A

    Im not sure if this can be done with long file names (have not used for a while) but could be used in a .BAT file with no problem.
  4. Hmm, it didn't do anything here (nothing I can see that is). Could you recheck if you wrote it correctly please?

    Thank you very much for helping!
  5. Sorry my mistake, the correct systax is

    FOR /R d:\mydir\ %A IN (mytext.txt) DO copy c:\mytext.txt %A

    Note round brackets () not <>

    But would be in the format of:
    FOR /R "DIRECTORY" %A IN ("FILENAME") DO COPY "FILE LOCTION" %A
    Where;
    "DIRECTORY" = Directory to write to.
    "FILENAME" = Full filename (including ext but no path) of the file name to write as a copy.
    "FILE LOCATION"= Full path and name of file to copy.

    P.S you can set to over wite existing files by putting a /s on the end like so

    FOR /R d:\mydir\ %A IN (mytext.txt) DO copy c:\mytext.txt %A /S
  6. Hi! I tried with brackets but it still doesn't seem to copy the file. :( I tried with various formats (nfo/txt) but doesn't seem to do its work.

    By the way, to execute the command I just start the .bat file right? Want to make sure just in case I'm doing something else wrong.
  7. ok found the problem. it's on the microsoft site here
    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true
    if you are using in a batch file you need to use %%A so the line will now read:-
    FOR /R d:\mydir\ %%A IN (mytext.txt) DO copy c:\mytext.txt %%A
    this is due to the fact that batch files treat all text between the % simbols as the name of a varible.
  8. Hmm, I tried with the double percentage signs, but still no use. :( Did you try it on your own computer?

    Btw I guess I should thank you for the research you're making. :wink:
  9. should you use "xcopy" instead of "copy"? I'm probably way off base.. but might be worth a shot.
  10. Hi.

    Dunno if the syntax is different with xcopy, but I tried with the current syntax and it didn't work. :roll:
  11. OK, Tested this on my computer and it worked fine as a one line batch file

    FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A

    This copied the file d:\copyme.txt to d:\copytest\ and all subdirectory

    In answer to Coronaz you could use Xcopy if you need to do more funky stuff (Keep owership tags, Copy according to date, etc) but for a standard copy of one file copy will work fine.

    The only thing you may need to do is put the /y on the end to automatically overwrite existing files of the same name.

    FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A /Y
  12. Hey,

    It worked! :D Thank you sooo very much Hollett! By the way where did you read about the %A attribute, I couldn't find anything with a search engine as it ignores those characters.

    Thank you once again for helping me out man! :wink:
  13. This batch file worked fine for me even for long file names--excellent. The only problem is that a folder with a space in it will not get a copy. Is there is a way to get a file copied to these type of folders also? Thanks so much. -- Craig
  14. To copy a file say, C:/whatever/file, to each folder, subfolder, sub-subfolder under x:/wherever, here is a script.

    [cpp]# Script CopyToSeveralFolders.txt
    var str folderlist, folder
    # Go to destination folder.
    cd "X:/whatever"
    # Collect a list of all folders at all levels.
    lf -r -n "*" "X:/whatever" ($ftype=="d") > $folderlist
    # Go thru $folderlist, one folder at a time.
    while ($folderlist <> "")
    do
    # Get the next folder.
    lex "1" $folderlist > $folder
    # Copy file to destination folder $folder.
    system copy ("\""+"C:/whatever/file"+"\"") ("\""+$folder+"\"")
    done[/cpp]


    Script is in biterscripting ( http://www.biterscripting.com ) . Copy and paste the script in file C:/Scripts/CopyToSeveralFolders.txt, start biterscripting, enter the following command.

    [cpp]script "C:/Scripts/CopyToSeveralFolders.txt"[/cpp]
  15. Hi Guys,

    I stumbled upon this forum trying to fix the same issue.
    What I eventually did is this:

    Create a batch file with the following line:

    for /R %%i in (.) do copy "%CD%\filename.ext" "%%i"

    The statement is as following:
    for /R = for statement for every directory in that folder.
    %%i = set variable
    in (.) = set variable with every directory
    do copy = well just copy
    "%CD%\filename.ext" = first statement %CD% is for current directory, second is the filename you want to copy. Put this inside "" to solve the space issue in directories
    second %%i = is the same variable. It remains to be the current directory. Put it inside "" to solve the space issue.

    This worked for me. You can use it without %CD%\ because you are executing it from the directory the file is in.

    Hope this helps anyone stumbling on this article.
  16. hollett said:
    OK, Tested this on my computer and it worked fine as a one line batch file

    FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A

    This copied the file d:\copyme.txt to d:\copytest\ and all subdirectory

    In answer to Coronaz you could use Xcopy if you need to do more funky stuff (Keep owership tags, Copy according to date, etc) but for a standard copy of one file copy will work fine.

    The only thing you may need to do is put the /y on the end to automatically overwrite existing files of the same name.

    FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A /Y


    Hollet, Thanks for your guide, what if the opposite, ie of all the subfolders containing the files. xml and will copy one folder to the folder where the folder is not made anymore. Thanks before
Ask a new question

Read More

Windows XP