Question of nested if-else condition in batch file - windows

I don't know why it doesn't work when nested if-else start from "Memo File AND Other File Not Found" to "Memo File Not Found".
Anyone can please help me?
Much appreciated if you could solved.
Thanks!
Original Code
#ECHO OFF
SET FOLDER="D:\temp\test"
SET TRANS="D:\temp\test\Trans\Trans.txt"
SET MEMO="D:\temp\test\Memo\Memo.txt"
SET OTHER="D:\temp\test\Other\Other.txt"
IF NOT EXIST %FOLDER% (
ECHO Folder Not Found
) ELSE (
IF NOT EXIST %MEMO% (
IF NOT EXIST %TRANS% (
IF NOT EXIST %OTHER% (
ECHO All Files Not Found
) ELSE (
IF NOT EXIST %MEMO% (
IF NOT EXIST %TRANS% (
ECHO Memo File AND Trans File Not Found
) ELSE (
IF NOT EXIST %MEMO% (
IF NOT EXIST %OTHER% (
ECHO Memo File AND Other File Not Found
) ELSE (
IF NOT EXIST %TRANS% (
IF NOT EXIST %OTHER% (
ECHO Trans File AND Other File Not Found
) ELSE (
IF NOT EXIST %TRANS% (
ECHO Trans File Not Found
) ELSE (
IF NOT EXIST %MEMO% (
ECHO Memo File Not Found
)
)
)
)
)
)
)
)
)
)
)
)

This is the way I would do it:
#ECHO OFF
SET FOLDER="D:\temp\test"
SET TRANS="D:\temp\test\Trans\Trans.txt"
SET MEMO="D:\temp\test\Memo\Memo.txt"
SET OTHER="D:\temp\test\Other\Other.txt"
IF NOT EXIST %FOLDER% (
ECHO Folder Not Found
GOTO :EOF
)
SET "NOTFOUND="
IF NOT EXIST %TRANS% SET "NOTFOUND=Trans"
IF NOT EXIST %MEMO% SET "NOTFOUND=Memo %NOTFOUND%"
IF NOT EXIST %OTHER% SET "NOTFOUND=Other %NOTFOUND%"
IF DEFINED NOTFOUND ECHO These files not found: %NOTFOUND%

Does this example output the information you're trying to retrieve:
#ECHO OFF
SET "FOLDER=D:\temp\test"
SET "TRANS=%FOLDER%\Trans\Trans.txt"
SET "MEMO=%FOLDER%\Memo\Memo.txt"
SET "OTHER=%FOLDER%\Other\Other.txt"
IF NOT EXIST "%FOLDER%\*" (
ECHO Folder Not Found
) ELSE (
IF NOT EXIST "%MEMO%" (
IF NOT EXIST "%TRANS%" (
IF NOT EXIST "%OTHER%" (
ECHO All Files Not Found
) ELSE (
ECHO Memo File AND Trans File Not Found
)
) ELSE (
IF NOT EXIST "%OTHER%" (
ECHO Memo File AND Other File Not Found
) ELSE (
ECHO Memo File Not Found
)
)
) ELSE (
IF NOT EXIST "%TRANS%" (
IF NOT EXIST "%OTHER%" (
ECHO Trans File AND Other File Not Found
) ELSE (
ECHO Trans File Not Found
)
) ELSE (
IF NOT EXIST "%OTHER%" (
ECHO Other File Not Found
) ELSE (
ECHO All Files Found
)
)
)
)
PAUSE

Related

How to reference the first parameter of a Windows batch file in an IF condition?

I'm very new in cmd batch files. I have code:
#echo off
if {%1} =="1" (
goto 1cmd
)
if {%1} =="2" (
goto 2cmd
)
if {%1} =="3" (
goto 3cmd
)
if {%1} =="" (
echo qwerty
)
:1cmd
call D:\test\1\1.cmd
goto end
:2cmd
call D:\test\2\2.cmd
goto end
:3cmd
call D:\test\3\3.cmd
goto end
:end
File is named a.bat. No matter what parameter I type, a.bat always calls 1.cmd.
What is the reason?
Is this working ?
#ECHO OFF
if "%~1" =="1" (
goto 1cmd
)
if "%~1" =="2" (
goto 2cmd
)
if "%~1" =="3" (
goto 3cmd
)
if {%1} =="" (
echo qwerty
)
exit /b 0
:1cmd
call D:\test\1\1.cmd
goto end
:2cmd
call D:\test\2\2.cmd
goto end
:3cmd
call D:\test\3\3.cmd
goto end

if statement not working batch

So I have this batch file that has something like this:
if "%1" == "dev" (
rem do something ...
)
else (
rem do something else ...
)
and I have this echo command above the if-else block
echo "%1" >> test.txt
The content of test.txt after running is "dev" (with the double quotes).
However, it always hit the else block instead of the if block. I checked that by putting another echo command inside the else ()
The problem is quite trivial! Simply move else ( one line up:
if "%1" == "dev" (
rem do something ...
) else (
rem do something else ...
)
Now it should work.
else have to be in same line of ) and (
IF /? show you this usage:
if exist filename (
del filename
) else (
echo filename not found
)
Or in 1 line:
if exist filename ( del filename ) else ( echo filename not found )
Or in 2 lines
if exist filename ( del filename
) else ( echo filename not found )
2 lines variation
if exist filename ( del filename ) else (
echo filename not found )
etc...

Equal variables - Batch

So, I've the following batch script:
#echo off
set /p name=
rem a random number, don't care about it.
set complete_name=%name%.Creepy
Goto STEP1
:STEP1
echo %complete_name%|findstr /C:"9000" >nul 2>&1
if not errorlevel 1 (
goto 9000
) else (
GOTO CHECK2
)
:CHECK2
echo %complete_name%|findstr /C:"930" >nul 2>&1
if not errorlevel 1 (
goto 930
) else (
GOTO CHECK3
)
:CHECK3
echo %complete_name%|findstr /C:"310" >nul 2>&1
if not errorlevel 1 (
goto 310
) else (
ECHO PROBLEM
)
:9000
ECHO 9000
PAUSE
:930
ECHO 930
PAUSE
:310
ECHO 310
PAUSE
I want it to check if "9000" is in the variable or not, same for "930" and "310". And if none of these numbers are in the variable Echo problem. But everytime i run this script it goes to ECHO PROBLEM even if 9000/920/310 is in %complete_name%. So, is this the right way to check if a variable is in another one or there is an easier way to do it?
So I've tried this code:
#echo off
set name=310
set complete_name=%name%.Creepy
Goto STEP1
:STEP1
setlocal
if "%complete_name:9000=%"=="%complete_name%" (
if "%complete_name:930=%"=="%complete_name%" (
if "%complete_name:310=%"=="%complete_name%" (
echo PROBLEM
) else (
goto 9000
)
) else (
goto 930
)
) else (
goto 310
)
goto :eof
but I'm stuck at echo problem...
Save next code snippet, possibly named blabla.bat:
#Echo OFF
setlocal
set complete_name=%1
if "%complete_name:9000=%"=="%complete_name%" (
if "%complete_name:930=%"=="%complete_name%" (
if "%complete_name:310=%"=="%complete_name%" (
echo problem
) else (
echo valid 310
)
) else (
echo valid 930
)
) else (
echo valid 9000
)
Exit /B
and watch the output from
blabla x9000y
blabla x930y
blabla x310y
blabla x9a0b0c0y
That's a way of 1. nested IF ... ( ... ) ELSE ( ... ) and 2. using "Edit/Replace" a variable
Or, if GOTOs considered necessary, there is something similar with For loop (a good thought topic as well..).
#Echo OFF
setlocal EnableDelayedExpansion
set complete_name=%1
for %%G in (9000 930 310) DO (
if /I "!complete_name:%%G=!" neq "%complete_name%" GOTO :%%G
)
echo problem %complete_name%
GOTO :commonEnd
:9000
Echo valid 9000 %complete_name%
GOTO :commonEnd
:930
Echo valid 930 %complete_name%
GOTO :commonEnd
:310
Echo valid 310 %complete_name%
GOTO :commonEnd
:commonEnd

Batch file move specific files

I have a very large number of files with this template:
example1.part001.rar
example1.part002.rar
example1.part003.rar
...
example2.part001.rar
example2.part002.rar
example2.part003.rar
...
and sometimes with only two digits after "part"
example3.part01.rar
example3.part02.rar
example3.part03.rar
...
I'm trying to create a batch file that first makes n directories (n is for number of groups of files) with the name " example n ", and then that moves all relatives file into them.
I will explain better.
I have
cat.part01.rar
cat.part02.rar
cat.part03.rar
dog.part001.rar
dog.part002.rar
mouse.part01.rar
mouse.part02.rar
mouse.part03.rar
mouse.part04.rar
I want to first make the directories "cat", "dog" and "mouse, and then move all relatives file into them (I mean all "foldername.part*.rar in "foldername"), so "cat.part*.rar" files into "cat" folder and so on.
Files are many, and filenames are very very different and random.
Can somebody help me? Thanks
EDIT :
#echo off&cls
setlocal EnableDelayedExpansion
for %%a in (*.rar) do (set $file=%%~na
set $file=!$file:^.= !
call:work %%a)
exit /b
:work
for %%b in (!$file!) do (if not exist %%b md %%b
copy "%1" ".\%%b"
exit /b)
This should do it also, and handle filenames with ! in them too.
#echo off
for %%a in (*.rar) do (
if exist "%%a" for %%b in ("%%~na") do (
md "%%~nb" 2>nul
move "%%~nb.part*.rar" "%%~nb" >nul
)
)
pause
Here I get a list of groups by splitting the file name by '.'. I then get the uniq values of the first column. For each of the results I create the directory. Then move all files into the directory
#/usr/bin/sh
groupnames=(`ls *.rar | awk -F"." '{print $1}' | uniq`)
for i in ${groupnames[*]}; do
mkdir $i
mv $i.*.rar ./$i/
done
Example files of test:
ball.part1.rar
ball.part2.rar
cat.part01.rar
cat.part02.rar
dog.part001.rar
dog.part002.rar
The script creates folders ball, cat, dog, after that, it moves the files to their respective folders, works with partX, partXY and partXYZ in filename
#echo off
#break off
#title Batch file move specific files
#color 0a
#cls
setlocal EnableDelayedExpansion
if "%~1" NEQ "" (
set "WORKINGDIR=%~1"
) else (
set "WORKINGDIR=!CD!"
)
if not exist "!WORKINGDIR!\*.rar" (
set "WORKINGDIR=!CD!"
)
if not exist "!WORKINGDIR!\*.rar" (
echo There are no winrar files in this directory
echo.
echo Exiting...
echo.
ping -n 4 localhost>nul
pause
exit
)
for %%a in ( "!WORKINGDIR!\*.rar" ) do (
set "name=%%~nxa"
if "!name:~-4!" EQU ".rar" (
set "name=!name:~0,-4!"
)
if "!name:~-8,5!" EQU ".part" (
echo !name:~-7! | find ".">nul
if "!errorlevel!" NEQ "0" (
set "name=!name:~0,-8!"
) else (
set "skip=yes"
)
) else if "!name:~-7,5!" EQU ".part" (
echo !name:~-6! | find ".">nul
if "!errorlevel!" NEQ "0" (
set "name=!name:~0,-7!"
) else (
set "skip=yes"
)
) else if "!name:~-6,5!" EQU ".part" (
echo !name:~-5! | find ".">nul
if "!errorlevel!" NEQ "0" (
set "name=!name:~0,-6!"
) else (
set "skip=yes"
)
)
if not defined skip (
set "skip=no"
)
if "!skip!" EQU "no" (
if not exist "!WORKINGDIR!\!name!\" (
mkdir "!WORKINGDIR!\!name!\"
)
move /y "!WORKINGDIR!\%%~nxa" "!WORKINGDIR!\!name!\%%~nxa"
)
set "skip=no"
)
pause
exit

Why does my if Else if statement not work in Batch script?

My Script
echo "Enter your choice (1 or 2 or 3) :"
set /p dbchoice=
IF %dbchoice EQU 1 (
set dbtype="oracle"
) ELSE (
IF %dbchoice EQU 2 (
set dbtype="sqlserver"
)
) ELSE (
IF %dbchoice EQU 3 (
set dbtype="db2"
)
) ELSE (
echo "Incorrect choice"
)
I get the following output:
E:\csmilm>set /p dbchoice=
1
ELSE was unexpected at this time.
E:\csmilm>) ELSE (
E:\csmilm>
What is the problem here?
Closing brackets in the wrong place.
Try:
IF %dbchoice EQU 1 (
set dbtype="oracle"
) ELSE (
IF %dbchoice EQU 2 (
set dbtype="sqlserver"
) ELSE (
IF %dbchoice EQU 3 (
set dbtype="db2"
) ELSE (
echo "Incorrect choice"
)
)
)

Resources