我将如何做到这一点?
我希望将文件(myfile)复制到另一个文件夹中并使用第一行重命名以及修改或创建日期/时间(yy-mm-dd - hh.mm_%firstline%.txt )。
这有意义吗?
答案 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
编辑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