Home > Back-end >  If else statement in bat
If else statement in bat

Time:10-21

The code below checks if there is a folder called Made_files if so it will cd to that folder and make a new file using variables Else it will make a folder called Made_files THEN cd to that folder and run the code but at the moment all it does is flash up then close instantlly can someone help


if EXIST Made_files (
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo @echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
) else mkdir Made_files
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo @echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
echo ___
pause 



CodePudding user response:

@ECHO OFF
SETLOCAL

:: Ask for filename and type

set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-

:: Required file to be located in "Made_files"
SET "subdir=Made_files"
MD ".\%subdir%" 2>NUL
CD "%subdir%"
(
 echo @echo off
 echo color 0a
)> %name%.%Filetype%

echo ___
pause 

GOTO :EOF

What your code does:

If the subdirectory already exists, switch to that directory, ask for the filename&type, then create a new file using the requested data. Otherwise, create the directory.

THEN

Change to the subdirectory again (so if made_files already existed, try to change to .\made_files\made_files), ask for the filename and type and create the file.

===

BUT Please read Stephan's DELAYEDEXPANSION link. Since you are not using delayedexpansion, when the if statement is parsed (and the if statement continues until the end of the else clause) the values of the variables name and filetype are replaced by their values when the statement was parsed (~verified), NOT as they were input, so the destination filename will be nothing.nothing , which is "." - a directory-name (sort-of).

It's likely that this is the source of the drama.

When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.

===

So : the code I suggest:

  1. Asks for the file name and type.
  2. Attempts to make the directory. If it already exists, this will generate an errormessage which is suppressed by the 2>nul.
  3. switches to that subdirectory.
  4. Creates the file.

Note that if the echoes are surrounded by parentheses, then their output is combined and directed to the file, saving a plethora of redirect-to-file clauses.

CodePudding user response:

It sounds like your script has an error and by default this will cause the window to close.

This can be annoying since it prevents you from seeing the error message to debug.

Put this at the top of your script and it will keep the terminal window open even if you hit an error:

@echo off
setlocal enabledelayedexpansion

REM Keep cmd window open until script finishes in case of error
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
  • Related