【发布时间】:2013-02-13 07:46:58
【问题描述】:
我该怎么做呢?
我希望将文件 (myfile) 复制到另一个文件夹并使用其第一行以及修改或创建日期/时间重命名 (yy-mm-dd--hh.mm_%firstline%.txt )。
这有意义吗?
【问题讨论】:
标签: batch-file copy
我该怎么做呢?
我希望将文件 (myfile) 复制到另一个文件夹并使用其第一行以及修改或创建日期/时间重命名 (yy-mm-dd--hh.mm_%firstline%.txt )。
这有意义吗?
【问题讨论】:
标签: batch-file copy
我不知道 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 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 等更改为实际位置(希望如此)。
【讨论】:
好的,这是最终结果!哇哦!!!!我只是稍微修改了@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
编辑 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
【讨论】: