0

我该怎么做呢?

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

那有意义吗?

4

3 回答 3

2

我不知道 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
于 2013-02-13T09:45:33.287 回答
1

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

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

@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您要放入的位置。

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

--EDIT--file_path通过提问者的更多输入(希望)更改为实际位置等。

于 2013-02-13T08:10:21.100 回答
1

好的,这是最终结果!哇哦!!!!我刚刚修改了@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 命令可以处理 !variables!

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-02-14T10:34:54.677 回答