XCopy to user file batch

June 8, 2010 at 02:21:21
Specs: Windows XP
I am currently trying to use Xcpy with a batch file to send a file from my PC to a number of PCs within the organisation. I can get this working however I was wondering if someone could tell me how to specify sending the file to only the current logged on user profile on the remote PC's. The script is...

@echo on
for %%a in (dt-162hs-002004; dt-162hs-002082; ) do (
xcopy /i c:\emails.TXT \\%%a\\C$\"DOCUMENTS AND SETTINGS\<Current User>\APPLICATION DATA\MICROSOFT\TEMPLATES")


See More: XCopy to user file batch

Report •


#1
June 8, 2010 at 07:41:50
Untested:
@echo on
for %%a in (dt-162hs-002004; dt-162hs-002082; ) do (
	for /f "tokens=2 delims=\" %%b in ('wmic /node:"%%a" ComputerSystem get username') do (
		SET current_user=%%b
	)
	xcopy /i c:\emails.TXT \\%%a\\C$\"DOCUMENTS AND SETTINGS\%current_user%\APPLICATION DATA\MICROSOFT\TEMPLATES")
)

Sorry :-(
Just tested, didn't work when I was logged on to the remote PC through Remote Desktop. WMIC should provide logged on username, for all sessions, just have to find the correct alias/path and object! I'll keep looking...

In the interim, you could try:

@echo on
for %%a in (dt-162hs-002004; dt-162hs-002082; ) do (
	for /f "tokens=2 delims=\" %%b in ('wmic /node:"%%a" ComputerSystem get username') do (
		SET current_user=%%b
	)
	if "%current_user%" neq "" (
		xcopy /i c:\emails.TXT \%%a\\C$\"DOCUMENTS AND SETTINGS\%current_user%\APPLICATION DATA\MICROSOFT\TEMPLATES"
	) else (
		@echo No on logged onto console at %%a
	)
)


Report •

#2
June 8, 2010 at 08:07:01
Thanks for this orangeboy, however when I run the bat file it asks if I want to create a file or directory within docs & settings rather than writing the file to the location within the user profile. It seems to be picking up the current user but not putting the file in the users folder within App Data.

Any more help will be greatly appreciated....


Note.....This was posted before your second attempt Orangeboy, which isnt working either.....


Report •

#3
June 8, 2010 at 10:47:05
I didn't even look at the construct of the xcopy command.
How about instead of:
\%%a\\C$\"DOCUMENTS AND SETTINGS\%current_user%\APPLICATION DATA\MICROSOFT\TEMPLATES"

trying:
"\\%%a\C$\DOCUMENTS AND SETTINGS\%current_user%\APPLICATION DATA\MICROSOFT\TEMPLATES"

I may not have copied the original code correctly concerning xcopy...


Report •

Related Solutions

#4
June 8, 2010 at 12:25:41
for %%a in (dt-162hs-002004; dt-162hs-002082; ) do (
for /f "tokens=3" %%b in ('net session %%a ^| find /i "user name"') do copy c:\emails.TXT \\%%a\\C$\"DOCUMENTS AND SETTINGS\%%b\APPLICATION DATA\MICROSOFT\TEMPLATES"
)
::--- end
i used copy instead of xcopy, and NET SESSION instead of
WMIC to get username. Not knowledgable about unc's, but I'm not sure what the second set of dbl slashes does (after
%%a), or what C$ is. My test just used:
"\\%cpu%\c\documents and settings..." cause that's how i have mine set up.

Report •

#5
June 8, 2010 at 15:58:23
"It seems to be picking up the current user but not putting the file in the users folder within App Data."

Every one is missing the fact that this is a Windows 7 or Windows Vista box. The code provided will only work on XP boxes. With that being said are you running any XP boxes on your network or are they all Windows Vista or newer? If you have a mix then you will need an if to determine the OS they are using so it copies to the right path.

ver | find "Version 5." > nul
if %ERRORLEVEL% == 0 (
goto Windows_XP
) else (
goto Windows_6_7
)

so your code might look like...

@echo off

for %%a in (dt-162hs-002004; dt-162hs-002082; ) do (
	for /f "tokens=2 delims=\" %%b in ('wmic /node:"%%a" ComputerSystem get username') do (
		SET current_user=%%b
	)
	if "%current_user%" neq "" (
		ver | find "Version 5." > nul
                if %ERRORLEVEL% == 0 (
                      set Userpath="DOCUMENTS AND SETTINGS\%current_user%\APPLICATION DATA\MICROSOFT\TEMPLATES"
                ) else (
                      set Userpath="USERS\%current_user%\APPDATA\LOCAL\MICROSOFT\TEMPLATES"
               )
               xcopy /i c:\emails.TXT \%%a\\C$\%Userpath%
	) else (
		@echo No on logged onto console at %%a
	)
)

This is untested but you get the idea.


Report •

#6
June 8, 2010 at 18:18:37
ace_omega: Every one is missing the fact that this is a Windows 7 or Windows Vista box. The code provided will only work on XP boxes.
Microsoft Windows [Version 6.1.7600]

C:\>type Users\hw.txt
The system cannot find the file specified.

C:\>echo Hello World > "Documents and Settings\hw.txt"

C:\>type Users\hw.txt
Hello World

wetfarm:
Windows can have more than one logged on user, and it can have no users logged in. Technically at this moment, your PC probably has three "users" logged in at this moment. You, Local Service, and Network Service.

That said, registry key HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName should tell you who last logged on the console.


Report •

#7
June 9, 2010 at 05:38:07
I am running this on a completely Windows XP network, but as yet none of the codes provided have worked.....I have also tried sevral variations but as yet I must be missing something.
If I sepcify the complete path including the actual userprofile then it copies the file fine, however when the code has to find the current user then nothing happens....

Report •

#8
June 9, 2010 at 07:10:52
Try adding an echo, to see what your script is trying to work with:
for %%a in (dt-162hs-002004; dt-162hs-002082; ) do (
for /f "tokens=3" %%b in ('net session %%a ^| find /i "user name"') do (
echo computer:[%%a] user:[%%b]
echo sentence:[copy c:\emails.TXT \\%%a\\C$\"DOCUMENTS AND SETTINGS\%%b\APPLICATION DATA\MICROSOFT\TEMPLATES"

copy c:\emails.TXT \\%%a\\C$\"DOCUMENTS AND SETTINGS\%%b\APPLICATION DATA\MICROSOFT\TEMPLATES"
)
)
do your computers actually have those semicolons at end of their designation? (dt-162hs-002004;)


Report •

#9
June 9, 2010 at 07:15:31
nbrane: net session %%a
That'd work if the user had an open share to the computer this script runs from, but what are the chances of that?

Report •

#10
June 9, 2010 at 07:52:20
< Deleted I just realized it was Razor who posted the version number not the OP >

Report •

#11
June 9, 2010 at 10:59:49
ok, back to the wmic method then, might add skip=1 and add a space to delims:

for /f "skip=1 tokens=2 delims=\ " %%b in ('wmic /node:"%%a" ComputerSystem get username') do (
echo computer:[%%a] user:[%%b]
)


Report •


Ask Question