Jump to content

script to check if file is on the computer


Recommended Posts

Posted

I am trying to create a script which uploads a file to a computer, I want the script called "payload.txt" to check if the file has been uploaded to the  directory specified on the computer.

Posted

How are the files being upload?  Is it a quack command, bat file, vbs or Powershell?

If there are no subfolders and they all end up in the root the the %tmp% folder then in Powershell the command to check is first create an array of the files that should be there.

$myfiles = @("file1.exe", "file2.exe", "file3.txt")

# Then loop through checking them with test-path, in Powershell $env:tmp is the same as %tmp%.

foreach ($file in $myfiles)
{
	if(Test-Path "$env:tmp\$file")
	{
		# Do something if file exists.
		Write-Host ("File {0} exists." -f @($file))
	}
	else
	{
		# Do something if file is missing.
		Write-Host ("File {0} is not present" -f @($file))
	}
}

 

If you use Powershell to do the copying then you can copy each file and and have a condition when it fails to copy a file that way you can check while copying.

$myfiles = @("file1.exe", "file2.exe", "file3.txt")

foreach ($file in $files)
{
	Copy-Item -Path "$rootpathtofiles\$file" -Destination "$env:tmp\$file" -ErrorAction "SilentlyContinue" -ErrorVariable myerror
	if($myerror)
	{
		# Do something about that file not being copied.
		Write-Host ("File {0} did not copy." -f @($file))
	}
	#Else is optional for files that copied, if you just want to continue on then leave out else.
	else
	{
		# Do something when file is successfully copied.
		Write-Host ("File {0} copied successfully" -f @($file))
	}
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...