0

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?

Gorgrak
  • 15
  • 6

3 Answers3

2

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
Miklos Aubert
  • 4,405
  • 2
  • 24
  • 33
  • Thank you for your efforts! I seem to be running into the same problem as I did with the other answer... the script is exiting after I open it right after the ECHO Please specify a file name. – Gorgrak Feb 13 '13 at 11:38
  • That's because I've written the script so that it can work with any file. You're supposed to call it on the command line, like so : myscript.bat exitsave.txt – Miklos Aubert Feb 13 '13 at 11:58
  • Okay, let me just edit the script so that it works for your specific file. – Miklos Aubert Feb 13 '13 at 12:00
  • Done. Assuming your file is really called "exitsave", without any extension. If it's "exitsave.txt", make sure to put the right name in the first FOR instruction. – Miklos Aubert Feb 13 '13 at 12:08
  • Holy Toledo Batman! It works!!! My ultimate goal is to have a single bat file that does multiple things based on user choice (pick 1 to do the copy/rename, pick 2 to exit, etc). How could this be implemented so that the user runs my utility bat that does all the things I want it to? – Gorgrak Feb 13 '13 at 12:11
  • Well, each of the actions have to start with a label, like :choice_1, :choice_2, etc. and end with an EXIT command. Then, at the start of the script, you'd use the SET /P command to read the input and a couple of IFs and GOTOs to do the rest... – Miklos Aubert Feb 13 '13 at 12:24
  • So how do I splice your code into the choice_2 we'll say? the file exitsave.txt already exists so what line of code do I place at the beginning of yours to make it work correctly? i am currently trying to run the bat as itself (not command line executing it like you told me to) and it isn't working. – Gorgrak Feb 13 '13 at 13:01
  • Yep, I hadn't really tested it. It's fixed now, and I think you can adapt it to your needs now. Good luck. – Miklos Aubert Feb 13 '13 at 15:17
  • Thanks a billion man! I didn't even think about using set filename!! you're a flippin' genius. – Gorgrak Feb 14 '13 at 05:28
1

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.

BDM
  • 3,760
  • 3
  • 19
  • 27
  • I think I've failed miserably since I can't get your script to do anything at all. I want to use relative pathing. I am looking to copy "exitsave" into a subfolder called "SAVES" and rename it to 13-02-12--03.43_level2.txt. I've decided I need the creation date (instead of modified date) and the words after the underscore is the complete first line from the file. – Gorgrak Feb 13 '13 at 08:33
  • @Gorg Don't worry, it was probably my confusing code or instructions. While I'm editing the code to suit your specifications, feel free to edit your code (that you got off me) to include renaming it with an extension, otherwise you won't be able to read it. – BDM Feb 13 '13 at 08:36
  • hmm for some reason when i try to execute this script it keeps closing right when I open it? – Gorgrak Feb 13 '13 at 08:48
  • @gorg Well, my code is probably wrong (although by scanning I can't find any errors). Try putting a pause after each line and seeing which line fails. That could give you some explanation. – BDM Feb 13 '13 at 08:55
  • i have determined it is crashing on the delims line. what part I have no idea – Gorgrak Feb 13 '13 at 08:57
  • @Gorg My assumption would be that it can't find the file "exitsave.txt" in the current directory. Either that or there is a typo that I'm oblivious to in there somewhere. – BDM Feb 13 '13 at 09:04
  • completely weird! Thanks for your precious time anyway!! – Gorgrak Feb 13 '13 at 09:19
  • @Gorg no problem, if it's not working we can move this conversation over to chat and continue trouble shooting. – BDM Feb 13 '13 at 09:20
1

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
  1. I cut up his script to just the bare bones of what I needed to splice into my utility file.
  2. My locale is en-US so when the dir /TC command is parsed with the findstr command I end up with MM/DD/YYYY so I moved around my variables accordingly to achieve the desired filename.
  3. My ultimate goal is to distribute the finished utility to friends so I have NO idea how to get around the locale issue for those living in other countries...
  4. I realized that I needed to append the AM/PM signature to my filename so I added a token accordingly.
  5. Finally, I realized that some people may not have the "SAVES" folder created, so to simplify the utility I opted for XCOPY which is MUCH more powerful than copy; it can copy files as well as folder structure or create folder structure as specified. The line of coding I used was taken from https://stackoverflow.com/a/3018371/2067486 so the user doesn't have to tell XCOPY anything.

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
Community
  • 1
  • 1
Gorgrak
  • 15
  • 6