Reputation: 143
I need to search a .txt
file, %temp%\OK-%username%.txt
and compare and echo a response. If file exists do()
, the following command, dir %temp%\OK-%username%.txt | findstr "OK"
, output return is: 23/10/2020 17:10 0 OK-%username%
.
set var=dir %temp%\OK-%username%.txt | findstr "OK"
if %var% EQU "OK-%username%.txt" (
msg "%username%" test
) else (
msg "%username%" test 2
)
Using echo to %var%
I got the following return:
"dir C:\Users\%username%.CAT\AppData\Local\Temp | findstr "OK-%username%"
expected is to return only the file location.
Upvotes: 0
Views: 47
Reputation: 143
An easily way to resolve this question is convert DOS language to Powershell, stay with the following commands, i got better result's with powershell, i hope enjoy.
$var = Get-ChildItem -Path "$env:TEMP" -ErrorAction SilentlyContinue
if ($var.Name -Contains "OK-$env:USERNAME.txt") {
msg "$env:USERNAME" test
} else {
msg "$env:USERNAME" test 2
}
Exit
Upvotes: 0
Reputation: 3264
You are trying to use a Linux/PowerShell style test -- IE one where the IF statement can contain a command and return it's contents.
That isn't possible in CMD
using just IF
and trying to nest the command in one of the evaluated terms will just yield a syntax error.
it is unclear if you want to have this run in a script or directly in the CLI. Although, if I understand your request it will not differ.
You would like to check if "%temp%\OK-%username%.txt" Exists, and take an action depending on whether or not it does this is very straight forward.
SET "_File=%temp%\OK-%username%.txt"
IF EXIST "%_File%" (
msg "%username%" test
) else (
msg "%username%" test 2
)
Upvotes: 1