Batch file renaming files but not double


  1. Posts : 59
    Windows 7 Ultimate Edition (64-bit) Service Pack 1
       #1

    Batch file renaming files but not double


    Hello,

    Got another batch file question.
    I think I`m close but I`m overlooking something.
    I have some logging that will get the date of today when I log off.
    But now if I log off twice the file won`t rename, filename already exists.
    So I came with a script to just add a number behind the file if it already exists.

    And here is the problem it doesn`t work but I can`t seems to find out why it won`t work.
    Hopefully an extra pair of eyes can help me out

    This is what I got so far:

    @ echo off
    set jaar=%date:~9,4%
    set maand=%date:~6,2%
    set dag=%date:~3,2%
    set restartvar=1
    if exist "D:\test\HardwareMonitoring %jaar%-%maand%-%dag%.txt" (
    goto restart
    ) else (
    goto shutdown
    )
    :restart
    if exist "D:\test\HardwareMonitoring %jaar%-%maand%-%dag% [%restartvar%].txt"(
    set /A restartvar=%restartvar%+1
    goto restart
    ) else (
    ren "D:\test\HardwareMonitoring.hml" "HardwareMonitoring %jaar%-%maand%-%dag% [%restart%].txt"
    )
    :shutdown
    ren "D:\test\HardwareMonitoring.hml" "HardwareMonitoring %jaar%-%maand%-%dag%.txt"

    The first file that is in that folder just renames so no problems there.
    It seems there is a problem in the restart loop.

    Hope that you can find the solution to my problem
      My Computer


  2. Posts : 59
    Windows 7 Ultimate Edition (64-bit) Service Pack 1
    Thread Starter
       #2

    isn`t there anyone who knows what goes wrong with the script?
    Really thought I overlooked something simple...
      My Computer


  3. Posts : 59
    Windows 7 Ultimate Edition (64-bit) Service Pack 1
    Thread Starter
       #3

    Isn`t there anyway to solve this?

    Maybe someone knows a different way how I can solve the problem, maybe write it to the original log file of the current date that it adds but doesn`t overwrite?
      My Computer


  4. Posts : 59
    Windows 7 Ultimate Edition (64-bit) Service Pack 1
    Thread Starter
       #4

    Oké as apparently nobody is able to find a solution I try it on a different way.
    Trying to copy all the text from the log file to the already existing logfile.
    And deleting the logfile so it will start over:

    @ echo off
    type "D:\test\HardwareMonitoring.hml"
    set jaar=%date:~9,4%
    set maand=%date:~6,2%
    set dag=%date:~3,2%
    set restartvar=1
    set /P logging="D:\test\HardwareMonitoring.hml"

    if exist "D:\test\HardwareMonitoring %jaar%-%maand%-%dag%.txt" (
    goto restart
    ) else (
    goto shutdown
    )
    :restart
    echo Computer is herstart! >> "D:\test\HardwareMonitoring %jaar%-%maand%-%dag%.txt"
    echo %logging% >> "D:\test\HardwareMonitoring %jaar%-%maand%-%dag%.txt"
    del "D:\test\HardwareMonitoring.hml"
    goto ending
    :shutdown
    ren "D:\test\HardwareMonitoring.hml" "HardwareMonitoring %jaar%-%maand%-%dag%.txt"
    :ending

    All goes well in this file except for one thing.
    I can only copy one line of text, the first line to be exact.
    The logfile is alot more then just one line.
    So new question
    Anybody knows a way of copying the content of an entire file to the renamed file?

    Hope there is someone out there that can help me out.
      My Computer


  5. Posts : 59
    Windows 7 Ultimate Edition (64-bit) Service Pack 1
    Thread Starter
       #5

    Come one is there really nobody that knows how I can make this work, any way that works?

    Just don`t want to overwrite the file any working suggestion is welcome, even an idea that I may have missed myself...
      My Computer


  6. Posts : 59
    Windows 7 Ultimate Edition (64-bit) Service Pack 1
    Thread Starter
       #6

    Solved my own problem now.
    Instead of writing it the lines of the new file to the old file.

    I know just let the computer check with start up if there is a log file of the current date.
    If this is the case, the script renames the file back to the original name and thus writing further in this file.
    And rename it again with shutdown.

    If the date doesn`t exist a new file will be created.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 03:44.
Find Us