# Solved: Batch script If-Else and string comparisson



## mydxbtester (Apr 21, 2015)

I have a difficulty in matching strings from filename. Say, I have this recording files as follow:

a. 2015-04-13-14-45-47_*XXXXXXXXXXXX*_*20010*.mp4
b. 2015-04-13-15-46-48_*YYYYYYYYYYYY_20015*.mp4
c. 2015-04-13-16-47-49_*20016*_*AAAAAAAAA*.mp4

20010, 20015, and 20016 are IP Phone Extensions
*XXXXXXXXXXXX* and *YYYYYYYYYYYY *are specific phone numbers
*AAAAAAAAA *is a random called number

What I wanted is for all recordings to be place to specific folder, everytime *XXXXXXXXXXXX or **YYYYYYYYYYYY *calls an IP Phone Extension - the recording should be placed to a folder named "Specific". So, items "a" and "b" will be moved to "Specific" folder while item "c" will be moved to folder 20016.

Here's what I did so far but it does not work:


```
set extensions=/20010/20015/20020/20025/XXXXXXXXXXXX/YYYYYYYYYYY/

for %%a in ("%FolderIncoming%\%FileMask%") do (

   set FileName=%%~na


   for %%b in (!Filename:_^= !) do (
      if "!extensions:/%%b/=!" neq "%extensions%" set "extension=%%b"
   )


   REM set TargetYear=!FileName:~0,4!
   set TargetFolder=!extension!\!FileName:~0,10!

   if %extension% == "XXXXXXXXXXXX" (
   REM Save file to "Specific" folder since it contains "XXXXXXXXXXXX"

   ) else (
   if %extension% == "YYYYYYYYYYY" (
   REM Save file to "Specific" folder since it contains "YYYYYYYYYYY"
   )
   ) else (
            if not exist "%FolderSorted%\!TargetFolder!\%%~na.mp3" (
               echo Processing '!FileName!' ...
               REM Save file to source IP phone extension
            ))
```
What could I be missing here.


----------



## foxidrive (Oct 20, 2012)

```
@echo off
md "Specific" 2>nul
for %%a in (*.mp4) do (
   for /f "tokens=2 delims=_" %%b in ("%%a") do if %%b geq 0 if %%b leq 99999 do move "%%a" "%%b"
   if exist "%%a" move "%%a" "Specific"
)
```


----------



## mydxbtester (Apr 21, 2015)

foxidrive said:


> ```
> @echo off
> md "Specific" 2>nul
> for %%a in (*.mp4) do (
> ...


Hi Foxidrive,

Can you elaborate your code further, I don't seem to get where I can insert those codes and how will I integrate it with the if-else condition whether it should be moved to "Specific" folder since it contains XXXXXXXXXXXX or YYYYYYYYYYYY numbers.

Thanks.


----------



## foxidrive (Oct 20, 2012)

Did you try it with copies of the files you showed, in an empty folder?
Did it work?


----------



## mydxbtester (Apr 21, 2015)

This is my actual working code:


```
@echo off
setlocal enabledelayedexpansion
set extensions=/20010/20015/20020/20025/20030/XXXXXXXXXXXX/YYYYYYYYYYYY/
set FolderIncoming=D:\Backup\Calls\Our_Calls
set FileMask=201*.wav
set FolderSorted=D:\Backup\Sorted
Set LameLocation=C:\Program Files (x86)\Lame

for %%a in ("%FolderIncoming%\%FileMask%") do (

   set FileName=%%~na


   for %%b in (!Filename:_^= !) do (
      if "!extensions:/%%b/=!" neq "%extensions%" set "extension=%%b"
   )


   set TargetFolder=!extension!\!FileName:~0,10!

            if not exist "%FolderSorted%\!TargetFolder!\%%~na.mp3" (
               echo Processing '!FileName!' ...
               if not exist "%FolderSorted%\!TargetFolder!" md "%FolderSorted%\!TargetFolder!"
               "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\!TargetFolder!\%%~na.mp3"
            )

)
```
What it does is move all converted mp3 files to it's correct folder (IP Extension number) regardless if it contains both XXXXXXXXXXXX and YYYYYYYYYYYY numbers.

I want to move all converted file with XXXXXXXXXXXX and YYYYYYYYYYYY (even with the normal IP extensions on the filename) number on it's filename to "specific" folder. So long as XXXXXXXXXXXX and YYYYYYYYYYYY existed on the filename, it will be moved to "Specific" folder.

How can I include your code for this to happen.


----------



## foxidrive (Oct 20, 2012)

My code had two flaws - an incorrect do keyword, and the folder wasn't created before moving the file.

Test this on some copies of your files.


```
@echo off
md "Specific" 2>nul
for %%a in (*.mp4) do (
   for /f "tokens=2 delims=_" %%b in ("%%a") do if %%b geq 0 if %%b leq 99999 (md "%%b" 2>nul & move "%%a" "%%b")
   if exist "%%a" move "%%a" "Specific"
)
pause
```


----------



## mydxbtester (Apr 21, 2015)

foxidrive said:


> My code had two flaws - an incorrect do keyword, and the folder wasn't created before moving the file.
> 
> Test this on some copies of your files.
> 
> ...


Hi Foxidrive,

Thanks again for the reply.

I did some changes on my code and tried integrating yours but everytime I execute the batch file, it just terminates w/o performing the task. What am I missing here?


```
@echo off
setlocal enabledelayedexpansion
set FolderIncoming=D:\Backup\PhoneCalls\All_Calls
set FileMask=201*.wav
set FolderSorted=D:\Backup\Test
Set LameLocation=D:\Program Files (x86)\Lame
Set Specific=D:\Backup\Test\Specific


for %%a in ("%FolderIncoming%\%FileMask%") do (

   Set FileName=%%~na


   for /f "tokens=2 delims=_" %%b in ("%%a") do if %%b geq 0 if %%b leq 99999 (

        set TargetFolder=%%b\!FileName:~0,10!

            if not exist "%FolderSorted%\!TargetFolder!\%%~na.mp3" (
               echo Processing '!FileName!' ...
               if not exist "%FolderSorted%\!TargetFolder!" md "%FolderSorted%\!TargetFolder!"
               "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\!TargetFolder!\%%~na.mp3"
            )   
 

   if exist "%%a" (
               if not exist "%Specific%\!TargetFolder!\%%~na.mp3" (
               echo Processing '!FileName!' ...
               if not exist "%Specific%\!TargetFolder!" md "%Specific%\!TargetFolder!"
               "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%Specific%\!TargetFolder!\%%~na.mp3"
            )


   ) 


)

pause
```


----------



## foxidrive (Oct 20, 2012)

Just a side comment - I rarely look at people's code that doesn't work unless it is described very well, because all the details you don't provide about the code could cause it to fail - and we don't have the information to solve it.

I did glance at your code just now - and it looks to be using *.wav and *.mp3 files.
Do you use *.mp4 files at all? Because your code doesn't seem to mention them.


----------



## mydxbtester (Apr 21, 2015)

Hi, I don't use mp4 files.

What my original code do is to convert all wav files to mp3 files and store them to correct folders (IP Extension named folders). If the converted mp3 file has the 'XXXXXXXXXXXX' and 'YYYYYYYYYYY' ip phones in it, it will be stored in "Specific" folder.

I edited my code and included yours but it doesn't seem to work, it just terminates whenever I execute the batch file.


----------



## foxidrive (Oct 20, 2012)

mydxbtester said:


> Hi, I don't use mp4 files.


Why did you use mp4 files in your question?


----------



## mydxbtester (Apr 21, 2015)

My bad, I didn't realize that I was typing the wrong file extension when I posted my question, that should be *.mp3 as it's a call recording.


----------



## foxidrive (Oct 20, 2012)

Here's a tip for the future.
Describe the task. Be accurate. Supply all details.

The filetype could have been fixed easily - except the task you described is different from what your code is doing.

There's a reason why a spoken language is used to describe what you want to do in a programming language.


----------



## mydxbtester (Apr 21, 2015)

Ok. What I wanted is to convert wav files stored in:

set FolderIncoming=D:\Backup\PhoneCalls\All_Calls

to mp3 files,

"%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\!TargetFolder!\%%~na.mp3"

And store them to fixed folders according to the extensions it has on it's filename. Extension we use are, say:

set extensions=/20010/20015/20020/20030/20035/20040/

and a generated recording filename sample will be:

a. 2015-04-13-14-45-47_*XXXXXXXXXXXX*_*20010*.mp4
b. 2015-04-13-15-46-48_*YYYYYYYYYYYY_20015*.mp4
c. 2015-04-13-16-47-49_*20016*_*AAAAAAAAA*.mp4

20010, 20015, and 20016 are IP Phone Extensions
*XXXXXXXXXXXX* and *YYYYYYYYYYYY *are specific phone numbers
*AAAAAAAAA *is a random called number

What I wanted is for all recordings to be place to specific folder, everytime *XXXXXXXXXXXX or **YYYYYYYYYYYY *calls an IP Phone Extension - the recording should be placed to a folder named "Specific". So, items "a" and "b" will be moved to "Specific" folder while item "c" will be moved to folder 20016.

Actually, the code you supplied works but I don't know how to integrate it on my current batch file so they will be stored on my pre-defined location:

set FolderIncoming=D:\Backup\PhoneCalls\All_Calls
set FileMask=201*.wav
set FolderSorted=D:\Backup\Test
Set LameLocation=D:\Program Files (x86)\Lame
Set Specific=D:\Backup\Test\Specific

All recordings with no *XXXXXXXXXXXX *and *YYYYYYYYYYYY* in the filename will go to their own folders (Extension-named folder) while those with *XXXXXXXXXXXX *and *YYYYYYYYYYYY* will be in "Specific" folder.

Can you help me check on this.


----------



## foxidrive (Oct 20, 2012)

I think you are still missing the dated folder names.

Test this on sample files: it may be simpler to move the files to a holding folder once they are processed also.


```
@echo off
set "FolderIncoming=D:\Backup\PhoneCalls\All_Calls"
set "FileMask=201*.wav"
set "FolderSorted=D:\Backup\Test"
Set "LameLocation=D:\Program Files (x86)\Lame"
Set "Specific=D:\Backup\Test\Specific"


for %%a in ("%FolderIncoming%\%FileMask%") do (
  echo processing "%%~nxa"
  for /f "tokens=1-3 delims=-" %%b in ("%%~nxa") do (
    for /f "tokens=2 delims=_" %%e in ("%%~nxa") do if %%e geq 0 if %%e leq 99999 (
          md "%FolderSorted%\%%b-%%c-%%d" 2>nul
          "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\%%b-%%c-%%d\%%~na.mp3"
       ) else (
          md "%FolderSorted%\Specific\%%b-%%c-%%d" 2>nul
          "%LameLocation%\lame.exe" -V9 --vbr-new -mm -h -q 0 "%%a" "%FolderSorted%\Specific\%%b-%%c-%%d\%%~na.mp3"
       )
    )
  )
pause
```


----------



## foxidrive (Oct 20, 2012)

mydxbtester said:


> The code works perfectly fine but it does not create the correct tree folder (based on the Extension found in the filename):
> 
> The converted files are as follows:
> 
> ...


Can you show me where you pointed this out in your descriptions?


----------



## mydxbtester (Apr 21, 2015)

Thank you.


----------



## mydxbtester (Apr 21, 2015)

Thank you. No worries, I got it.


----------

