Rename a file on multiple PC hostnames from a text file

shaider007 101 Reputation points
2023-09-20T15:13:26.6533333+00:00

I would like to create a batch file wherein I have a text file with multiple PC hostnames, to rename a file with path source: C:\Users%userprofile%\Appdata\Local\test123 to test123_Old on all PCs- with a logfile that will show which PC was failed to do the renaming of file on C:\temp.

I use below command and still in the process to execute it as batch file and adding a command for the logfile.

psexec @C:\hostnames.txt ren C:\Users\%userprofile%\Appdata\Local\test123 test123_Old

#since there are different user IDs from different PC hostnames, I replaced it with %userprofile%, to skip it and just only rename the test123 which is the last part.

Error:

PsExec could not start ren on PC123456:
The system cannot find the file specified:

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,468 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 33,371 Reputation points
    2023-09-20T23:11:53.9233333+00:00

    #since there are different user IDs from different PC hostnames, I replaced it with %userprofile%, to skip it and just only rename the test123 which is the last part.

    To skip what? The userprofile environment variable contains text like C:\Users\UserName so the file name you are trying to process is C:\Users\C:\Users\UserName\Appdata\Local\test123. That won't work. It would use your username, not any user's name on the target pc's.

    There is no ren.exe program for psexec to execute. "Ren" is a command prompt command. You would need to execute cmd.exe.

    To find the individual test123 files, you look through each users folder. Powershell is your best option.

    The code to do that is this.

    Get-ChildItem c:\users | foreach { 
    	$f = get-item ($_.fullname + '\Appdata\Local\test123') -ea SilentlyContinue 
    	if ($f) { 
    		'Renaming {0}' -f $f.fullname 
    		Rename-Item  $f -NewName 'Test123_old' 
    	} 
    }
    

    On a single line running against my test machine it would be this.

    psexec \\test10 powershell.exe -command "Get-ChildItem c:\users | foreach { $f = get-item ($_.fullname + '\Appdata\Local\test123') -ea SilentlyContinue;  if ($f) { 'Renaming {0}' -f $f.fullname; Rename-Item  $f -NewName 'Test123_old'}}
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.