Need help CMD copy to multiple locations

bankadmin
bankadmin used Ask the Experts™
on
I need to put a file into about 100 profiles desktop folder, all the profiles reside in one folder. I also need to remove a file from the same location its not the same file so I can't use the replace command.
This folders are named by username its for re-directed profiles in citrix

I have tried copy c:\filename f:\profiles\%username%\desktop
it only copies to my desktop folder
I have tried copy c:\filename "f:\profiles\*\desktop
It errors out I have tried ** also
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support

Commented:
> all the profiles reside in one folder

You need something like this in a bat/cmd file.
Open a CMD window and run the file.

Note: The code is currently in debug mode for visual reasons. If you are satisfied w/ how it runs in the cmd window, remove the ECHO prefix fronting the COPY and DEL commands to run it for real.
@echo off
setlocal enabledelayedexpansion
for /d %%a in (f:\profiles\*) do (
  ECHO copy "c:\filename" "%%~fa\desktop"
  ECHO del "%%~fa\desktop\thisotherfile"
)

Open in new window


If you want to run the code directly in the cmd window, without saving it to a bat/cmd file, just replace all the double %% with single %
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
TEST FIRST!

Copy a file to all user desktops (you MUST have permission to each folder to do this)
for /f "tokens=*" %a in ('dir /b /ad c:\users') do @copy "c:\source\path\filename.ext" "f:\profiles\%a\desktop"

For the file deletion:
for /f "tokens=*" %a in ('dir /b /ad c:\users') do @echo del "f:\profiles\%a\desktop\filename.ext"
Distinguished Expert 2017

Commented:
Has consideration been given to use user GPP to push files onto user desktop.

Depending on what the file is, read only or the user can write into it?
If read-only, a computer based GPO/GPP can handle the removal/copy of files into all user desktop.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
NVIT I was able to get that to work for me. Thank you and the others who responded.
I did run into an issue today the file that I moved to everyone's desktop folder using the command worked however users don't have permissions to the file I added. Any idea's on that?
Distinguished Expert 2017

Commented:
Pkease refer to my suggestion.
It is best to use this as a user based process who would copy using their own permissions the files in question.
You would need to use icacls post copy to reprocess the rights to child objects.
End-user support
Commented:
@bankadmin...

Your original question says you need to copy the file. But your last post says you moved the file. Technically, moving a file will cause the denied issue you describe. Use Copy command instead.

Check the Security rights on the source file. Give Domain Users the 'Read' right. Make sure no Deny rights exist.

EDIT:
Not necessary to adjust Security rights mentioned, presuming you as admin are running the Copy command. Security rights of copied file will be taken from target folder.
Distinguished Expert 2017

Commented:
NVIt, not disagreeing with you with the exception that it is likely this might be needed again when the new file needs to be replaced with an newer one.
Using a user based GPO that runs a script the admin controls is a fairly straight forward. The script would need some added logic on whether to run or not by checking with the old file exists at the destination before it deletes the old file and adds a new one.

Does the end user need write into the file, does the user modify this file, if the user only views the contents of the file which the delete old and copy new one means to me. If I were in the asker's place, I would limit how many copies of this file I need to manage by placing the file for user access only in the public desktop thus limiting the user's ability to delete/modify the file.
NVITEnd-user support

Commented:
Arnold. Either way works. But, since he seems to have it working already, apart from the"move" vs "copy" issue I mentioned
Distinguished Expert 2017

Commented:
have seen times that even when files are copied into a location, the parent rights are not immediately applied..
I doubt the asker's use of "move" really means the command move, as that would mean the file would only be placed in the first user's folder in the list as the source file will not exist for the second user.
Thanks for all the input and sorry for the delay

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial