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...
Related
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
#echo off
cls
setlocal EnableDelayedExpansion
echo
for %%f in (*.lnk) do (
set x=%%f
echo !x!
set first_char= "!x:~0,1!"
echo !first_char!
IF !first_char! == "." (
echo "first char is a dot"
) ELSE (
echo "first char is NOT a dot"
)
)
PAUSE
My files start with a dot and this windows cmd .bat programme echo allway "first char is NOT a dot"
so there is a problem surely with the IF interpretation.
Can someone figure it out ?
Ok i understand now !
the correct way is :
rem no space after equal and no need double quote
set first_char=!x:~0,1!
rem and this IF will work perfect
IF !first_char! == . (
Fine thanks for your help.
#echo off
set doit=false
if exist findstr /n . lla.txt | findstr ^j: set doit=true
if "%doit%"=="true" (
echo found
) else (
echo doelse
)
hi every one
i want to check lines in a text file
for example if line3 is exist in my text file then echo found
if not then echo doelse
whats my problem ?
To see if lla.txt has at least 3 lines replace j with 3 and use this type of structure:
#Echo Off
(
FindStr /N "^" "lla.txt" | FindStr "^j:" > Nul
) && (
Echo Line Found
) || (
Echo Line Not Found
)
Pause
This uses conditional execution, whereby:
&& essentially means if the previous command/block was
successful.(returned a 0 %ErrorLevel%)
|| essentially means if the previous command/block was
unsuccessful.(returned a non 0 %ErrorLevel%)
If lla.txt cannot be found the first FindStr will output an error and carry out the commands within your Line Not Found parentheses.
If that's what you want then you can supress the error message by sending the STDERR output to a NUL device like this:
#Echo Off
(
FindStr /N "^" "lla.txt" 2> Nul | FindStr "^j:" > Nul
) && (
Echo Found
) || (
Echo Line Not Found
)
Pause
If you want to do something different in those cases you should verify the source files existence first:
This uses an If/Else structure:
#Echo Off
If Exist "lla.txt" (
(
FindStr /N "^" "lla.txt" | FindStr "^j:" > Nul
) && (
Echo Found
) || (
Echo DoElse
)
) Else (
Echo No source file
)
Pause
This uses an If, Echoes a message for 3 seconds and Exits
#Echo Off
If Not Exist "lla.txt" (
Echo No source file
Timeout /T 3 > Nul
Exit /B
)
(
FindStr /N "^" "lla.txt" | FindStr "^j:" > Nul
) && (
Echo Line Found
) || (
Echo Line Not Found
)
Pause
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
#echo off
set filename =
cd GWConfig_TDS-mtpe3003
set filename = VCU17_CCU6\applications\VCU17APP
GOTO CHECKFILE
:CHECKFILE
echo reached
IF EXIST %filename% ( echo exists
) ELSE ( echo Doesnot exist )
/////////////////////////////////////////////////
Here output shows :
reached
Doesnot echo "exists" or "Doesnot exist"
Is there anything wrong with the use of variable "filename".
Also,
#echo off
set filename =
cd GWConfig_TDS-mtpe3003
set filename = VCU17_CCU6\applications\VCU17APP
GOTO CHECKFILE
:CHECKFILE
echo reached
IF EXIST VCU17_CCU6\applications\VCU17APP ( echo exists
) ELSE ( echo Doesnot exist )
gives output:
reached
exists.
There are two problems here. One is the space after the variable name:
SET filename = whatever
should be
SET filename=whatever
(Or you could use %filename % later, but that's just horrible :)
The second problem is that without any quotes, your "IF" test won't work properly if %filename% is empty. Quote it instead:
IF EXIST "%filename%" ( echo exists
) ELSE ( echo Doesnot exist )
I don't have the time to properly re-create this now, but I can spot a few potential problems:
Have you tried removing the spaces around the = in set:
set filename=VCU17_CCU6\applications\VCU17APP
otherwise you might have a single space leading your file name
You might want to try to quote the use of %filename%:
IF EXIST "%filename%" ( ...