Solved: Batch script If-Else and string comparisson

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

mydxbtester

Thread Starter
Joined
Apr 21, 2015
Messages
9
I have a difficulty in matching strings from filename. Say, I have this recording files as follow:

a. [FONT=&quot]2015-04-13-14-45-47_XXXXXXXXXXXX_20010.mp4
[/FONT]
b. [FONT=&quot]2015-04-13-15-46-48_[/FONT][FONT=&quot][FONT=&quot]YYYYYYYYYYYY_[/FONT]20015.mp4[/FONT]
c. [FONT=&quot][FONT=&quot]2015-04-13-16-47-49_20016_AAAAAAAAA.mp4[/FONT]
[/FONT]

20010, 20015, and 20016 are IP Phone Extensions
[FONT=&quot]XXXXXXXXXXXX[/FONT] and [FONT=&quot]YYYYYYYYYYYY [/FONT]are specific phone numbers
[FONT=&quot][FONT=&quot]AAAAAAAAA [/FONT][/FONT]is a random called number

What I wanted is for all recordings to be place to specific folder, everytime [FONT=&quot]XXXXXXXXXXXX or [/FONT][FONT=&quot]YYYYYYYYYYYY [/FONT]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:

Code:
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

Banned
Joined
Oct 20, 2012
Messages
793
Code:
@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

Thread Starter
Joined
Apr 21, 2015
Messages
9
Code:
@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"
)

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

Banned
Joined
Oct 20, 2012
Messages
793
Did you try it with copies of the files you showed, in an empty folder?
Did it work?
 

mydxbtester

Thread Starter
Joined
Apr 21, 2015
Messages
9
This is my actual working code:

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

Banned
Joined
Oct 20, 2012
Messages
793
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.

Code:
@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

Thread Starter
Joined
Apr 21, 2015
Messages
9
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.

Code:
@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
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?


Code:
@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

Banned
Joined
Oct 20, 2012
Messages
793
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

Thread Starter
Joined
Apr 21, 2015
Messages
9
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.
 

mydxbtester

Thread Starter
Joined
Apr 21, 2015
Messages
9
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

Banned
Joined
Oct 20, 2012
Messages
793
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

Thread Starter
Joined
Apr 21, 2015
Messages
9
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. [FONT=&quot]2015-04-13-14-45-47_XXXXXXXXXXXX_20010.mp4
[/FONT]
b. [FONT=&quot]2015-04-13-15-46-48_[/FONT][FONT=&quot][FONT=&quot]YYYYYYYYYYYY_[/FONT]20015.mp4[/FONT]
c. [FONT=&quot][FONT=&quot]2015-04-13-16-47-49_20016_AAAAAAAAA.mp4[/FONT][/FONT]

20010, 20015, and 20016 are IP Phone Extensions
[FONT=&quot]XXXXXXXXXXXX[/FONT] and [FONT=&quot]YYYYYYYYYYYY [/FONT]are specific phone numbers
[FONT=&quot][FONT=&quot]AAAAAAAAA [/FONT][/FONT]is a random called number

What I wanted is for all recordings to be place to specific folder, everytime [FONT=&quot]XXXXXXXXXXXX or [/FONT][FONT=&quot]YYYYYYYYYYYY [/FONT]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 [FONT=&quot]XXXXXXXXXXXX [/FONT]and [FONT=&quot]YYYYYYYYYYYY[/FONT] in the filename will go to their own folders (Extension-named folder) while those with [FONT=&quot]XXXXXXXXXXXX [/FONT]and [FONT=&quot]YYYYYYYYYYYY[/FONT] will be in "Specific" folder.

Can you help me check on this.
 

foxidrive

Banned
Joined
Oct 20, 2012
Messages
793
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.

Code:
@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

Banned
Joined
Oct 20, 2012
Messages
793
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:

a. [FONT=&quot]2015-04-13-14-45-47_XXXXXXXXXXXX_20010.[FONT=&quot]wav[/FONT]

- Since this contains [/FONT][FONT=&quot]XXXXXXXXXXXX[/FONT] [FONT=&quot]in the filename[/FONT], it should be converted and to mp3 and saved to "Specific" folder.

i.e: D:\Backup\Test\Specific\20010\2015-04-13\[FONT=&quot]2015-04-13-14-45-47_XXXXXXXXXXXX_20010.[FONT=&quot]mp3[/FONT][/FONT]
[FONT=&quot]
[/FONT]b. [FONT=&quot]2015-04-13-15-46-48_[/FONT][FONT=&quot][FONT=&quot]YYYYYYYYYYYY_[/FONT]20015.[FONT=&quot]wav[/FONT][/FONT]

Same with this file, it should be saved under:

D:\Backup\Test\Specific\20015\2015-04-13\[FONT=&quot]2015-04-13-15-46-48_YYYYYYYYYYYY.[FONT=&quot]mp3[/FONT][/FONT]
Can you show me where you pointed this out in your descriptions?
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top