Batch Script Addon Feature

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mydxbtester
Posts: 7
Joined: 23 Feb 2015 07:12

Batch Script Addon Feature

#1 Post by mydxbtester » 12 Apr 2015 04:44

Currently, I have this script up and running:

Code: Select all

@echo off
for %%a in ("%FolderIncoming%\%FileMask%") do (
   set FileName=%%~na
   set TargetYear=!FileName:~0,4!
   set TargetFolder=!TargetYear!\!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"
            )
)


It's working perfectly fine but how can I add the feature where it will place each converted wav file to specific folder location based on IP Extensions. Right now, we 7 extensions (1020, 10201, 10202, 10203, 10204, 10205, 10206) and I want it be be saved in folders as:

Date\Extension\Converted_Files_Here

Examples of saved recording name:

a. 2015-04-10-08-52-15_xxxxxxxxxxxx_10201.mp3
b. 2015-04-10-10-03-52_10202_xxxxxxxxxxx.mp3

Filename (a) should be saved in 2015-04-10\10201\2015-04-10-08-52-15_xxxxxxxxxxxx_10201.mp3

and

Filename (b) should be saved in 2015-04-10\10202\2015-04-10-10-03-52_10202_xxxxxxxxxxx.mp3

It's a bit puzzling since extensions begins, in-between, or ended with _ (underscore).

How can I make this into reality?

Thank you.
Last edited by mydxbtester on 14 Apr 2015 07:14, edited 1 time in total.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch Script Addon Feature

#2 Post by ShadowThief » 12 Apr 2015 06:17

Are the other strings to search for also in seemingly random locations in the file name?

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch Script Addon Feature

#3 Post by Aacini » 12 Apr 2015 06:45

The code below assume that the files are on the disk already, but you may modify it in order to directly create each file in the proper folder with your code:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set extensions=/1020/10201/10202/10203/10204/10205/10206/

for %%a in (*.mp3) do (
   set "filename=%%~Na"
   for %%b in (!filename:_^= !) do (
      if "!extensions:/%%b/=!" neq "%extensions%" set "extension=%%b"
   )
   md "!filename:~0,10!\!extension!" 2>NUL
   move "%%a" "!filename:~0,10!\!extension!\%%a"
)

Antonio
Last edited by Aacini on 13 Apr 2015 07:45, edited 3 times in total.

mydxbtester
Posts: 7
Joined: 23 Feb 2015 07:12

Re: Batch Script Addon Feature

#4 Post by mydxbtester » 12 Apr 2015 06:55

@ShadowThief: Yes, the strings to search (Extension) are in random location within the filename, as shown from the sample filenames. But they can be identified by the (_) underscore character before the Extension number, in between the Extension number, or after the Extension number.

@aacini: If possible, can you integrate your code with the original code I posted. And yes, files I am to convert and move to the folders are stored in:

FolderIncoming=D:\Recordings\Calls\All_Calls

mydxbtester
Posts: 7
Joined: 23 Feb 2015 07:12

Re: Batch Script Addon Feature

#5 Post by mydxbtester » 12 Apr 2015 23:35

Hi Aacini,

I tried integrating your code for testing to mine but it does not work. With your code on it, it still functions the old way in which it creates the old folder path, without the Extension.

Code: Select all

setlocal enabledelayedexpansion
set extensions=/10000/10010/10020/10030/10040/10050/10060/

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!"
            )

)


It should be able to create the correct path then places the recording into that folder.

Extension\Date\Filename_here
Last edited by mydxbtester on 21 Apr 2015 07:24, edited 1 time in total.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch Script Addon Feature

#6 Post by Aacini » 13 Apr 2015 07:43

@mydxbtester:

This is strange. The next line should have a caret before the equal sign in my original code:

Code: Select all

   for %%b in (!filename:_^= !) do (

I fixed the code in my original post. Then, I copied previous line and when I pasted it here, it does NOT include the caret! :evil:

Please, fix this line and test it.

Antonio

mydxbtester
Posts: 7
Joined: 23 Feb 2015 07:12

Re: Batch Script Addon Feature

#7 Post by mydxbtester » 13 Apr 2015 23:25

Thanks a ton, Aacini. The caret did the job:

Can I make a last request before I mark this thread as closed, there's this certain phone numbers XXXXXXXXXXXX and YYYYYYYYYYYY that whenever it calls to certain IP extensions - the script should still convert it to mp3 and not copy to those IP-Extension named folders but rather it is copied to a special folder named as 'Others' inside the TargetFolder (!extension!\!FileName:~0,10!) folder.

Please take note that both numbers are present in the filename, i.e:

2015-04-13-14-45-47_XXXXXXXXXXXX_10020.mp4

XXXXXXXXXXXX - this is the phone number while 10020 is the Extension being called. Precedence will take place of copying 2015-04-13-14-45-47_XXXXXXXXXXXX_10020.mp4 file to 'Others' folder and not to 10020 folder.

Here's what I did so far but it gives me "( was unexpected at this time":

Code: Select all

setlocal enabledelayedexpansion
set extensions=/10010/10020/10030/10040/10050/10060/XXXXXXXXXXXX/YYYYYYYYYYY/
set FolderIncoming=C:\Backup\PhoneCalls\All_Calls
set FileMask=201*.wav
set FolderSorted=C:\Backup\Test
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"
   )


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



   if %extension% == "XXXXXXXXXXXX" (
   REM My commands in here

   ) else (
   if %extension% == "YYYYYYYYYYY" (
   REM My commands in here
   )
   ) else (
            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"
            ))

)

Post Reply