#1 08 Jun 2017 22:36

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

How to check the existence of a file that includes a variable string??

I'm not sure if I worded the title correctly.

The following works

if exist "%appdata%\Spotify\Spotify.exe" echo hi

output

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Windows\system32>if exist "%appdata%\Spotify\Spotify.exe" echo hi
hi

C:\Windows\system32>

However in my batch file if I introduce a variable, or dynamic path as a string it can't check for the existence of that file, even though it was proved above.
Do anyone have a suggestion (other than using a hard-path)

@echo off&cls
goto :start

:: paths to stuff to pin to the taskbar - ordered in the same order
"C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\ONENOTE.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
"%appdata%\Spotify\Spotify.exe"
%appdata%\Spotify\Spotify.exe
"C:\Program Files\Notepad++\notepad++.exe"
"C:\Program Files (x86)\Notepad++\notepad++.exe"
"C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe"

:start
:: read files in list into memory
for /f usebackq^ tokens^=1-3*^ delims^=^" %%a in (%0) do (
  REM echo %%a
  if exist %%a echo found %%a
  REM call :sub_addarray "%%~a" "%%~c"
  if "%%a" equ ":start" goto :exitloop)
:exitloop
pause

output

found C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE
found C:\Program Files (x86)\Microsoft Office\root\Office16\ONENOTE.EXE
found C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE
found C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE
found C:\Program Files (x86)\Notepad++\notepad++.exe
found C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe
Press any key to continue . . .

Last edited by NDog (08 Jun 2017 22:41)


cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 09 Jun 2017 08:55

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,076

Re: How to check the existence of a file that includes a variable string??

I seem to remember experiencing something like this a number of years ago.

I believe I ended up using "setlocal enabledelayedexpansion" with "!" notation for the variables I wanted to expand.  I'm afraid I don't have the script available to refer to in case there were other tricks required.

There might be something that could be done with "call" (with "%%" notation for the variables) -- but I didn't know that trick at the time, hence using "!".


cmd | *sh | Ruby | Chef

Offline

#3 11 Jun 2017 08:59

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

Re: How to check the existence of a file that includes a variable string??

cool thanks

@echo off&cls
goto :start

:: paths to stuff to pin to the taskbar - ordered in the same order
:: office 2016 (o365 install)
"C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\ONENOTE.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
:: techie stuff
"%appdata%\Spotify\Spotify.exe"
"C:\Program Files\Notepad++\notepad++.exe"
"C:\Program Files (x86)\Notepad++\notepad++.exe"
"C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe"

:start
:: read files in list into memory
setlocal enabledelayedexpansion
for /f usebackq^ tokens^=1-3*^ delims^=^" %%a in (%0) do (
  call set _path=%%a
  if exist "!_path!" call :sub_addarray "%%~a" "%%~c"
  if "%%a" equ ":start" goto :exitloop)
:exitloop
setlocal disabledelayedexpansion

pause

:sub_addarray
  echo %1  
  goto :eof

output

"C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\ONENOTE.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"
"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
"C:\Users\ndog\AppData\Roaming\Spotify\Spotify.exe"
"C:\Program Files (x86)\Notepad++\notepad++.exe"
"C:\Program Files (x86)\AutoIt3\SciTE\SciTE.exe"
Press any key to continue . . .

cmd, vbs, ps, bash
autoit, python, swift

Offline

#4 12 Jun 2017 15:17

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,076

Re: How to check the existence of a file that includes a variable string??

Sweet big_smile


cmd | *sh | Ruby | Chef

Offline

Board footer

Powered by FluxBB