【问题标题】:.BAT Copy and rename file from first line of text as well as modified date?.BAT 从第一行文本复制和重命名文件以及修改日期?
【发布时间】:2013-02-13 07:46:58
【问题描述】:

我该怎么做呢?

我希望将文件 (myfile) 复制到另一个文件夹并使用其第一行以及修改或创建日期/时间重命名 (yy-mm-dd--hh.mm_%firstline%.txt )。

这有意义吗?

【问题讨论】:

    标签: batch-file copy


    【解决方案1】:

    我不知道 DIR 命令是否总是以相同的方式显示日期和时间,而不管语言环境如何,但假设输出如下:

    13/02/2013  10:19                 8 exitsave.txt
    

    我认为下面的脚本应该可以完成这项工作。它还假定您的 exitsave.txt 文件的第一行不包含任何特殊字符,例如文件名中禁止使用的 : 或 /。

    @echo off
    
    REM  List the choices here.
    ECHO 1. Save and rename.
    ECHO 2. Do nothing and quit.
    
    REM  Set the number of choices (ugly but it's not the worst thing here)
    SET numchoice=2
    
    :mainloop
    
        SET /P choice= Enter your choice : 
    
        FOR /L %%i IN (1, 1, %numchoice%) DO (
            IF "%choice%"=="%%i" (
                GOTO :choice_%%i
            )
        )
        GOTO mainloop
    
    :choice_1
    
        SET filename=exitsave
        SET firstline=
    
        REM Read the first line of the file
        FOR /F "tokens=1* usebackq delims=" %%a IN (%filename%) DO (
            SET firstline=%%a
            REM  exit after reading the first line, or the FOR loop will read the entire file
            GOTO outofloop
        )
    
        :outofloop
        REM DIR /TC %1 lists the file and its creation date/time,
        REM we use FINDSTR to filter the output to just the line we need
        REM Assuming a DD/MM/YYYY format, this FOR loop splits the line into :
        REM %%i = DD, %%j = MM, %%k = YYYY and the rest of the line...
    
        FOR /F "delims=/ tokens=1,2,3* usebackq" %%i IN (`DIR /TC %filename% ^| FINDSTR %filename%`) DO (
    
            REM We need to split the %%k variable again, this time with the space delimiter :
            REM %%a = YYYY, %%b = rest of the line
    
            FOR /F "tokens=1,2*" %%a IN ("%%k") DO (
    
                REM Assuming a HH:MM format, again we split the %%b variable using the ":" delimiter :
                REM %%x = HH, %%y = MM
    
                FOR /F "delims=: tokens=1,2" %%x IN ("%%b") DO (
    
                    REM finally, we can generate our new filename
                    COPY %filename% SAVES\%%a-%%j-%%i-%%x.%%y--%firstline%.txt
    
                )
            )
        )
        ECHO Done!
        PAUSE
        EXIT
    
    :choice_2
    
        REM  Put your code here
    
        PAUSE
        EXIT
    

    【讨论】:

    • 感谢您的努力!我似乎遇到了与其他答案相同的问题...在 ECHO 之后立即打开脚本后退出请指定文件名。
    • 那是因为我已经编写了脚本,以便它可以处理任何文件。您应该在命令行上调用它,如下所示:myscript.bat exitsave.txt
    • 好的,让我编辑脚本,使其适用于您的特定文件。
    • 完成。假设您的文件真的被称为“exitsave”,没有任何扩展名。如果是“exitsave.txt”,请确保在第一条 FOR 指令中输入正确的名称。
    • 圣托莱多蝙蝠侠!有用!!!我的最终目标是拥有一个 bat 文件,它可以根据用户选择执行多项操作(选择 1 进行复制/重命名,选择 2 退出等)。如何实现这一点,以便用户运行我的实用 bat 来做我想做的所有事情?
    【解决方案2】:

    围绕这些主题有很多问题和答案,尽管没有一个与您的完全相同。您可能应该在此处发布之前进行搜索。

    但是,因为(就像我说的)我认为任何问题都没有相同的细节,而且开始将它们全部放在一起可能很难有人开始将它们放在一起,这里是代码:

    @echo off & setlocal enabledelayedexpansion
    for /f "delims=" %%i in (exitsave.txt) do (
        copy exitsave.txt %cd%\SAVES
        set title=!date:~12,2!-!date:~7,2!-!date:~4,2!--!time:~0,2!.!time:~3,2!_%%i && goto next
    :next
    ren %cd%\SAVES\exitsave.txt %title%.txt
    if exist file_path echo procedure completed successfully
    pause
    exit
    

    您只需将file_path 替换为原始文件的路径,将file_path2 替换为您要放置的位置。

    如果我遗漏了什么,请告诉我。

    --编辑-- 通过提问者的更多输入,将file_path 等更改为实际位置(希望如此)。

    【讨论】:

    • 我觉得我失败得很惨,因为我根本无法让你的脚本做任何事情。我想使用相对路径。我希望将“exitsave”复制到一个名为“SAVES”的子文件夹中,并将其重命名为 13-02-12--03.43_level2.txt。我已经决定我需要创建日期(而不是修改日期),并且下划线后面的单词是文件中完整的第一行。
    • @Gorg 别担心,这可能是我令人困惑的代码或说明。当我正在编辑代码以符合您的规范时,请随时编辑您的代码(您从我这里得到的)以包括使用扩展名重命名它,否则您将无法阅读它。
    • 嗯,由于某种原因,当我尝试执行此脚本时,它会在我打开它时一直关闭?
    • @gorg 好吧,我的代码可能是错误的(尽管通过扫描我找不到任何错误)。尝试在每一行之后暂停一下,看看哪一行失败。这可以给你一些解释。
    • 我已经确定它在 delims 线上崩溃了。我不知道哪一部分
    【解决方案3】:

    好的,这是最终结果!哇哦!!!!我只是稍微修改了@Miklos:

        SET filename=exitsave
        SET firstline=
    
    REM Read the first line of the file
    FOR /F "tokens=1* usebackq delims=" %%a IN (%filename%) DO (
        SET firstline=%%a
        REM  exit after reading the first line, or the FOR loop will read the entire file
        GOTO outofloop
    )
    
    :outofloop
    REM DIR /TC %1 lists the file and its creation date/time,
    REM we use FINDSTR to filter the output to just the line we need
    REM Assuming a MM/DD/YYYY format, this FOR loop splits the line into :
    REM %%i = MM, %%j = DD, %%k = YYYY + the rest of the line...
    
    FOR /F "delims=/ tokens=1,2,3* usebackq" %%i IN (`DIR /TC %filename% ^| FINDSTR %filename%`) DO (
    
        REM So we need to split the %%k variable, this time with the space delimiter
        REM %%a = YYYY, %%b = HH:MM, %%c = rest of the line (for determining AM/PM)
    
        FOR /F "tokens=1,2,3*" %%a IN ("%%k") DO (
    
            REM Assuming a HH:MM format, again we split the %%b variable using the ":"
            delimiter
            REM %%x = HH, %%y = MM
    
            FOR /F "tokens=1,2 delims=:" %%x IN ("%%b") DO (
    
                REM finally, we can generate our new filename
    
          ECHO F | XCOPY %filename% "SAVES\(%%a-%%i-%%j@%%x.%%y%%c)__%firstline%.txt" /Q /Y
    
                )
            )
        )
    )
    PAUSE
    
    1. 我将他的脚本剪切成我需要拼接到我的实用程序文件中的基本内容。
    2. 我的语言环境是 en-US,因此当使用 findstr 命令解析 dir /TC 命令时,我最终得到 MM/DD/YYYY,因此我相应地移动了我的变量以获得所需的文件名。
    3. 我的最终目标是将完成的实用程序分发给朋友,所以我不知道如何解决居住在其他国家/地区的人的语言环境问题...
    4. 我意识到我需要将 AM/PM 签名附加到我的文件名中,因此我相应地添加了一个令牌。
    5. 最后,我意识到有些人可能没有创建“SAVES”文件夹,所以为了简化实用程序,我选择了比复制功能强大得多的 XCOPY;它可以复制文件以及文件夹结构或创建指定的文件夹结构。我使用的代码行取自 https://stackoverflow.com/a/3018371/2067486,因此用户无需告诉 XCOPY 任何内容。

    编辑 2/15/13:我想出了如何在军队时间而不是 AM/PM 中保存文件。这样可以更好地对文件进行排序。将此代码放在 XCOPY 命令所在的位置,并用新的 XCOPY 覆盖旧的 XCOPY。 !!重要的!!确保将 SETLOCAL ENABLEDELAYEDEXPANSION 放在整个脚本开头的某个位置,以便 FOR 命令可以处理!变量!

    SET clock=%%x
    IF "%%c" == "PM" (
         SET /A army=!clock! + 12
    ) ELSE (
         SET /A army=0!clock! & SET army=!clock:~-2!
    )
    
    ECHO F | XCOPY %filename% "SAVES\(%%a-%%i-%%j@!army!.%%y)%firstline%.txt" /Q /Y
    

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 2021-07-21
      • 2016-07-26
      • 2013-01-02
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2011-06-22
      • 2021-02-21
      相关资源
      最近更新 更多