Solved Batch file renaming files but not double

Sygron

New member
Member
Local time
5:45 PM
Messages
59
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Windows 7 Ultimate Edition (64-bit) Service Pack 1
CPU
Intel(R) Core(TM) i7 CPU 4820K @ 3.70GHz
Motherboard
Asus X79 Deluxe
Memory
16 GB
Graphics Card(s)
NVIDIA GeForce GTX 760
Sound Card
None
Monitor(s) Displays
SyncMaster P2270HD
Screen Resolution
1920 x 1080
Hard Drives
C:\ 128 GB SSD
D:\ 2 TB HDD
PSU
550 Watt
Cooling
Water Cooling
Keyboard
Microsoft Natural Ergonomic Keyboard 4000
Mouse
8 Button Wheel Mouse Present
Internet Speed
Low
Antivirus
Microsoft security essentials
Browser
IE11
isn`t there anyone who knows what goes wrong with the script?
Really thought I overlooked something simple...
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Windows 7 Ultimate Edition (64-bit) Service Pack 1
CPU
Intel(R) Core(TM) i7 CPU 4820K @ 3.70GHz
Motherboard
Asus X79 Deluxe
Memory
16 GB
Graphics Card(s)
NVIDIA GeForce GTX 760
Sound Card
None
Monitor(s) Displays
SyncMaster P2270HD
Screen Resolution
1920 x 1080
Hard Drives
C:\ 128 GB SSD
D:\ 2 TB HDD
PSU
550 Watt
Cooling
Water Cooling
Keyboard
Microsoft Natural Ergonomic Keyboard 4000
Mouse
8 Button Wheel Mouse Present
Internet Speed
Low
Antivirus
Microsoft security essentials
Browser
IE11
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Windows 7 Ultimate Edition (64-bit) Service Pack 1
CPU
Intel(R) Core(TM) i7 CPU 4820K @ 3.70GHz
Motherboard
Asus X79 Deluxe
Memory
16 GB
Graphics Card(s)
NVIDIA GeForce GTX 760
Sound Card
None
Monitor(s) Displays
SyncMaster P2270HD
Screen Resolution
1920 x 1080
Hard Drives
C:\ 128 GB SSD
D:\ 2 TB HDD
PSU
550 Watt
Cooling
Water Cooling
Keyboard
Microsoft Natural Ergonomic Keyboard 4000
Mouse
8 Button Wheel Mouse Present
Internet Speed
Low
Antivirus
Microsoft security essentials
Browser
IE11
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Windows 7 Ultimate Edition (64-bit) Service Pack 1
CPU
Intel(R) Core(TM) i7 CPU 4820K @ 3.70GHz
Motherboard
Asus X79 Deluxe
Memory
16 GB
Graphics Card(s)
NVIDIA GeForce GTX 760
Sound Card
None
Monitor(s) Displays
SyncMaster P2270HD
Screen Resolution
1920 x 1080
Hard Drives
C:\ 128 GB SSD
D:\ 2 TB HDD
PSU
550 Watt
Cooling
Water Cooling
Keyboard
Microsoft Natural Ergonomic Keyboard 4000
Mouse
8 Button Wheel Mouse Present
Internet Speed
Low
Antivirus
Microsoft security essentials
Browser
IE11
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Windows 7 Ultimate Edition (64-bit) Service Pack 1
CPU
Intel(R) Core(TM) i7 CPU 4820K @ 3.70GHz
Motherboard
Asus X79 Deluxe
Memory
16 GB
Graphics Card(s)
NVIDIA GeForce GTX 760
Sound Card
None
Monitor(s) Displays
SyncMaster P2270HD
Screen Resolution
1920 x 1080
Hard Drives
C:\ 128 GB SSD
D:\ 2 TB HDD
PSU
550 Watt
Cooling
Water Cooling
Keyboard
Microsoft Natural Ergonomic Keyboard 4000
Mouse
8 Button Wheel Mouse Present
Internet Speed
Low
Antivirus
Microsoft security essentials
Browser
IE11
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom build
OS
Windows 7 Ultimate Edition (64-bit) Service Pack 1
CPU
Intel(R) Core(TM) i7 CPU 4820K @ 3.70GHz
Motherboard
Asus X79 Deluxe
Memory
16 GB
Graphics Card(s)
NVIDIA GeForce GTX 760
Sound Card
None
Monitor(s) Displays
SyncMaster P2270HD
Screen Resolution
1920 x 1080
Hard Drives
C:\ 128 GB SSD
D:\ 2 TB HDD
PSU
550 Watt
Cooling
Water Cooling
Keyboard
Microsoft Natural Ergonomic Keyboard 4000
Mouse
8 Button Wheel Mouse Present
Internet Speed
Low
Antivirus
Microsoft security essentials
Browser
IE11
Back
Top