How would I go about doing this?
I am wanting the file (myfile) to be copied into a another folder and renamed with its first line as well as the modified or creation date/time (yy-mm-dd--hh.mm_%firstline%.txt).
Does that make sense?
How would I go about doing this?
I am wanting the file (myfile) to be copied into a another folder and renamed with its first line as well as the modified or creation date/time (yy-mm-dd--hh.mm_%firstline%.txt).
Does that make sense?
I don't know if the DIR command always displays dates and times the same way regardless of locale, but assuming this output :
13/02/2013 10:19 8 exitsave.txt
I think the following script should do the job. It also assumes that the first line of your exitsave.txt file doesn't contain any special characters like : or / which are forbidden in a file name.
@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
There are quite a few questions and answers around these sort of topics, although none of them are exactly the same as yours. You should probably search before you post here.
But, since (like I said) I don't think any questions have the same specifics, and it may be hard for someone starting to put them all together, here is the code:
@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
All you need to do is replace file_path
with the path of the original file and file_path2
with the place you want to put it in.
If I've left anything out let me know.
--EDIT--
Changed file_path
and the like to the actual places (hopefully) by more input from the asker.
Okay here is the FINAL result!! WOOHOO!!!! I just modified @Miklos a bit:
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
EDIT 2/15/13: I figured out how to make the file save in army time instead of AM/PM. This allows for better sorting of the files. Put this code right where the XCOPY command is and overwrite the old XCOPY with this new one. !!IMPORTANT!! Make sure to put SETLOCAL ENABLEDELAYEDEXPANSION somewhere at the beginning of the whole script so the FOR commands can process the !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