Home > Software engineering >  Copy a .txt file with a variable in the title to another folder
Copy a .txt file with a variable in the title to another folder

Time:11-04

I have a simple ping script that produces an output file at the end to a local folder, the file name contains a variable that pulls the host name of the computer its run on and adds it to the file name. I then want to be able to move this file to another file path but wasn't how to do with the variable names .txt file.

$H = hostname 

cd c:/users 

ping ipaddress -n 4 |Foreach{"{0} - {1}" -f (Get-Date),$_} > chosenfolder\$H"DC1!".txt

Any help would be appreciated.

Thanks

Tried using the basic copy-item cmdlet but this didn't work.

CodePudding user response:

If all you are after is to move the file then you can you the mv command in powershell.

$H = hostname 

cd c:/users 

ping ipaddress -n 4 |Foreach{"{0} - {1}" -f (Get-Date),$_} > chosenfolder\$H"DC1!".txt

mv chosenfolder\$H"DC1!".txt C:\<whatever path>
  • Related