| | |
Simple problem requiring complex solution in DOS
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 16
Reputation:
Solved Threads: 0
Hi all,
I have written a code in DOS. The program is to find a file in a location and print "File exists" if it is present.
Assume a file named test.txt exists in location D:\
In my sys I have enabled the option in xp-explorer
"Tools->Folderoptions->View->Hide extension for known file types". So if I search for the file with extension test.txt the code is working fine. Where as if i disenable the option the code cannot find the file.
Since I need to run the code in different machines, i need to search for a particular file, regardless of whether the option is enabled or not.
even if i am using "test" for search the code is not locating the file
here is the code:
cd D:\
if exist test.txt (
echo FILE EXISTS
) else (
if exist test (
echo FILE EXISTS
) else (
echo FILE doesn't exists
)
)
Thanks in advance
I have written a code in DOS. The program is to find a file in a location and print "File exists" if it is present.
Assume a file named test.txt exists in location D:\
In my sys I have enabled the option in xp-explorer
"Tools->Folderoptions->View->Hide extension for known file types". So if I search for the file with extension test.txt the code is working fine. Where as if i disenable the option the code cannot find the file.
Since I need to run the code in different machines, i need to search for a particular file, regardless of whether the option is enabled or not.
even if i am using "test" for search the code is not locating the file
here is the code:
cd D:\
if exist test.txt (
echo FILE EXISTS
) else (
if exist test (
echo FILE EXISTS
) else (
echo FILE doesn't exists
)
)
Thanks in advance
Never Say Never
It is working me too. 
use the @echo off to hide the your program's details

use the @echo off to hide the your program's details
@ echo off cd D:\ if exist test.txt ( echo FILE EXISTS rem add a pause command here,to see the result Pause ) else ( if exist test ( echo FILE EXISTS Pause ) else ( echo FILE doesn't exists Pause ) )
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
-=For airbourne=-
ok,you're right
reference for 'if'
Remarks
If the condition specified in an if clause is true, the command that follows the condition is carried out. If the condition is false, the command in the if clause is ignored and the command executes any command that is specified in the else clause.
When a program stops, it returns an exit code. To use exit codes as conditions, use errorlevel.
If you use defined, the following three variables are added to the environment: %errorlevel%, %cmdcmdline%, and %cmdextversion%.
%errorlevel% expands into a string representation of the current value of the ERRORLEVEL environment variable. This assumes that there is not an existing environment variable with the name ERRORLEVEL—if there is, you will get that ERRORLEVEL value instead.
%cmdcmdline% expands into the original command line that was passed to Cmd.exe prior to any processing by Cmd.exe. This assumes that there is not an existing environment variable with the name CMDCMDLINE—if there is, you will get the CMDCMDLINE value instead.
%cmdextversion% expands into the string representation of the current value of cmdextversion. This assumes that there is not an existing environment variable with the name CMDEXTVERSION—if there is, you will get the CMDEXTVERSION value instead.
You must use the else clause on the same line as the command after the if.
Examples
To display the message "Cannot find data file" if the file Product.dat cannot be found, type:
if not exist product.dat echo Cannot find data file
To format a disk in drive A and display an error message if an error occurs during the formatting process, type the following lines in a batch file:
:begin
@echo off
format a: /s
if not errorlevel 1 goto end
echo An error occurred during formatting.
:end
echo End of batch program.
To delete the file Product.dat from the current directory or display a message if Product.dat is not found, type the following lines in a batch file:
IF EXIST Product.dat (
del Product.dat
) ELSE (
echo The Product.dat file is missing.
)
Note
These lines can be combined into a single line as follows:
IF EXIST Product.dat (del Product.dat) ELSE (echo The Product.dat file is missing.)
To echo the value of the ERRORLEVEL environment variable after running a batch file, type the following lines in the batch file:
goto answer%errorlevel%
:answer1
echo Program had return code 1
:answer0
echo Program had return code 0
goto end
:end
echo Done!
To go to the "okay" label if the value of the ERRORLEVEL environment variable is less than or equal to 1, type:
if %errorlevel% LEQ 1 goto okay
ok,you're right
reference for 'if'
Remarks
If the condition specified in an if clause is true, the command that follows the condition is carried out. If the condition is false, the command in the if clause is ignored and the command executes any command that is specified in the else clause.
When a program stops, it returns an exit code. To use exit codes as conditions, use errorlevel.
If you use defined, the following three variables are added to the environment: %errorlevel%, %cmdcmdline%, and %cmdextversion%.
%errorlevel% expands into a string representation of the current value of the ERRORLEVEL environment variable. This assumes that there is not an existing environment variable with the name ERRORLEVEL—if there is, you will get that ERRORLEVEL value instead.
%cmdcmdline% expands into the original command line that was passed to Cmd.exe prior to any processing by Cmd.exe. This assumes that there is not an existing environment variable with the name CMDCMDLINE—if there is, you will get the CMDCMDLINE value instead.
%cmdextversion% expands into the string representation of the current value of cmdextversion. This assumes that there is not an existing environment variable with the name CMDEXTVERSION—if there is, you will get the CMDEXTVERSION value instead.
You must use the else clause on the same line as the command after the if.
Examples
To display the message "Cannot find data file" if the file Product.dat cannot be found, type:
if not exist product.dat echo Cannot find data file
To format a disk in drive A and display an error message if an error occurs during the formatting process, type the following lines in a batch file:
:begin
@echo off
format a: /s
if not errorlevel 1 goto end
echo An error occurred during formatting.
:end
echo End of batch program.
To delete the file Product.dat from the current directory or display a message if Product.dat is not found, type the following lines in a batch file:
IF EXIST Product.dat (
del Product.dat
) ELSE (
echo The Product.dat file is missing.
)
Note
These lines can be combined into a single line as follows:
IF EXIST Product.dat (del Product.dat) ELSE (echo The Product.dat file is missing.)
To echo the value of the ERRORLEVEL environment variable after running a batch file, type the following lines in the batch file:
goto answer%errorlevel%
:answer1
echo Program had return code 1
:answer0
echo Program had return code 0
goto end
:end
echo Done!
To go to the "okay" label if the value of the ERRORLEVEL environment variable is less than or equal to 1, type:
if %errorlevel% LEQ 1 goto okay
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
•
•
Join Date: Apr 2009
Posts: 16
Reputation:
Solved Threads: 0
Thanks to all who helped me....
I got the problem.....it is not due to file extension....
In some systems, even if u chdir...it is moving to the default dir....
hence the file is searched in the default dir location.....
ex: if i give D:....but the sys has the default dir as C:
it is searching for the test.txt file in the C: dir..
I got the problem.....it is not due to file extension....
In some systems, even if u chdir...it is moving to the default dir....
hence the file is searched in the default dir location.....
ex: if i give D:....but the sys has the default dir as C:
it is searching for the test.txt file in the C: dir..
Last edited by bugista; Apr 24th, 2009 at 3:06 am.
Never Say Never
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the Windows Software Forum
- Previous Thread: to convert Snap shot(.snp) files to Excel - Microsoft Windows 2000
- Next Thread: messenger problems vista
| Thread Tools | Search this Thread |
acquisition adobe ajax applications apps asp backup bailout ballmer banshee beta billgates binary blackberry blogsoftware bostock browser business cisco cs4 development download drive ebay economy email excel exchange facebook freesoftware gaming gartner google grassley halo halo3 hard hardware ibm ie8 intel internet jobs jquery law linux merger microsoft microsoftoffice mobile mobilephone monitor msdn net news nintendo office officefileformats officeproductivitysuites officesuites onelaptopperchild onlinetimetracker openoffice opensource opensuse os outlook patent redhat remoteaccess rim saas screen security server siliconvalley software sony spreadsheet sprint survey takeover timetrackersoftware touchcomputing virtualization vista vmware web wii windows windows7 windowslive word xbox xbox360 xboxlive yahoo yahoo! yang zoho





An IF THEN ELSE will only display one result so stopping at each point is more code than you need.