if exist command is not working in batch file - batch-file

I have one file at c:\1\1.txt
In my batch program before performing any operation I have to check whether the file exists or not
My command is
If exist c:\1\1.txt
Echo 1
Its not working. In fact, because of if command the batch is crashing.

read HELP IF and then try
IF EXIST c:\1\1.txt ECHO 1
note that you have to put the IF command, the condition, and the conditional command instruction in the same line
alternatively you can use parentheses
IF EXIST c:\1\1.txt (
ECHO 1
)

It works for me. I have used this code, written in batch.bat:
#ECHO OFF
If exist c:\1\1.txt ECHO 1
Please, specify "bash is crashing".

test in reverse CONDITION using NOT
If NOT exist c:\1\1.txt EXIT
Echo EXIST

first check by command console the name of file doing
cd c:\1
dir *.txt
why?
if you create a new file txt with notepad and save it writing in name field 1.txt, the final file name is 1.txt.txt , you can verify this with command console doing
cd c:\1
dir *.txt
then if exist sentence work fine because 1.txt don't exist, the true name of file is 1.txt.txt.
is a posibility for this problem.

Related

Bat file- If file exists then restart computer

What would the contents of a bat file be if I want the following
If a file exists (file name and location)
Then delete the file and then restart the computer
It's pretty straightforward -
#echo off
set FileToCheck=NAME_OF_THE_FILE_HERE
if not exist %FileToCheck% goto done
del %FileToCheck%
shutdown /r
:done
You could also use set FileToCheck=%1 instead of hard-coding the filename in your batch file if you want to pass the filename to look for as a command line parameter, i.e. (assuming your batch file is named foo.bat):
c:\>foo RemoveMeAndRebootIfIExist.txt

Bat file to create & write to a file

I'm trying to create and write to a file using .bat
#echo off
echo Jackdows loves my big sphinx >> %appdata%\data.html
echo Of quartz. >> %appdata%\data.html
exit
Works properly. However, if user runs it again, it writes the same values to the file again. So in the files, there are multiple values. Is it possible to prevent this ?
>> appends text to a file regardless of whether it exists elsewhere in the file or not. You can, however, search the file first for the string and then only append the line if it does not already exist.
#echo off
:: Appends a string to a file only if that string is not present in that file
call :ainp "Jackdaws love my big sphinx" text.txt
call :ainp "Of quartz" text.txt
call :ainp "Rule Brittania" text.txt
exit /b
:: Append If Not Present
:AINP
set "search_string=%~1"
set "search_file=%~2"
>nul find "%search_string%" %search_file%
if %errorlevel% equ 1 (
>>%search_file% echo %search_string%
)

Write the computername to a text file if the name is not already in the text file

I have a small batch file that writes the computer name to a log.txt. I need the batch file to scan the list of computernames currently in the file. If the name exist then just exit and if the name doe snot exist I need it to write it.
I have written the part to write the computer name to a file
Echo %computername%>>C:\log.txt
Now I need it to read the log and only write the computer name if it does not exist in the file.
Try this:
FINDSTR "^%computername%$" < C:\log.txt >NUL || ECHO %computername%>>C:\log.txt
The FINDSTR command searches for %computername% in C:\log.txt, making sure it is not a substring of some other name (the ^ and $ symbols around the search term, see FINDSTR /? for more information). If FINDSTR finds nothing, it raises ERRORLEVEL.
The || command delimiter means that the value of ERRORLEVEL should be checked before invoking the following command (ECHO). If ERRORLEVEL is raised, the command executes, otherwise it doesn't.
So, if FINDSTR finds nothing, the name will be added to the file.

How to detect using microsoft .bat files is file empty?

and exectute some command if file empty
Here's a sample batch file that does this. You call it with the name of the file you want to check is empty or not:
#ECHO OFF
IF "%~z1"=="0" GOTO FileEmpty
ECHO File is not empty
GOTO End
:FileEmpty
ECHO File is empty
:End
Note that the file must exist in order for this to work. But you can check that easily with IF EXIST

Is it possible for a batch file to create a file, but only if the file isn't already there?

I would like to know if its possible for a batch file to create a file in a certain location, but only if it doesn't exist there.
For example, if the file in question doesn't exist, create it. Otherwise, do nothing.
Use "if":
if not exist c:\test\test.txt type NUL > c:\test\test.txt
echo 2>> c:\test\test.txt
If echo ever writes anything to the error stream you'll get a shock, but I can't think of any circumstance where it would...
if not exist C:\test\test.txt copy NUL C:\test\test.txt
(And it's really hard typing those paths. I think I never touched anything below the drive root for a few years by now. Are you creating files on Unix systems in / out of habit too?)
if not exist C:\test\test.txt echo (what you want to be inside of the file here) >>C:\test\test.txt
Here's how to create a file with a batch file:
if not exist "C:\folder\test file.txt" goto create
exit
:create
echo Your>"C:\folder\test file.txt"
echo Text>>"C:\folder\test file.txt"
echo Here>>"C:\folder\test file.txt"
If you want you can replace exit with code for handling the existing file.
If the file exists, nothing will happen.
If the file doesn't exist, a file will be created with the text:
Your
Text
Here
It works. I tested it.

Resources