Page 1 of 1

renaming a file from A - B.MP3 to B - A.MP3

Posted: 04 Sep 2017 21:30
by nnnmmm

Code: Select all

suppose a file name has these chars besides the alphabet !ab & , ( ' cd - 12 ! & , '34.mp3
and i want to rename a file as  12 ! & , '34 - !ab & , ( ' cd.mp3
there is only one " - " in each file.


simply saying, from A - B.MP3 to B - A.MP3
how can i do it?

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 00:01
by Kvc
Hi nnnmmm,

I'm not sure if the script will work fine on the special characters. But, it will work fine on the alphabets. And, We need something to start for achieving the goal. SO, I've started from first doing this for simple filenames. Hope, this will help.

If This is the output you wanted from script - then I will work on the next step. :)

Image

Code: Select all

@Echo off
SetLocal EnableDelayedExpansion
Set "_File=%~n1"
Set "_Ext=%~x1"

For /F "Tokens=1,2* delims=-" %%A In ("%_File%") Do (
   Set _String=%%B-%%A!_Ext!
   Echo !_String: =!
   )

Goto :EOF


This is kind of small plugin... Use it as a command - while keeping the test.bat file in the working directory or in any pre-defiend path. :)

*Edit 1: The Image and codebox didn't appered as expected in the comment. SO, edited to fix that. :)
*Edit 2: Ohh... Forgot to untick the 'Disable BBCode' option while commenting.. wasted lot of time. (still learning)

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 01:32
by nnnmmm
yes, hope that it can do with space, comma, &, !, '

!abc - simon & garfield.mp3
simon & garfield - !abc.mp3

gets tough, tough gets going - billy.mp3
billy - gets tough, tough gets going.mp3

sir, i can dance '99 - baccara.mp3
baccara - sir, i can dance '99.mp3

i want to reposition mp3 names at times

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 05:27
by Hackoo
You can try with this script :

Code: Select all

@echo off
Title Reverse String
set "InputFile=list.txt"
set "OutPutFile=list2.txt"
set "Tmpvbs=%~n0.vbs"
(
   echo Data = WScript.Arguments(0^)
   echo Data = StrReverse(Data^)
   echo wscript.echo Data
)>"%Tmpvbs%"

If exist "%OutPutFile%" Del "%OutPutFile%"
setlocal EnableDelayedExpansion
@for /f "tokens=*" %%a in ('Type "%InputFile%"') do (
   Call :Reverse "%%~na"
   echo !Newstring!%%~xa >>"%OutPutFile%"
)
If exist "%Tmpvbs%" Del "%Tmpvbs%"
start "" "%OutPutFile%" & exit
::********************************************
:Reverse <InputData>
@for /f "tokens=*" %%i in ('cscript //nologo "%Tmpvbs%" "%~1"') do (
   set "NewString=%%~ni"
)
exit /b
::********************************************

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 05:49
by pieh-ejdsch
hello nnnmmm,

with a FOR /f loop, the whole will be nothing, because the apostrophe can not be processed in the output from the DIR at the loop.
furthermore, this issue will also go into the groats for characters from another codepage.
thus the use of the Delimiter with these filenames is not easily possible.
with a temporary file, this problem also exists.
rename mp3

What you can do around this is a FOR loop (without Option /f ) which always prints the name correctly regardless of the codepage set.
You can create the name parts by replacing it with a directory name [\].
the loop is on the fly and can however rename a renamed file again while renaming files and rename at the same time rename and rename.
In order to keep these many small things in mind, a temporary folder is used.
like this script

Code: Select all

@echo OFF
setlocal
set     "process=echo ren"
 rem set process=ren
set      "Folder=d:\mmtest"
set       "Files=*.mp3"
set   "Recursive=1"
 rem Setting Delim
set "Delim= - "

 rem this is a temporary Folder for Sorting and renaming only
set "tempFolder=%temp%\nonExistingFolder"

 rem use call :seperate with copies for testing
  rem call :seperate
if :SUB==only * You want test with seperate Files * & @(
:seperate
rd /s /q "%folder%" 2>nul >nul
md "%folder%"
robocopy /create /mir D:\users\public\music "%folder%" /njh /fp /ndl /nfl /njs
exit /b
)

 rem END Settings

rd /s /q "%tempfolder%" 2>nul >nul
md "%tempfolder%"
call :subST_LW Q "%%folder%%"
call :subST_LW R "%%tempfolder%%"
if errorlevel 3 pause &exit /b 1
set Substlist= %Q% %R%
echo temporary Folder is filled ...
robocopy /create /mir %Q% %R% /njh /njs /ndl /nfl
pushD %R%
echo Rename ...
call :process
popD
for %%S in (%SubstList% )do subst /D %%S
rd /S /Q "%tempfolder%"
echo END script
pause
exit /b

:process
for %%F in (%files%)do (
  set "fileName=%%F"
  setlocal enabledelayedexpansion
  for %%X in ("\!filename:%Delim%=\!")do (
    endlocal
    if NOT exist %Q%%%~pnxF >&2 echo file %Q%%%~pnxF not found!
    if exist %Q%%%~pnxF (
      for %%U in ("%%~pX.") do %process% "%Q%%%~pnxF" "%%~nX%delim%%%~nxU%%~xX"
    )
  )
)
if defined recursive (
  for /D %%D in (*)do @( pushd %%D
    if not errorlevel 1 (
      call :process
      popD
    )
  )
)
@exit /b

:subST_LW outVar_LW VarPathToSubst
set "%1="
set "FreeDrive=Z Y X W V U T S R Q P O N M L K J I H G F E D C B A"
for %%i in (%FreeDrive%) do @if not defined %1 >nul subst %%i: %2 &&set %1=%%i:
if not defined %1 1>&2 echo Kein Freies Laufwerk gefunden!& exit /b 3
exit /b
Oh, and
and ask your questions a bit better and grab the few lines of your code so far here to do something to contribute.

Phil

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 06:40
by nnnmmm

Code: Select all

@echo OFF
setlocal
set     "process=echo ren"
 rem set process=ren
set      "Folder=m:\mmtest"
set       "Files=*.mp3"
set   "Recursive=1"
 rem Setting Delim
set "Delim= - "

 rem this is a temporary Folder for Sorting and renaming only
set "tempFolder=m:\zzz1"

 rem use call :seperate with copies for testing
  rem call :seperate
if :SUB==only * You want test with seperate Files * & @(
:seperate
rd /s /q "%folder%" 2>nul >nul
md "%folder%"
robocopy /create /mir m:\xxxx "%folder%" /njh /fp /ndl /nfl /njs
exit /b


the instruction as to how to use was not clear, and i changed a few heading to make them all inside m: drive
i copied some mp3's in m:\mmtest and then i put your batch file in the same dir in m:\mmtest, then i made a temp dir m:\zzz1
and i made the robocopy's direction to m:\xxxx and then i ran your batch, and nothing happened.

it is too bad if the script is too difficult

code pages should be ok, i will try only on the english named mp3s' with few symbols like ! & ( ) , '

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 08:33
by pieh-ejdsch
No not quite so.

if I create a variable with the name tempFolder and explain a line about it for what this should be used somewhere in the batch again emerge and be used. For that there are variables.
Perhaps I have expressed something unhappy, but this variable represents a folder - which is created in the Temporary Windows directory and is deleted again at the end of this batch. Take your fingers away from this variable! Do not touch!

The variable Folder should provide your work folder with test data or your original data to be recalculated.
The variable recursive is self-explanatory

the note - call: seperate - means nothing else than the "Robocopy 0 byte files" specified in "your" work folder. So you do not need to create test data yourself. You only have to remove the REM at - call: seperate -
With the function: seperate only the folder to be copied has to be specified. For me this was "D:\users\public\music", this folder has copied the script into the work folder (variable folder) as 0 byte test data.
So if you do not want to do a test, then comment out the line "call: seperate" - otherwise the data in the work folder will be deleted. before and after. so be careful!

Phil

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 12:48
by barnabe0057
Hi, this is another way of proceeding :

Code: Select all

@echo off
setlocal enableextensions

cd /d %~1 || exit /B 1

for /f "delims=" %%H in ('dir /b /a-d') do (
   set file=%%H
   call :new_name file
)

endlocal
exit /B

:new_name
setlocal enabledelayedexpansion

for /F "tokens=1,2,3 delims=-." %%A in ('echo "!%~1!"') Do (2>nul ren "!%~1!" "%%~B-%%~A.%%~C")

endlocal
goto :eof


To use it, you have to put the file in a folder and drag the folder on the script.

There is only 1 limitation : if filename contains exclamation points, those will be vanished

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 05 Sep 2017 17:48
by nnnmmm
barnabe0057, it worked.
so far, i found 1 minor problem,
delims may be " - " and not "-", this would fix

Code: Select all

abc - 123.mp3 becoming
 123-abc .mp3

Re: renaming a file from A - B.MP3 to B - A.MP3

Posted: 06 Sep 2017 04:27
by pieh-ejdsch
Hello,

I have even adapted the script for you. and reduced to the essentials. Error messages has been added.

Code: Select all

@echo OFF
setlocal
set     "process=echo ren"
  rem set process=ren
set      "Folder=d:\mp3 files"

 rem Setting Delim
set       "Delim= - "
set       "Files=*%delim%*.mp3"
set   "Recursive=1"


 rem END Settings

 rem this is a temporary Folder for Sorting and renaming only
 rem  don't remove
set "tempFolder=%temp%\nonExistingFolder"

rd /s /q "%tempfolder%" 2>nul >nul
md "%tempfolder%"
call :subST_LW Q "%%folder%%"
call :subST_LW R "%%tempfolder%%"
if errorlevel 3 pause &exit /b 1
set Substlist= %Q% %R%
echo temporary Folder is filled ...
robocopy /create /mir %Q% %R% "%Files%" /njh /njs /ndl /nfl
pushD %R%
echo Rename ...
call :process
popD
for %%S in (%SubstList% )do subst /D %%S
rd /S /Q "%tempfolder%"
echo END script
pause
exit /b

:process
for %%F in ("%files%") do (
  set "fileName=%%~nF"
  setlocal enabledelayedexpansion
  set "part2=!fileName:*%delim%=!"
  set "part1=!fileName:%delim%=" "!"
  for %%X in ("!part2!%delim%!part1!") do if .!! equ . (
    endlocal
    if NOT exist %Q%%%~pnxF >&2 echo file %Q%%%~pnxF not found!
    if exist %Q%%%~pnxF %process% "%Q%%%~pnxF" "%%~X%%~xF"
  )
)
if defined recursive (
  for /D %%D in (*)do @( pushd %%D
    if not errorlevel 1 (
      call :process
      popD
    ) else >&2 echo %%D
  )
)
@exit /b

:subST_LW outVar_LW VarPathToSubst
set "%1="
set "FreeDrive=Z Y X W V U T S R Q P O N M L K J I H G F E D C B A"
for %%i in (%FreeDrive%) do @if not defined %1 >nul subst %%i: %2 &&set %1=%%i:
if not defined %1 1>&2 echo Kein Freies Laufwerk gefunden!& exit /b 3
exit /b


Phil