Make folder from part of filename & Merge files into it

ORIGINAL DOS BATCH - Works! With help from Foxidrive

[code]@echo off
FOR %%a in (".") do call :next "%%a"
goto :eof
:next
for /f "delims=-" %%b in ("%~n1") do set "folder=%%b"
if "%folder%"=="%~n1" goto :EOF
if "%folder:~-1%"==" " set "folder=%folder:~0,-1%"
md "%folder%" 2>nul
set c=-1
set "name=%~n1"
:: remove existing " (cNNN)"
echo "%name%|" |findstr /r /c:" (c[0-9]*)|">nul && call :remove

:loop
set /a c=c+1
set num= (c%c%)
if %c% EQU 0 (set num=)
if exist "%folder%%name%%num%%~x1" goto :loop
echo processing %1
MOVE /-Y "%~1" "%folder%%name%%num%%~x1" >nul
goto :EOF

:: Search and Replace - strings
:remove
call :Search_and_replace "%name%"
for /f "delims=" %%c in ('cscript /nologo _.vbs') do set "name=%%c"
)
del _.vbs
goto :EOF

:Search_and_replace
set s=regex.replace("%~1","$1")

_.vbs echo set regex=new regexp

_.vbs echo regex.global=True
_.vbs echo regEx.IgnoreCase=True
_.vbs echo regex.pattern="(.) (c[0-9])"
_.vbs echo wscript.stdOut.write %s%[/code]

What it Does:
[ul][li]Makes folder from part of filename :: in this case everything before the first dash.[/li]
[li]Files are then move into folder. [/li]
[li]If folder already exists the files are merged into existing folder. [/li]
[li]If file already exists with the same name, the files are renamed with tag to avoid conflict. (c1), (c2)[/li]
[li]NO files are overwritten.[/li][/ul]
Constraints:
[ul]
Author - [Series 01] - Title (tag).ext
Singer - [Album] - 01 Song Title.ext

DELIMINATORS: dash, square bracket and parenthesis. These are designated and not used elsewhere. (so no hyphened words in the author or singer names)

[/ul]
Example:

BEFORE:
Jane Austen - Emma.epub
Jane Austen - Pride And Prejudice.epub
Jane Austen - Pride And Prejudice.jpg

AFTER:
Jane Austen/
[ul]Jane Austen - Emma.epub
Jane Austen - Pride And Prejudice.epub
Jane Austen - Pride And Prejudice.jpg
Jane Austen - Pride And Prejudice (c1).epub
Jane Austen - Pride And Prejudice (c2).epub
[/ul]

ISSUE:
Help! I can't get it work as a button. Guessing a I need to alter the code to fit in DirOpus environment ?

Similar Topics:
Create folders based on filenames then move files into them
Button to move files in folder named from file

How are you trying to run it at the moment? What goes wrong?

Using a rename script rather than a batch file would probably be more robust, but you can call a batch file from an Opus button if you wish.

My scripting ability is not that extensive. My reference books are always at my elbows. DOS is not my natural habitat. My original script was written in "bash".

My DOS script works fine from the command prompt but when I make into a button it fails. So it is user error :smiley: in my button making. (new to DirOpus)

The reason I didn't use the built rename was.. because there's a quirk Directory Opus when renaming inline. If a file already has a numerical data in parentheses the DirOpus will increment it thinking it's its own inline numbering sequence. If there is character with it, inline-renaming will split on the last parenthesis.

RENAME INLINE NUMBERING QUIRK

What Happens:
filename (2011).txt
filename (2012).txt
filename (v1.0).txt
filename (v1. (1).txt

What should happen:
filename (2011).txt
filename (2011)(1).txt
filename (v1.0).txt
filename (v1.0)(1).txt

A Better Mouse Trap:
If there is a way to do the all functions I want with the built-in Opus command, I'm all for it . I am not that attached to my scripts. :laughing: The INLINE numbering problem needs a work-around though. Haven't a clue on how to do that.

MY ORIGINAL LINUX BASH VERSION

#!/bin/bash for i in $(ls | sed 's/ /~/g'); do FILENAME=$(echo "$i" | sed 's/~/ /g') if ! [ -d "$FILENAME" ]; then AUTHORNAME=$(echo "$FILENAME" | awk -F"-" '{ print $1 }') AUTHORNAME=$( echo "$AUTHORNAME" | sed -r 's/ +$//g' ) if ! [ -d "$AUTHORNAME" ]; then mkdir "$AUTHORNAME" fi COUNT=1 EXTENSION=$(echo "$FILENAME" | awk -F. '{ print $NF }') NEWNAME=$(echo "$FILENAME" | sed -r "s/\.$EXTENSION$//g") TEMPNAME=$NEWNAME while [ -e "$AUTHORNAME/$NEWNAME.$EXTENSION" ]; do NEWNAME="$TEMPNAME(C$COUNT)" COUNT=$((COUNT+1)) done mv "$FILENAME" "$AUTHORNAME/$NEWNAME.$EXTENSION" fi done

Caveates:
Linux Bash scripts works best because takes into consideration possible extra spaces, while DOS is fussy about it. DOS will not move a file if there are 2 spaces before the dash.
DOS script still has an issue with hidden files beginning with a tilde ~. Recently discovered this when the script found a stray ~temp.doc file.

The "Button" version when clicked; you see the black DOS screen flash briefly. Nothing happens. No folder or files are move or changed.

Try replacing the @echo off line (it's not needed) with @externalonly (so that the "set" command isn't interpreted as the Opus set command instead of the DOS one).

Failing that, it might make sense to keep the batch in a .bat file and call that from the button, instead of putting it inside the button. (Opus will split MS-DOS Batch Function buttons into multiple smaller batch files if there are any Opus commands in between the external/DOS commands, so you have to be careful if you want to use batch loops and gotos in the command itself. Easier to avoid them.)

(Or, even better, use a rename script, but that would mean re-writing things, of course.)