pause in batch file not working - batch-file

I have a batch file that tests to see if a file exists.
After that, it has to pause.
But pause is not working for some reason.
Any help please. thanks much
if exist C:\autoexec.bat
ECHO Folder C:\autoexec.bat exists
PAUSE
Pause works at command prompt.
The file exists for sure. But I can't see Echo as well as Pause
The screen just disappears once I run the .bat file

Do you want to write:
if exist C:\autoexec.bat ECHO Folder C:\autoexec.bat exists
PAUSE
Note that shell after the if command is expecting a command to execute. If there is no command after de condition it should abort batch with:
"The syntax of the command is incorrect."
Edit. Mr.Helpy Got it first.
Note if you want multiple lines use a block
if exist C:\autoexec.bat (
ECHO Folder C:\autoexec.bat exists
REM other commands...
)
PAUSE

The answer is in the command. The valid syntax for if command is
IF [NOT] EXIST filename command
OR
IF [NOT] EXIST filename (command) ELSE (command)
The word command at the last is very important. What I did was this
#Echo off
if exist C:\autoexec.bat goto hi
if not exist C:\autoexec.bat goto bye
:hi
ECHO Folder C:\autoexec.bat exists
PAUSE
:bye
Echo Folder C:\autoexec.bat does not exists
pause
And it worked like a charm
Regards,
See http://ss64.com/nt/if.html

Basic troubleshooting: run from a cmdwindow, not per doubleclick.
Then you see, that
if exist C:\autoexec.bat
give you a syntax error, that breaks the execution of your batchfile.
if expects a command to execute (at the same line), but there isn't one. So the echo line is never reached.

Related

batch: command prompt works but neither file.cmd nor file.bat

I am new to batch. I work with website, I have a folder called web, in this folder, I have many sub directories, such as 'template1', 'template2', etc. In those templates, there is one thing in common, each only contains three same files, 'draw.html', 'draw.js', 'draw.css'. Each time I want to create a new page I need to manually create this pattern. I wonder if I can code a batch file, so I can do this by click on pat.bat, and it generate the folder and three files for me. after thinking, I decide to do this: in web folder, create two files: ['pat.bat', 'pat.txt'] in 'pat.txt', there are three strings seperated by line: [draw.html, draw.js, draw.css], the logic in batch file is pretty simple: it asks the user to input a directory name, then generate
directory_name
|-draw.js
|-draw.html
|-draw.css
here's the batch file I worked out <pat.bat>
#echo off
:dir_loop
echo Create a directory named:
set /p directory=
if "%directory%" equ "" (
echo Please enter a valid directory
goto :dir_loop
)
:pat_loop
if not exist pat.txt (
echo Please create a pat.txt file which
echo contains all your files separated by
echo line with correct extension to
echo generate into the folder.
echo "Have you done it? (y/n)"
set /p good=
if not exist pat.txt (
echo Sorry, no pat.txt detected in current directory
goto :pat_loop
)
)
:final
mkdir %directory%
for /F "delims=," %i in (pat.txt) do (
cd %directory%
cd. > %i
cd..
)
pat.txt
draw.js
draw.css
draw.html
ok, here comes the problem, when I change the 'directory' variable to a default value rather than user input, and run this in the elevated command prompt inside my web directory, it works fine (following code)
#echo off
:dir_loop
echo Create a directory named:
rem !!! change directory from user input to assignment
set directory=template1
if "%directory%" equ "" (
echo Please enter a valid directory
goto :dir_loop
)
:pat_loop
if not exist pat.txt (
echo Please create a pat.txt file which
echo contains all your files separated by
echo line with correct extension to
echo generate into the folder.
echo "Have you done it? (y/n)"
set /p good=
if not exist pat.txt (
echo Sorry, no pat.txt detected in current directory
goto :pat_loop
)
)
:final
mkdir %directory%
for /F "delims=," %i in (pat.txt) do (
cd %directory%
cd. > %i
cd..
)
but, if I set the variable 'directory' to user input rather than string, when I click on the <pat.bat> file, entry my folder name for example, ak, it creates an ak folder in web with nothing in it. (first <pat.bat> code)
I guess it's my for loop's variable assignment issue, I will be so glad if you can help.
PS: you can test the code by simply create a folder on desktop, create two files inside of it pat.txt, pat.bat and paste it in. to test the second bat file, type cmd in the folder, paste in the code (make sure pat.txt is created at first)
#echo off
cd %~dp0
:dir_loop
echo Create a directory named:
set /p directory=
if "%directory%" equ "" (
echo Please enter a valid directory
goto :dir_loop
)
:pat_loop
if not exist pat.txt (
echo Please create a pat.txt file which
echo contains all your files separated by
echo line with correct extension to
echo generate into the folder.
echo "Have you done it? (y/n)"
set /p good=
if not exist pat.txt (
echo Sorry, no pat.txt detected in current directory
goto :pat_loop
)
)
:final
mkdir %directory%
for /F "delims=," %%i in (pat.txt) do (
cd %directory%
cd. > %%i
cd..
)
This code worked for me in a folder in the path C:\Users\Neko\Desktop\Test. There were several issues with your code.
As #Mofi said, in batch files, the command extension of for loop variables get expanded to %%var instead of %var so you have to change your %is to %%i.
This code would automatically execute in the %HOMEPATH% directory (C:\Users\<User>) which would cause errors in finding pat.txt, I changed it so that it executes in the parent directory of pat.bat, %~dp0 (parent directory of batch file). This would look for pat.txt in the same folder as the batch file (which for me was C:\Users\Neko\Desktop\Test) and then makes the folder in that directory. (you can change it by editing the :final label to include a cd <path you want folders in>
Again, like #Mofi said, %%i instead of %i is needed in batch-files. Your error was caused by this since the for loop wouldn't execute.
I ran this file with the circumstances you suggested (you can test the code by simply create a folder on desktop, create two files inside of it pat.txt, pat.bat and paste it in. to test the second bat file, type cmd in the folder, paste in the code (make sure pat.txt is created at first)) and it worked for me, it created draw.js, draw.css, and draw.html in the C:\Users\Neko\Desktop\Test\<%directory%> folder succesfully. (When %directory% is abc it creates draw.js, draw.css, and draw.html in the C:\Users\Neko\Desktop\Test\abc folder)

Start batch file from terminal and pass a specific argument

I have a batch file with the following code which is starting a jarfile.
#ECHO OFF
IF NOT EXIST jarFile.jar GOTO ERROR
IF EXIST jarFile.jar Start java -jar jarFile.jar
ECHO Successfully started!
PAUSE
EXIT
:ERROR
ECHO Jar file doesn't found!
PAUSE
EXIT
I would like to ask you when i start the bat file from CMD prompt, to use a parameter "-force" which can skip the pause after starting it.
Example: start someBatFile.bat -force(the new parameter) and when it is used the "-force" parameter i want to skip the pause. Is it possible to do that and if it is can you help me ?
Change the line with PAUSE to the following:
IF NOT !%1!==!-force! PAUSE

How to create a text file inside a folder on the desktop using batch

I am trying to get a batch file to create a folder on the desktop with a text file inside of it. Every-time i try to run this line of code it gives my the error that "The filename, directory name, or volume label syntax is incorrect."
echo ========================
::CREATE FILES START
cd /d C:
md Title
echo.>"C:\Users\%USERACCOUNT%\Desktop\Example\example.txt"
::CREATE FILES END
echo Done!
pause >nul
Your code is changing to drive C, then creating GeoHunt2015 in root. Then you try to echo the file into non-existent folder on desktop, hence the error.
This assumes your %userprofile% is "c:\users\name"
md "%USERPROFILE%\Desktop\GeoHunt2015"
echo.>"%USERPROFILE%\Desktop\GeoHunt2015\Mission_Instuctions.txt"
or you can cd to desktop
echo ========================
:: CREATE FILES START
cd /d "%USERPROFILE%\Desktop\"
md GeoHunt2015
echo. >"GeoHunt2015\Mission_Instructions.txt"
:: CREATE FILES END
echo Done!
pause >nul
Is %USERACCOUNT% defined?
Is the echo actually causing the issue?
Try commenting out stuff until you are sure that the echo is causing the syntax error.
A couple things I can see. You're switching to the C: directory, then making the GeoHunt2015 folder, but then attempting to echo into the GeoHunt2015 folder on your desktop.
Try this echo instead:
echo.>"C:\GeoHunt2015\Mission_Instructions.txt"
Try using
mkdir "C:\%USERPROFILE%\Desktop\GeoHunt2015"
echo.>"C:\%USERPROFILE%\Desktop\GeoHunt2015\Mission_Instuctions.txt"
as you had in your original version of the question.

if exist command is not working in 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.

Batch script fails with "The system cannot find the path specified"

I have written the following batch script, which runs another batch script on a directory, or, with addition of a flag, on a directory tree and then on an equivalent directory or directory tree on a different drive (Z:). No matter which option I choose, it outputs the error "The system cannot find the path specified." It does do what it's supposed to if I do it on just one directory, even though it gives the error. It doesn't work successfully on a directory tree. I've run it without #echo off to try understand where its failing, without success. The directory which it's trying to change into does exist.
#echo off
set origdir=%CD%
if X%~f1==X (
echo Please input a directory.
goto done
)
chdir /d %~f1
for %%X in (myotherscript.bat) do (set FOUND=%%~$PATH:X)
if not defined FOUND (
echo myotherscript is not in your PATH
)
if X%2==X/R (
goto recursive
) else ( goto single )
:recursive
for /d /r %%G in (.) do call myotherscript
echo Z:%~p1
chdir /d "Z:%~p1"
for /d /r %%G in (.) do call myotherscript
goto ended
:single
call myotherscript
echo Z:%~p1
chdir /d "Z:%~p1"
call myotherscript
goto ended
:ended
chdir /d origdir
goto done
:done
pause
Here is "myotherscript" Yes, purge does exist.
#echo off
if exist "D:\path\to\purge.bat" (
call purge
for %%f in (*.log.*) do call :renameit "%%f"
for %%f in (*.drw.*) do call :renameit "%%f"
for %%f in (*.asm.*) do call :renameit "%%f"
for %%f in (*.prt.*) do call :renameit "%%f"
goto done ) else (
echo Purge does not exist.
goto done )
:renameit
ren %1 *.1
:done
Any help would be appreciated.
Thanks
I'm not sure why this (very old) question got reactivated. But since it has, let's see if we can close this out.
There seem to be two problems here. First:
it outputs the error "The system cannot find the path specified."
This looks like a simple typo on this line:
chdir /d origdir
Without the '%' marks, this is trying to change to a directory literally named origdir, rather than the original directory the script was run from, which would be:
chdir /d %origdir%
Second problem is:
It does do what it's supposed to if I do it on just one directory, even though it gives the error. It doesn't work successfully on a directory tree.
At a guess, this is due to this line:
if X%2==X/R
"IF" is case sensitive. If you tried to run this using /r, it wouldn't see the request for recursion and would always execute single.
For me I got the "The system cannot find the path specified" due to a missing exe that seemed way later in the script. It seems that the pipes in DOS don't always output data in the order of execution. I was used to UNIX where the output from each "echo" command in a script goes in order, so I had added debug output in the .bat file to try to tell me what lines had executed.
The problem is, the error about the file not found was happening in the output log (and screen) way earlier than the echo commands would indicate. So I don't know if the WinXP cmd shell was going a few steps ahead, or it was parsing for the exe to call during startup of the called bat file or what.
It turned out it was in fact a bad path to the .exe I was running from a call'd bat script, but the echo debug statements made me think I was in a way earlier part of the script. Once I added the right path before the exe it all worked

Resources