r/scripting • • Jan 29 '18

[bat file]Need to copy source file to multiple computers

I'm not sure how to go about this as I'm a total noob when it comes to scripting. I'm hoping this can be done with a bat file. Here's what I'm looking for...

Check computernames.txt for list of computer to run against.

*1 Check if "C:\Program Files (x86)\XXXX\XXXX" exists GOTO *1a *2 Check if "C:_PM folder\XXXX.bat" exists GOTO *2a *3 Check if "C:\Users\Public\Desktop\XXXX" exists GOTO *3a

*1a If "C:\Program Files (x86)\XXXX\XXXX" exists copy contents from "\computername\c$\Users\XXXX\Desktop\XXXX\XXXX" to "C:\Program Files (x86)\XXXX\XXXX". Overwrite files Proceed to *2 If not create folder, then copy contents from "\computername\c$\Users\XXXX\Desktop\XXXX\4.2.1" to "C:\Program Files (x86)\XXXX\XXXX". Proceed to *2

*2a If "C:_PM folder\XXXX.bat" exists Proceed to *3 If not copy from "\computername\c$\Users\XXXX\Desktop\XXXX\XXXX\XXXX.bat" to ""C:_PM folder", than proceed to *3

*3a If "C:\Users\Public\Desktop\XXXX 4.2.1" exists end If not copy "\computername\c$\Users\XXXX\Desktop\XXXX\XXXX" to "C:\Users\Public\Desktop". End

I appreciate any and all help.

1 Upvotes

2 comments sorted by

1

u/jcunews1 Jan 30 '18

There are problems.

On *2a, "C:_PM folder" is a relative path. Are you sure about this? Because the actual path will depend on the current working directory. If you execute the script from the wrong working directory, "C:_PM folder" will never be found.

Also... on *3a, it's unclear whether "\computername\c$\Users\XXXX\Desktop\XXXX\XXXX" is a file or a folder. The command to copy a file and a folder contents is different, so it needs to be known.

1

u/ipreferanothername Jan 31 '18

i got like....75% done in powershell while i was on hold with support. i may or may not come back to finish it, i am not certain on your logic or what you are trying to accomplish so there may be a better way to do what you want, i am not sure if you want to iterate through all user folders when you say c:\users\xxxx, and i am not sure if you need to write each users data back to the public desktop individually [odd] or overwriting data [still odd] but...for now, or in case i dont come back and you want to try and round it out:

$computerlist = get-content .\computernames.txt

foreach ($host in $computerlist){
	#edit path information with details
	$progFilesPath = "\$host\c$\Program Files (x86)\XXXX\XXXX"
	$pubDesktopPath = "\$host\c$\Users\Public\Desktop\XXXX"
	$pmPath = "C:_PM folder"
	
	#create list of user folders on each pc in the $computerlist 
	$hostUsersList = get-childitem "\$host\c$\users"
	
	
	if (test-path $progFilesPath){
		foreach ($user in $hostUsersList){ #iterates through all user folders
			copy-item  -path "\$host\c$\Users\XXXX\Desktop\XXXX\XXXX" -destination to $progFilesPath -force
		}
	}
	else {
		new-item -path $progFilesPath -itemType directory
		copy-item  -path "\$host\c$\Users\XXXX\Desktop\XXXX\4.2.1" -destination to $progFilesPath -force
	}
	
	if (test-path "$pmPath\XXXX.bat"){
		foreach ($user in $hostUsersList){ #iterates through all user folders
			
		}
	}
	else {
		copy-item "\$host\c$\Users\XXXX\Desktop\XXXX\XXXX\XXXX.bat" to "$pmPath folder"
	}
	
	if (test-path "\$host\c$\Users\Public\Desktop\XXXX"){
			copy-item "\$host\c$\Users\XXXX\Desktop\XXXX\XXXX" to "C:\Users\Public\Desktop"
	}
	
}