批量重命名.txt文件

时间:2015-09-02 07:46:00

标签: batch-file

我从Project Gutenberg以.txt格式下载了大约34000本书。现在我想通过其内容重命名所有这些内容。例如,每个文本文件都包含其标题"标题"和"作者姓名"所以我想重命名其所有文本文件" Title"和"作者姓名"通过一些命令。

我创建了一个批处理文件。它运行但不重命名文件。这是我的代码:

sudo netstat -lnp | grep 8080

2 个答案:

答案 0 :(得分:0)

您当前的脚本只会打印重命名命令,而不是执行它们。您应该删除using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; namespace girldfriends_demands { class Program { private static string inputString = String.Empty; private static int testCases=0; private static BigInteger[,] indexArray; static void Main(string[] args) { initialize(); printDemand(); Console.ReadLine(); } private static void initialize() { inputString = Console.ReadLine(); testCases = int.Parse(Console.ReadLine()); indexArray = new BigInteger[testCases, 2]; for (int i = 0; i < testCases; i++) { string[] tokens = Console.ReadLine().Split(); indexArray[i, 0] = BigInteger.Parse(tokens[0]); indexArray[i, 1] = BigInteger.Parse(tokens[1]); } } private static void printDemand() { char[] convertedString = inputString.ToCharArray(); for (int i = 0; i < testCases; i++) { BigInteger length=inputString.Length; BigInteger startf, endf; ; BigInteger.DivRem(indexArray[i, 0] - 1,length,out startf); BigInteger.DivRem(indexArray[i, 1]-1,length,out endf); char c=convertedString[((int)startf)]; char e=convertedString[((int)endf)]; if(c==e) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } } } (在检查它产生的内容后):

echo

您的脚本也存在一些格式问题。应该有这样的空格:

echo rename "!fname!" "!nname!"

以后应该没有新行:

for /f "usebackq skip=7 delims=" %%f in ("%%~i") do

答案 1 :(得分:0)

您可以将此作为基础

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Change to source folder
    pushd "e:\test" && (

        rem Where the renamed files will be placed to avoid re-rename
        if not exist renamed\ md renamed

        rem For each input file
        for %%f in (*.txt) do (

            rem Retrieve the data from inside the file
            set "author=" & set "title="
            for /f "tokens=1,* delims=: " %%a in ('
                findstr /b "Author: Title:" "%%~ff"
            ') do if not defined %%a set "%%a=%%b"

            rem If the fields have been retrieved then do the rename
            if defined author if defined title (
                setlocal enabledelayedexpansion
                for /f "delims=" %%a in ("!author! - !title!") do (
                    endlocal
                    echo move "%%~ff" "renamed\%%a%%~xf"
                    rem NOTE: operation is only echoed to console
                    rem       if console output seems correct, then
                    rem       remove the echo command
                )
            )

        )
        rem Done. Return to previous active directory
        popd
    )

当然,文件系统有关于文件名中允许的内容的规则,并且不知道可以找到哪种字符,此代码可能并且可能无法重命名某些文件。