Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have one file and I would like to copy and paste it to all the folders in a directory. is there a way to make it via cmd/dos?

Could you please help me?

What I have tried:

I tried xcopy command but no use.
Posted
Updated 28-Apr-21 3:46am
v2
Comments
CHill60 28-Apr-21 9:28am    
What exactly did you try with xcopy?

for /D %i in (*) do copy <filename> %i
 
Share this answer
 
Why would you copy a file to more than one folder? That seems like a waste of disk space and sounds like it's bordering on a bad idea. But, there may be some legit reason for doing it.

The command line tools will only target one destination folder at a time, so you're going to have to generate a list of directory paths, then iterate over that list, running a copy command to copy the file to each of those folders, one at a time.

You do can this in a batch file with the following command:
for /f "tokens=*" %%G in ('dir /a:d /b /s') do copy sourcefile.ext "%%G"

There is a teensy problem with this. Any directory path with a '.' character in it will be treated like a file extension. For example, a folder with the name "Software 2.31.2" will be seen as "Software 2.31" (note the missing 3rd number.)

So if you have periods in your folder names, you can run into problems.
 
Share this answer
 
Just try using a loop in /Recursive /Directories:

FOR /R - Loop through files (recursively)
FOR /D - Loop through several folders/directories

For /R "Drive:\Path\To\Origem\Folder" /D

And use %~Full Path To Target Folder:

copy "D:\Path\To\My-File-To-Copy.eXtension" "%~fi"

+ In your command line:

For /R "D:\Path\To\Folder" /D %i in (*)do copy "D:\Path\To\My-File-To-Copy.eXtension" "%~fi"

+ In your bat/cmd file:

For /R "D:\Path\To\Folder" /D %%i in (*)do copy "D:\Path\To\My-File-To-Copy.eXtension" "%%~fi"

Quote:
The option /d /r is undocumented, but can be a useful combination, while it will recurse through all subfolders the wildcard will only match against Folder/Directory names (not filenames).


Quote:
Period/Full Stop
Although Win32 will not recognise any file or directory name that begins or ends with a '.' (period / full stop) it is possible to include a Full Stop in the middle of a directory name and this can cause issues with FOR /D.
Parameter expansion will treat a Full Stop as a file extension, so for a directory name like "Sample 2.6.4" the output of %%~nG will be truncated to "Sample 2.6" to return the whole folder name use %%G or %%~nxG

Sources: for /d - Loop through directory - Windows CMD - SS64.com[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900