Parse file name using batch automation

我们两清 提交于 2020-01-17 01:17:28

问题


This maybe simple enough, but I am not that of an expert to computer languages. I've been stuck searching for a solution for almost 3 hours on the internet.

Assuming all my mp3 files are titled with "Name of Artist - Title of Song.mp3" I would want it to output into a txt file that contants:
Artist: Name of Artist
Song: Title of Song

How do I parse the file name into two parts separated with a hyphen? I've been trying to do some sort of automation with batch files for archiving purposes and here's my code where I'm stuck with:

@echo off
for /r %%a in (*.mp3) do (
(
for %%b in ("%%~na") do echo ^Artist: %%~b
echo ^Song:
)>"%%~dpna.txt"
) 

回答1:


Change the startfolder following PushD to fit your environment.

:: Q:\Test\2018\06\03\SO_50666632.cmd
@echo off
PushD "%USERPROFILE%\Music" || (Echo can't locate folder&Pause&exit /B 1)

for /r %%a in (*.mp3) do (
    if exist "%%~dpna.txt" (
        Echo "%%~dpna.txt" already present, skip
    ) else (
       for /f "tokens=1,*delims=-" %%b in ("%%~na") do (
           echo Artist: %%b
           echo Song  :%%c
       )>"%%~dpna.txt"
    )
)

Sample output on my ramdrive a:

> tree /F
Auflistung der Ordnerpfade
Volumeseriennummer : 5566-7788
A:.
│   Name of Artist - Title of Song.mp3
│   Name of Artist - Title of Song.txt
│
└───Music
        Survivor - Eye of the Tiger.mp3
        Survivor - Eye of the Tiger.txt


> type "Name of Artist - Title of Song.txt"
Artist: Name of Artist
Song  : Title of Song

> type "Music\Survivor - Eye of the Tiger.txt"
Artist: Survivor
Song  : Eye of the Tiger



回答2:


How do I parse the file name into two parts separated with a hyphen?

I would want it to output into a txt file that contains:

Artist: Name of Artist
Song: Title of Song

Use the following batch file as a starting point:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=-" %%i in ('dir /b name*') do (
  echo Artist: %%i 
  echo Song: %%j
 )>>file.txt
endlocal

Example usage:

> dir name*
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

03/06/2018  14:06                 0 Name of Artist - Title of Song.mp3
03/06/2018  14:07                 0 Name of Artist 1 - Title of Song 1.mp3
               2 File(s)              0 bytes
               0 Dir(s)  1,269,011,574,784 bytes free

> test

> type file.txt
Artist: Name of Artist
Song:  Title of Song.mp3
Artist: Name of Artist 1
Song:  Title of Song 1.mp3    
>

I would want it to have a single text file for every mp3 file. Is that possible?

Yes Use the following batch file:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b name*.mp3') do (
  set _filename=%%~dpna.txt
  for /f "tokens=1,2 delims=-" %%i in ("%%a") do (
    echo Artist: %%i 
    echo Song: %%j
    )>!_filename!
  )
endlocal

Example usage:

> dir *.mp3
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

03/06/2018  14:06                 0 Name of Artist - Title of Song.mp3
03/06/2018  14:07                 0 Name of Artist 1 - Title of Song 1.mp3
               2 File(s)              0 bytes
               0 Dir(s)  1,269,022,654,464 bytes free

> test

> type name*.txt

Name of Artist - Title of Song.txt


Artist: Name of Artist 1
Song:  Title of Song 1.mp3

Name of Artist 1 - Title of Song 1.txt


Artist: Name of Artist 1
Song:  Title of Song 1.mp3

  • An A-Z Index of the Windows CMD command line
  • A categorized list of Windows CMD commands
  • dir - Display a list of files and subfolders.
  • for /f - Loop command against the results of another command.


来源:https://stackoverflow.com/questions/50666632/parse-file-name-using-batch-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!