I am new to Powershell, and I want to check if specfic path exits in Android emulator, I need to use it in PowerShell. I try to print the result of adb shell ls /data/local/tmp/
, it will list files, and if the path does not exist, it will show No such file, but how to detect it? Any one knows?
Asked
Active
Viewed 1,660 times
1

newszer
- 440
- 1
- 4
- 23
-
http://codewiki.wikidot.com/shell-script:if-else – pskink Jan 17 '18 at 06:14
-
I need to run in powershell, yours answer is for shell – newszer Jan 17 '18 at 06:17
-
you have powershell installed on your android device? – pskink Jan 17 '18 at 06:20
-
No, I use adb shell command – newszer Jan 17 '18 at 06:20
-
so you use unix shell, read the link i posted above – pskink Jan 17 '18 at 06:21
-
I think the link does not contain the answer to my question, thank you anyway – newszer Jan 17 '18 at 06:25
-
So: https://stackoverflow.com/questions/11526285/how-to-count-objects-in-powershell ? It may help to edit your question with what you've written already. – Morrison Chang Jan 17 '18 at 06:28
-
so what do you want to achieve actually? to test on your android device if the given directory exist? if so, what then? – pskink Jan 17 '18 at 06:31
-
Thank you Morrison, I try your approach, it works in most cases, because the count I get when file does exits is 2, but when the path exists and it contain 1 file the count is also 2. So I think I need to some deeper detect. – newszer Jan 17 '18 at 06:39
-
Hi pskink, yes I want to detect whether the path exist in android device, but it is needed to detect it in an Powershell script. – newszer Jan 17 '18 at 06:40
2 Answers
1
Actually I have found my way to check it
$checkPathExists = (adb shell ls $pathToCheck) | Out-String
if($checkPathExists -match 'No such file or directory')
{
Write-Host "$pathToCheck path does not exist. Try to create it ..." -ForegroundColor Red
adb shell mkdir $pathToCheck
if(!$?)
{
Write-Host "Creating $pathToCheck folder failed!" -ForegroundColor Red
exit 1
}
}

newszer
- 440
- 1
- 4
- 23
0
Validaitng paths are done using the Test-Path cmdlet on Windows/OSX/Linux.
Get parameter, examples, full and Online help for a cmdlet or function
(Get-Command -Name Test-Path).Parameters
Get-help -Name Test-Path -Examples
Get-help -Name Test-Path -Full
Get-help -Name Test-Path -Online
Then just use If/Then, Try/Catch to perform actions based on your needs.

postanote
- 15,138
- 2
- 14
- 25