1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Batch script If-Else and string comparisson

Discussion in 'DOS/Other' started by mydxbtester, Apr 21, 2015.

Thread Status:
Not open for further replies.
Advertisement
  1. mydxbtester

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

    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.
     
  2. foxidrive

    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"
    )
    
     
  3. mydxbtester

    mydxbtester Thread Starter

    Joined:
    Apr 21, 2015
    Messages:
    9

    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.
     
  4. foxidrive

    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?
     
  5. mydxbtester

    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.
     
  6. foxidrive

    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
    
     
  7. mydxbtester

    mydxbtester Thread Starter

    Joined:
    Apr 21, 2015
    Messages:
    9
    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
     
  8. foxidrive

    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.
     
  9. mydxbtester

    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.
     
  10. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Why did you use mp4 files in your question?
     
  11. mydxbtester

    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.
     
  12. foxidrive

    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.
     
  13. mydxbtester

    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. 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.
     
  14. foxidrive

    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
    
     
  15. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Can you show me where you pointed this out in your descriptions?
     
  16. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1146951

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice