Move path in PowerShell

SAM2009
SAM2009 used Ask the Experts™
on
Hi,

In PowerShell I rund this cmd:

move  "C:\Windows\System32\dns\$($line)_$($Year)-$($Month)-$($Day).txt" "C:\temp\DNS\Exports\${line}_$Year-$Month-$Day.txt"

Why it looks not taking the value of the variables because it gives me this error:

move : Could not find a part of the path.
At C:\temp\Export-DNS.ps1:21 char:1
+ move "C:\Windows\System32\dns\$($line)_$($Year)-$($Month)-$($Day).txt" "C:\temp\ ...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sam JacobsCitrix Technology Professional / Director of TechDev Services, IPM

Commented:
The second part of the command needs to be slightly corrected … from this:
 
"C:\temp\DNS\Exports\${line}_$Year-$Month-$Day.txt"

Open in new window

to  this:
 "C:\temp\DNS\Exports\$($line)_$($Year)-$($Month)-$($Day).txt"

Open in new window

Jose Gabriel Ortega CastroCEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
Awarded 2018
Distinguished Expert 2018

Commented:
How are you defining these variables?
$year
$month
$day?
$line?
etc
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
I've made the experience that PowerShell is usually right; if it says it can't find the path, it can't find the path.
This will give you the path that's not working:
# ...
$source = "C:\Windows\System32\dns\$($line)_$($Year)-$($Month)-$($Day).txt"
$destination = "C:\temp\DNS\Exports\${line}_$Year-$Month-$Day.txt"
If (Test-Path -Path $source -PathType Leaf) {
	If (Test-Path -Path ([IO.Path]::GetDirectoryName($destination)) -PathType Container) {
		Move-Item -Path $source -Destination $destination
	} Else {
		Write-Warning "Destination folder '$($destination)' not found!"
	}
} Else {
	Write-Warning "Source file '$($source)' not found!"
}

Open in new window


Sam Jacobs,
the ${...} syntax actually works. Wrapping a variable name in curly brackets allows you to define and access variable names that otherwise wouldn't be allowed; you can, for example, use a '+' as variable:
$line = 'Foo'
"Line: ${line}"
${+} = 'bar'
"+: ${+}"

Open in new window

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
Yes the msg error is correct because it did not the value in the variable but consider it as string...

But the way I type is correct too that's why I don't understand. Is because the I use: Move

Is someone can try a cmd with MOVE and add a variable in the path? Tell me it works please.
Sam JacobsCitrix Technology Professional / Director of TechDev Services, IPM

Commented:
oBdA … I keep learning new stuff from you every day.

Thanks.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Yes, you can use variables in a string like you did.
Yes, that script will work, provided all required elements exist.
The script above should give you the exact name PS is trying to access, so which path is it that can't be found, source or target?
What is the name that can't be found, and how does it differ from what you expected it to be?

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial