3

This is my code and it doesn't work:

$brisanje=dat.txt, dat2.txt
Remove-Item $brisanje

The error that I get here is

Remove-Item : Cannot find path 'C:\Users\stefan\Desktop\brisanjedat\dat.txt, dat2.txt' because it does not exist.

But when I write it like this it works like a charm:

Remove-Item dat.txt, dat2.txt

I've been stuck with this problem for hours, any solutions?

2
  • $brisanje="dat.txt","dat2.txt" Commented Jan 8, 2017 at 13:33
  • thank you, it works Commented Jan 8, 2017 at 13:36

2 Answers 2

5

The PowerShell parser has two modes, depending on context.

When in an argument mode context (like in the case of Remove-Item dat.txt,dat2.txt), it treats the bare words as a list of expandable strings.

In expression mode (the default), a bare word at the start of an expression is treated like a command and powershell will attempt to resolve it, which is why you see the error.

Use quotes to make sure the parser knows you mean dat1.txt,dat2.txt to be a string array:

$brisanje="dat.txt","dat2.txt"

See the output from Get-Help about_Parsing for more information

Sign up to request clarification or add additional context in comments.

Comments

1

It's also a good idea to use dot (.) sourcing in your code too. Shells and Scripts have different scopes, and when you dot source variables you combine the two. I dot source everything, especially if I'm using it over and over again or in different environments.

cd C:\Users\stefan\Desktop\brisanjedat\ $brisanje= ".\dat.txt",".\dat2.txt" Remove-Item $brisanje

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.