使用批处理自动化解析文件名 [英] Parse file name using batch automation

查看:111
本文介绍了使用批处理自动化解析文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这也许很简单,但是我不是计算机语言专家. 我一直在互联网上寻找解决方案将近3个小时.

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.

假设我所有的mp3文件都以艺术家姓名-Song.mp3的标题" 命名 艺术家:艺术家名称
歌曲:歌曲标题

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

推荐答案

在PushD之后更改启动文件夹以适合您的环境.

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

我的ramdrive a上的示例输出:

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

这篇关于使用批处理自动化解析文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆