Solved XP CMD Batch Check Existense Of 2No Files Within Diff Paths

September 12, 2020 at 01:49:28
Specs: windows xp home edit, 384 MB of ram
Hi Can someone help me with this,
I am trying to write a batch file that checks for the existence of 2 Number files but the files reside in different paths for example:

%SystemDrive%\MyFile.DLL
& %SystemRoot%\Myfile.DLL
It would be nice if files were different but for the sake of simplicity/ease how would you batch check for both files. I know you could use if exist then branch and check for the second file, I am looking for a more elegant way if possible.
In Laymans terms:
if [Path]\thisFile & [Path]\thatFile exist move onto next bit of code

Could someone provide code and explain how it works?

P.S. The best I can come up with is this but not sure how to write ELSE into it if the two files are found?
for /d %%i in ("%SystemDrive%\File1" "%Systemroot%\System32\file2") do if not exist %%i goto NOEXIS (But would like ELSE bolted on here if possible if files exist)
:NOEXIS
Do-some-code

Thanks in advance!
Much oblighed
David

message edited by jackngill


See More: XP CMD Batch Check Existense Of 2No Files Within Diff Paths


✔ Best Answer
September 15, 2020 at 20:12:48
Ah, I did really screw up the 0 and 1 thing, had them reversed, which you admirably figured out forthwith.
Some wires crossed in your last rendition, however.. Not to elaborate, I'll just post my intended code "from scratch" as should work per my tests.
@echo off&setlocal
set spaths="%systemroot%" "%systemdrive%"
set target="myuke.pdf"
for %%a in (%spaths%) do call :xx %%a %target%
rem %xx% possible outcomes: 00 (both) 01 10 11 (neither), and work from there.
echo proceeding to label :%xx%
goto :%xx%
goto :eof

:xx
echo testing: dir /b "%~1\%~2"
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
goto :eof

:11
echo none exist, can't continue
goto :xit
:01
echo first path has target, second fails
goto :xit
:10
echo second path has target, first does not
goto :xit
:00
echo success, both paths have target. proceed to wreak havoc
echo ...
goto :xit


:xit
echo done testing %spaths% for %target%

My sincere apologies for the zero/one reversal. Also for abusing "xx", casting it both as a variable and a legit label, which is definitely bad practice inviting much confusion. Above script worked in my tests. I'll leave the crappy version (above) intact, and insert corrected below, substiting only the name of the subroutine :xx to be :test-exists:
@echo off&setlocal
set spaths="%systemroot%" "%systemdrive%"
set target="myUKE.pdf"
for %%a in (%spaths%) do call :test-exists %%a %target%
rem %xx% possible outcomes: 00 (both) 01 10 11 (neither), and work from there.
echo proceeding to label :%xx%
goto :%xx%
goto :eof

:test-exists
echo testing: dir /b "%~1\%~2"
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
goto :eof

:11
echo none exist, can't continue
goto :xit
:01
echo first path has target, second fails
goto :xit
:10
echo second path has target, first does not
goto :xit
:00
echo success, both paths have target. proceed to wreak havoc
echo ...
goto :xit


:xit
echo done testing %spaths% for %target%

apologies, and thanks for your patience thus far.


#1
September 12, 2020 at 08:28:11
it basically if exist "file" (do things if true) else (do things if not true)

you can leave the else part of of it for simplicity.

anyways here is the code:

@echo off
if not exist "%SystemDrive%\MyFile.DLL" (
	if not exist "%SystemRoot%\Myfile.DLL" (
		goto :NOEXIS
	)
)
echo either one or both exist&pause>nul
exit/b
:NOEXIS 
echo both do not exist&pause>nul

my system specs: https://www.userbenchmark.com/UserR...


Reply ↓  Report •

#2
September 12, 2020 at 12:39:55
And what about using the command Where.exe /?

Where.exe /? : Locate and display files in a directory tree.
The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.

further reading and help about the command Where /?

Example :

Where /R %systemroot% "runas*" /F

Give me as result :

"C:\Windows\System32\runas.exe"
"C:\Windows\System32\en-US\runas.exe.mui"
"C:\Windows\System32\fr-FR\runas.exe.mui"
"C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\RunAsHelper.psm1"
"C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\ar-SA\RunAsHelper.strings.psd1"
"C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\fr-FR\RunAsHelper.strings.psd1"
"C:\Windows\WinSxS\x86_microsoft-windows-runas.resources_31bf3856ad364e35_10.0.17134.1_en-us_3b34f82eafeb8f32\runas.exe.mui"
"C:\Windows\WinSxS\x86_microsoft-windows-runas.resources_31bf3856ad364e35_10.0.17134.1_fr-fr_ddb7cb11a2e49739\runas.exe.mui"
"C:\Windows\WinSxS\x86_microsoft-windows-runas_31bf3856ad364e35_10.0.17134.1_none_e94a3a180e3a575b\runas.exe"
"C:\Windows\WinSxS\x86_microsoft.powershel..resources.resources_31bf3856ad364e35_10.0.17134.112_ar-sa_72d53c0686c3eaf7\RunAsHelper.strings.psd1"
"C:\Windows\WinSxS\x86_microsoft.powershel..resources.resources_31bf3856ad364e35_10.0.17134.112_fr-fr_a9f7b2693ebf7cde\RunAsHelper.strings.psd1"
"C:\Windows\WinSxS\x86_microsoft.powershel..resources.resources_31bf3856ad364e35_10.0.17134.1_ar-sa_768e0aa499f12fee\RunAsHelper.strings.psd1"
"C:\Windows\WinSxS\x86_microsoft.powershel..resources.resources_31bf3856ad364e35_10.0.17134.1_fr-fr_adb0810751ecc1d5\RunAsHelper.strings.psd1"
"C:\Windows\WinSxS\x86_microsoft.powershell.dscresources_31bf3856ad364e35_10.0.17134.112_none_2017d5f63b3a02b0\RunAsHelper.psm1"
"C:\Windows\WinSxS\x86_microsoft.powershell.dscresources_31bf3856ad364e35_10.0.17134.1_none_23d0a4944e6747a7\RunAsHelper.psm1"

message edited by Hackoo


Reply ↓  Report •

#3
September 13, 2020 at 05:26:59
@ Hi Hackoo & Hidde663,

Many thanks for your replies, yes I see how the if not exist is structured to suit my particular situation. I am not familair with where.exe program though. I think the "where" program seems best suited is to find a file & report on its path. Do you have a download link for it to investigate further please?

I managed to get the For Command to work like so: (however with 1No Caveat)

for /d %%i in ("%SystemDrive%\File1.Dll" "%SystemRoot%\System32\File2.TXT") do if not exist %%i (goto NOEXIS) ELSE (if exist %%i goto EXISDO)

Then just setup the NOEXIS & EXISDO labels with relevant commands.

The caveat is that the above code will fail if one of the files is missing it will goto NOEXIS label (Both files need to exist) & I am not sure if I have written the code in the correct format e.g. indented etc

Anyways many thanks for replies its always good to get other viewpoints!

Much oblighed

David


Reply ↓  Report •

Related Solutions

#4
September 13, 2020 at 20:32:43
Ha! I'd never heard of the 'where' command. Learned something! But I don't really see much difference from 'DIR'. I'm still kind of confused this post. My understanding so far: look for two files (with same name I guess) in two diff. paths, %systemdrive%, %systemroot%. Of four possibilities: neither exist, one exists, the other exists, or both exist. You could do this:
:: begin script trial-only
@echo off&setlocal
set spaths="%systemroot%" "%systemdrive%"
set target="myfile.dll"
set xx=
rem go through two, or more paths as set by %spaths%
for %%a in (%spaths%) do call :xx %%a %target%
echo result: %xx%
goto :eof

:xx
rem remove following 'echo' line after checking.
echo debug display, testing using: dir /b "%~1\%~2"
rem suppress all output from DIR both result and errmsg: >nul 2>&1
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
rem --------- end prototype script
this seemed to work in my limited tests.
%xx% possible outcomes for two paths: 00 (both) 01 10 11 (neither), and work from there. perhaps as followup:
goto :%xx% with subsequent labels :11 :00 :01 :10 each with code for handling each eventuality.

message edited by nbrane


Reply ↓  Report •

#5
September 14, 2020 at 05:45:09
Hi nbrane,

Thanks for getting back with your alternative option. Yes you are right re - Of four possibilities: neither exist, one exists, the other exists, or both exist. I have used your code as close as I could re below. Here is what I did I copied a random file "MyUKE.pdf" to systemroot & systemdrive then ran the code below based on your code shown below
::=====================Start=====================::
@echo off&setlocal
set spaths="%systemroot%" "%systemdrive%"
set target="MyUKE.pdf"
set xx=
rem go through two, or more paths as set by %spaths%
for %%a in (%spaths%) do call :xx %%a %target%
echo result: %xx%
goto :eof
:xx
rem remove following 'echo' line after checking.
echo debug display, testing using: dir /b "%~1\%~2"
rem suppress all output from DIR both result and errmsg: >nul 2>&1
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
pause
:eof
@Exit
::===================End======================::
I got back (CMD screen) the following:
debug display, testing using: dir /b "C:\Windows\MyUke.pdf"
Press any key to continue...

Is this what would be expected as I am not fully understanding your code? You have written "00" = Both files exist in e.g systemroot & systemdrive? "11" = Neither in systemroot or systemdrive. What does 01 = ? & 10 = ? Does 01 = only exists in systemroot & "10" only exists in systemdrive? How would you code these outcomes out for example?
By the way interesting way of looking at it though.

Best Regards,

David

message edited by jackngill


Reply ↓  Report •

#6
September 14, 2020 at 18:07:25
"What does 01 = ? & 10 = ? Does 01 = only exists in systemroot & "10" only exists in systemdrive" Yes, that's correct, depending on the order that you place the two paths in %spaths%. variable.
The coding of the outcomes was in "followup", which admittedly I might have made clearer. The value of %xx% (00, 01, 10, 11) is used as a goto (to dodge a bunch of IFs ELSEs). For each of the four, create a label, and code for each contingency in it's associated label, as in following example (after %xx% has been established):
rem %xx% is either 00, 01. 10, or 11
echo proceeding to batch label :%xx%...
rem this could also be a call, as long as you end each with segment with "exit /b" instead of "goto :xit"
goto :%xx%

:11
echo none exist, can't continue
goto :xit
:01
echo second path has target, first does not
goto :xit
:10
echo first path has target, second fails
goto :xit
:00
echo success, both paths have target. proceed to wreak havoc
...
goto :xit
::------------ end snippet
Of course, I have no idea what you do with each condition. This just sets up the framework to implement it. label :xit is not defined, it's just, as indicated, a exit point.

message edited by nbrane


Reply ↓  Report •

#7
September 15, 2020 at 05:34:32
@ nbrane

Before proceeding thanks for your time and patience with this coding issue I'm having.
I have elected to use a file called MyUKE.pdf for testing purposes & have got this far, maybe you can diagnose what I am doing wrong, used the following code with MyUKE.pdf

@echo off&setlocal
set spaths="systemroot" "systemdrive"
%echo spaths%
set target="MyUKE.pdf"
echo %target%
set xx=
rem go through two, or more paths as set by %spaths%
for %%a in (%spaths%) do call :xx %%a %target%
set xx=%xx%%errorlevel%
goto xx

:xx
rem suppress all output from DIR both result and errmsg: >nul 2>&1
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
goto %xx%

:00 (ran 1st time with :11 label)
echo none exist, can't continue neither file exists in both paths
pause
goto Xit

:01
echo second path has target, first does not systemdrive has it systemroot does Not
pause
goto xit

:10
echo first path has target, second fails systemroot has file systemdrive does Not
pause
goto Xit

:11 (ran 1st time with label :00)
echo success, both paths have target. proceed to wreak havoc
pause
goto Xit

:Xit
@exit

Starting process of elimination
1) This is with MyUKE.pdf in the respective folders "systemroot" "systemdrive" in that order. I ran the CMD & it reported both files not within "systemroot" "systemdrive" so I swopped the labels 00 & 11 & that worked for both files being in both folders but I am assuming 11 = not in dirs.
2) So as a further check then renamed both files MyUKE.old & ran again but reported back same case defaulted to label :11 (as in 1) above) which is incorrect

Here is what was reported within CMD window 2nd attempt post label swop:
::===================Start===================::
"MyUKE.pdf"
The system cannot find the batch label specified -1
Success, both paths have target. Proceed to write havoc.
paktc... (= press any key to continue)
::===================End===================::
have considered switching echo on instead of off will try in the meantime to see if error can be seen within action reported.

Assuming :11 = both files not in "systemroot" "systemdrive"
:00 = both files presant in "systemroot" "systemdrive"
"systemroot" is the first integer 0 or 1 (yes, no)
"systemdrive" is the second integer 0 or 1 (yes, no)

Is there by chance a bolt on program that would facilitate "OR" & "AND" I suppose "AND" comprises 2 "IF's" stacked IFs just wondering?
? Of course, I have no idea what you do with each condition. Ans the 1st 3 labels would default to error message & exit
The 00 option would then go onto further progressive code

Best Regards

David

message edited by jackngill


Reply ↓  Report •

#8
September 15, 2020 at 20:12:48
✔ Best Answer
Ah, I did really screw up the 0 and 1 thing, had them reversed, which you admirably figured out forthwith.
Some wires crossed in your last rendition, however.. Not to elaborate, I'll just post my intended code "from scratch" as should work per my tests.
@echo off&setlocal
set spaths="%systemroot%" "%systemdrive%"
set target="myuke.pdf"
for %%a in (%spaths%) do call :xx %%a %target%
rem %xx% possible outcomes: 00 (both) 01 10 11 (neither), and work from there.
echo proceeding to label :%xx%
goto :%xx%
goto :eof

:xx
echo testing: dir /b "%~1\%~2"
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
goto :eof

:11
echo none exist, can't continue
goto :xit
:01
echo first path has target, second fails
goto :xit
:10
echo second path has target, first does not
goto :xit
:00
echo success, both paths have target. proceed to wreak havoc
echo ...
goto :xit


:xit
echo done testing %spaths% for %target%

My sincere apologies for the zero/one reversal. Also for abusing "xx", casting it both as a variable and a legit label, which is definitely bad practice inviting much confusion. Above script worked in my tests. I'll leave the crappy version (above) intact, and insert corrected below, substiting only the name of the subroutine :xx to be :test-exists:
@echo off&setlocal
set spaths="%systemroot%" "%systemdrive%"
set target="myUKE.pdf"
for %%a in (%spaths%) do call :test-exists %%a %target%
rem %xx% possible outcomes: 00 (both) 01 10 11 (neither), and work from there.
echo proceeding to label :%xx%
goto :%xx%
goto :eof

:test-exists
echo testing: dir /b "%~1\%~2"
dir /b "%~1\%~2" > nul 2>&1
set xx=%xx%%errorlevel%
goto :eof

:11
echo none exist, can't continue
goto :xit
:01
echo first path has target, second fails
goto :xit
:10
echo second path has target, first does not
goto :xit
:00
echo success, both paths have target. proceed to wreak havoc
echo ...
goto :xit


:xit
echo done testing %spaths% for %target%

apologies, and thanks for your patience thus far.

Reply ↓  Report •

#9
September 16, 2020 at 12:16:57
@nbrane,

Indeed NO, apologies are extended to you, You are the one that has been patient & long suffering as I am not very good at batch scripting so I need a lot of schooling and explanation which to some would consider simple and yes it is very easy for me to get my wires crossed. I have tried out your new version & I can confirm that it works at my end also Many Thanks!!!

Yours is my opinion is the most elegant way of undertaking this task. I have also been looking @ "if and or" in batch files today to help and aid things. I have also come up with an albeit "Clunky" alternative with 3 of the 4 outcomes going to a separate label & the correct outcome going to a :OKAY label & the alternative branching to an :ERROR label. The :OKAY label could be the :ONWARD Label if you just wanted to check for errors and proceed and the pause commands are there to view in CMD window Anyway here it is for anyone who visits and wants it as an alternative.

@echo off&SETLOCAL
if /i exist %systemdrive%\MyUKE.pdf if /i exist %systemroot%\MyUKE.pdf SET FILE=Y0
if /i not exist %systemdrive%\MyUKE.pdf if /i not exist %systemroot%\MyUKE.pdf SET FILE=F1
if /i exist %systemdrive%\MyUKE.pdf if /i not exist %systemroot%\MyUKE.pdf SET FILE=F2
if /i not exist %systemdrive%\MyUKE.pdf if /i exist %systemroot%\MyUKE.pdf SET FILE=F3
if "%FILE%"=="Y0" (goto :OKAY)
if "%FILE%"=="F1" (goto :ERROR) 
if "%FILE%"=="F2" (goto :ERROR) 
if "%FILE%"=="F3" (goto :ERROR)
goto EOF

:OKAY
cls
ECHO Both Files exist in %systemdrive%\ drive and %systemroot%
pause
goto :ONWARD

:ERROR
cls
if "%FILE%"=="F1" Echo both files are missing in both %systemroot% and %systemdrive%
if "%FILE%"=="F2" Echo file missing in %systemroot%
if "%FILE%"=="F3" Echo File is missing in %systemdrive%
ENDLOCAL
pause
goto EOF

:ONWARD
cls
ECHO CARRY ON WITH REST OF CODING
pause
got EOF

EOF
@EXIT

Again Many thanks for help & assistance in this matter

Much oblighed

David

message edited by jackngill


Reply ↓  Report •

Ask Question