Powershell Test Path Behavior

I am trying to write a script in powershell to batch convert video files.

The way I intend to use this is to go to some folder full of video files and run it. It uses a conversion program that can be run in "command line mode" (named handbrake) and saves files with "-android" added to them before the file extension. For example, if I have a file called video1.avi in ​​a folder, after running the script, the folder has 2 files: video1.avi and video1-android.avi

The reason I want to do this is to check that for each video file there is a converted version (with the addition of -android to the name). And if so, skip the conversion for this file.

And here I have problems. The problem is the behavior of Test-Path (the cmdlet I use to check if the file exists).

What happens if the video file has an β€œunusual” name (for example, in my case this video is [long] .avi) Test-Path always returns False if you try to check if this file exists.

A simple way to check this, for example, for this:

Go to an empty folder, run Notepad to create a file with "[" in its name:

&notepad test[x].txt

Save file

then do the following:

Get-ChildItem | ForEach-Object {Test-Path $_.FullName}

This does not return true! It is right? Well, this is not the case if the file has a β€œ[” in its name (I have not checked any other special characters)

I realized that if you avoid the "[" and "]", it works

Test-Path 'test`[x`].txt'

returns true.

? : BaseName , "-android.avi" , .

,

+5
2

PowerShell Path, . , PowerShell * , [ ] . about_Wildcards.

, , -LiteralPath. [ ] , :

Get-ChildItem | ForEach {Test-Path -LiteralPath `
                         "$([io.path]::ChangeExtension($_.FullName,'avi'))"}

FYI, , Get-ChildItem Test-Path , , LiteralPath "PSPath", PSPath FileInfo, Get-ChildItem. LiteralPath (er PSPath) " ".

+8
 dir | % {test-path "$($_.Name)-android.avi"}
+1

All Articles