0

I have a batch script which when given the input "edit", should then echo "hello" as a sort of debug and open the batch scripts file in notepad. However for some inexplicable reason the script will not respond to the if statement no matter what. How do I get it to respond to the "edit" input?

REM @ECHO OFF cd/ cd projects\py_test ECHO Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY. ECHO. SET /P name="Type file name with file extension .py to start or type EDIT to edit this .bat: " REM @ECHO OFF cmd /k IF %name%==edit GOTO EDIT REM IF EXIST %name% py %name% REM IF NOT EXIST %name% echo [101mERROR: The requested file could not be found. Make sure the file exists in "C:\Projects\py_test\" and that the filename includes the ".py" extension.[0m @ECHO OFF :EDIT ECHO HELLO notepad projects-py_test-dot_start.bat` 
1
  • 1
    In my opinion the entire script is pointless, but was I to use such a thing, I think I'd create a .py script named edit.py. That way the user just enters the name and it is run from python; it'll make the process more understandable and straight forward, and would mean the the end user didn't needlessly have to append .py to their entry, as it would be a valid suffix on every entry. Commented Feb 12, 2019 at 8:11

1 Answer 1

1

Firstly, why all the REM @ECHO OFFs? It looks ugly, especially when they are all caps.

Then, you want to run cmd /k for an if statement for no real reason? With the variable name you need to preferbly enclose the if statement variables in double quotes to eliminate possible whitespace:

@echo off cd /d "C:\projects\py_test" echo Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY. echo/ set /p name="Type file name with file extension .py to start or type EDIT to edit this .bat: " if defined name set "name=%name:"=%" if /i "%name%"=="edit" goto edit goto :EOF :edit echo hello notepad echo "%~f0" 

but by guessing that you simply want to launch a python script if it exists, else edit itself, then I would instead do this version without the labels. It simply checks if the name typed exists (hoping the user typed the full script with extension) else, we add the extension test in case the user typed only the name and not extension.:

@echo off cd /d "C:\projects\py_test" echo Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY. echo/ set /p name="Type file name with file extension .py to start or type EDIT to edit this .bat: " if defined name set "name=%name:"=%" if /i "%name%"=="edit" notepad "%~f0" if exist "%name%" ( python "%name%" ) else ( if exist "%name%.py" ( python "%name%.py" ) else ( echo "%name%" does not exist ) ) 
Sign up to request clarification or add additional context in comments.

9 Comments

Gerhard, as the file to edit is the batch file itself, I would suggest that you utilise the %0 option instead of projects-py_test-dot_start.bat. This would mean that regardless of any future renaming of the file, it would still be found for editing.
@Compo Thanks, will add it, I however suspect the OP wants that feature to edit a file if it does not exist (i.e a python file), but yes, it is not specified anyway, thanks for good point!
Gerhard, I would not advocate using "%~dpf0" which makes little sense, instead going for just %0, which should auto enclose itself with doublequotes. However if it makes you feel safer, perhaps "%~f0" or "%~dpnx0" as alternatives.
@Compo the reason is because we do a cd earlier in the batch, so just doing %0 would try and open the file inside the cd'd dir, but %~f0 is better yes.
@Gerhard, just because you used CD earlier shouldn't necessarily matter, try it! 1. @CD /D E:\DATA, 2. @Echo([%0], 3. @Pause. The output will be something along these lines, ["C:\Users\Gerhard\Desktop\test.cmd"], i.e. the full path enclosed within doublequotes.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.