1

I have a script that scans computers for a certain registry entry. I want to make it to where after the scan the text file that is created, to include the date and time of day as part of the filename (date from the local machine is ok). I have found some examples that seem to do this but either I don't understand their answer or they are not including it in the file name.

Please, if you give an answer to explain what each part is that allows the date to be in the file name. I have seen things about tokens and indices and I do not understand what those are but I am very new to scripting and am trying to learn about this.

Below is the code I have so far for my script.

@echo off

Setlocal
::Test Script
echo This script is used to find a registry setting.(Actual registry patch has been changed for use as an example)
set /p c= Input computer name and push enter: 
set stamp=%date% at %time%
set DateTime=%stamp

call :screen
call :log

pause

:screen
echo.
echo Computer name: %c%
reg query "\\%c%\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Registry Folder\Registry Folder2\" /V "Registry Key" | FIND "Registry Key"
echo.
goto :eof

:log
echo.
echo Registry value for %c% on %stamp% >TextFile.txt
reg query "\\%c%\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Registry Folder\Registry Folder2\" /V "Registry Key" | FIND "Registry Key" >>TextFile.txt
echo.
goto :eof

As you can see I changed the actual registry key I was looking for. Honestly any command can be put there as I am focused on trying to find out how to make the text file that is being created have the correct timestamp within the file name.

Operating System: Windows XP and Windows 7 Note: Please excuse me not being able to give the exact name of the scripting language I am using as the only way I know how to refer to it is "Windows Command Line Script"... I don't even know if that is correct.

Patrick
  • 187
  • 1
  • 4
  • 13
  • Please excuse my code. This is the second script I have ever written and the first without a friend/co-worker helping. I am extremely excited about what I can do with scripting and am trying to be short and concise in it. – Patrick Aug 27 '12 at 19:16
  • The file that is created from the output (i.e. TextFile.txt) will have its created/modified datetime. Can you use that? – Dercsár Aug 27 '12 at 19:22
  • I can see the date and time inside the text file, but not as the name of the text file. When I tried to put %stamp% where I am naming the text file I get an error message. I tried a lot of combinations, just none of them worked. So far only inside the file have I been able to achieve showing the date and time.. and of course from within the command prompt. – Patrick Aug 27 '12 at 19:45
  • Is anyone able to help me with this? I see suggestions from: http://stackoverflow.com/questions/1064557/creating-a-filename-as-a-timestamp-in-a-batch-job but as of yet not a single one seems to work. – Patrick Aug 28 '12 at 19:21

1 Answers1

3

I found the answer mostly with help from a co-worker and also some with various stack overflow posts. I was concerned with not only seeing the answer but what exactly was going on with the commands.

Below sets the DateTime variable to the local date and time in yyyymmdd_hhmmss This code is correctly used being placed in your script.

for /f "skip=1" %%c in ('wmic os get LocalDateTime') do set DateTime=%%c
set DateTime=%DateTime:~0,8%_%DateTime:~8,6%

Explanation: If you run the below command in cmd

wmic os get LocalDateTime

you get the output of (different numbers depending on the date/time you do this):

LocalDateTime
20120829151726.702000-300

Now we need to format this. For this purpose I only care for the date and time. The second line has the date and time in the format yyyymmddhhmmss with some extra gobbly goop. We obviously don't want the first line and we don't want the .702000-300.

To remove this we need to format. By using "skip=1",you are skipping the first line. The :~#,# helps you to choose which characters to use (Don't actually use the # character, I'm only using that as a placeholder so you know a number goes there). The first # is your starting value and the second # is the number of characters after the starting value. We want to use the first 8 characters for the date (yyymmdd) and therefore we have the starting value as 0 (the first #) and the second # as 8.

I then add the underscore and the information needed for the 6 characters for the time (hhmmss).

Within your script you can now have the code to call the DateTime variable.

echo Hello!>>BestFileEver_%DateTime%.txt

Now the text file with the word hello in it will have the file name below:

BestFileEver_20120829_153326.txt

Now obviously the numbers will be different depending on the time you did this.

Patrick
  • 187
  • 1
  • 4
  • 13