.BAT从第一行文本复制和重命名文件以及修改日期?

时间:2013-02-13 07:46:58

标签: batch-file copy

我将如何做到这一点?

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

这有意义吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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等更改为实际位置(希望如此)。

答案 2 :(得分: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任何内容。
  6. 编辑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
    
相关问题