bpl5000
asked on
Need a batch file to copy a file to multiple subfolders
I need a batch file that will copy a file (desktop.ini) into multiple subfolders, but only one layer deep. For example, in c:\temp... I want it to copy the file into:
c:\temp\folder1
c:\temp\folder2
c:\temp\folder3
But not into...
c:\temp\folder1\subfolder1
c:\temp\folder2\subfolder2
etc.
I just want to copy the file into subfolders that are in the root of the temp folder. I will be running this command on a Windows 2008 server. Any help would be greatly appreciated!
c:\temp\folder1
c:\temp\folder2
c:\temp\folder3
But not into...
c:\temp\folder1\subfolder1
c:\temp\folder2\subfolder2
etc.
I just want to copy the file into subfolders that are in the root of the temp folder. I will be running this command on a Windows 2008 server. Any help would be greatly appreciated!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kent, the code you have works well except if the file exists. If it exists, then I get access denied. Anyway around this?
ASKER
When I try using power shell with KenMcF's commands, I get the following error...
Copy-Item : Access to the path 'D:\users\DSmith\desktop.i ni' is denied.
Anyway around the access is denied? I opened PowerShell with Run As Administrator.
Copy-Item : Access to the path 'D:\users\DSmith\desktop.i
Anyway around the access is denied? I opened PowerShell with Run As Administrator.
Does the account you are using to run the script have access to the folder? Can you manually copy the file into that folder?
ASKER
Yes, I'm using a domain admin account and I can manually copy and overwrite the file. I think it might have something to do with desktop.ini being a system file, but it does seem strange that it lets me do it manually.
Try this then
foreach ($dir in $dirs){copy-item c:\desktop.ini $dir.fullname -force}
foreach ($dir in $dirs){copy-item c:\desktop.ini $dir.fullname -force}
ASKER
It's looking better! Now I'm getting the message "desktop.ini already exists". Is there a switch to overwrite?
ASKER
It seems that "-force" should overwrite. Maybe I should first delete the file? I tried this...
foreach ($dir in $dirs){remove-item c:\desktop.ini $dir.fullname -force}
But I get this error for each folder...
A parameter cannot be found that matches parameter name 'c:\temp\folder1'
I know I have the remove-item parameters wrong, but I can't figure out the correct way.
foreach ($dir in $dirs){remove-item c:\desktop.ini $dir.fullname -force}
But I get this error for each folder...
A parameter cannot be found that matches parameter name 'c:\temp\folder1'
I know I have the remove-item parameters wrong, but I can't figure out the correct way.
you can do this
remove-item "$($dir.fullname)\desktop. ini" -force
remove-item "$($dir.fullname)\desktop.
ASKER
That does work, but when people are in the folder, it recreates the ini file after the deletion and before I can copy it. Once the file is recreated, then I can't copy the file.
Since the PowerShell commands are all new to me, I tried using kdyer's batch file and attrib the desktop.ini file first. It looks like this...
FOR /F %%A IN ('DIR /AD /B D:temp\*') DO (
attrib -h -s D:\temp\%%A\desktop.ini
COPY desktop.INI D:temp\%%A
attrib +h +s D:\temp\%%A\desktop.ini
)
This seems to work well... thanks for the help!
Since the PowerShell commands are all new to me, I tried using kdyer's batch file and attrib the desktop.ini file first. It looks like this...
FOR /F %%A IN ('DIR /AD /B D:temp\*') DO (
attrib -h -s D:\temp\%%A\desktop.ini
COPY desktop.INI D:temp\%%A
attrib +h +s D:\temp\%%A\desktop.ini
)
This seems to work well... thanks for the help!
You kids and your fancy powershell scripts. In my day we only had one line to work with. AND WE LIKED IT!
for /d %i in (c:\temp\*) do attrib -s -h -r "%i\desktop.ini" && copy desktop.ini "%i" && attrib -s -h -r "%i\desktop.ini"
Now get off my lawn =P
for /d %i in (c:\temp\*) do attrib -s -h -r "%i\desktop.ini" && copy desktop.ini "%i" && attrib -s -h -r "%i\desktop.ini"
Now get off my lawn =P
Or will you manually enter each directory ?
If manually its quite simple
http://support.microsoft.com/kb/240268
Please check out this link and look into COPY and XCOPY, XCOPY is more powerful than copy but if you want the batch file to find the sub directories then this will be much more complicated as you will need to use more advanced commands like FIND and FOR
If that is the case then this would also be easier in just about any other language, even VBS