I am driving for the next five hours but as soon as I get a chance I will try this out. Thank you. Marcus
Main Topics
Browse All TopicsI am trying to create a text file using a batch file. I format the text file then open it for the user to make entries into. I want to save the file name as follows.
computername_InstallerName
I have tried a number of ways and it seems to save it and while I may get the name to work when I go to open it it does not find it. Is that because it is always looking for the current time where my time does not match it?
Can this be done or am I trying to do the impossible?
I appreciate all suggestions.
Marcus
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi Marcus,
varSaveFile is your only file with a date and time portion in it. But is only written once, and that is a empty line. On every other occasion you use varNOTSigned, which is a fixed name containing the username as variable part.
However, even if not intended, this should open a notepad with the "correct" file containing all the stuff you put in it. And then send it.
As t0t0 tried to tell you already, %date% and %time% cannot be used without transformation in filenames, as there are slashes and colons, which are not allowed as part of filenames.
I for myself prefer to use a construction of
I have done as instructed and my date shows up as ...
MARCUS-MOBILE_Joe_Poe_Phas
So, it is putting the Year, then day, then the Month for the date portion. And when I go to send the file it finds it now. If you notice that the name portion has an underscore added to it. If I leave a blank space it can no longer find the file and saves the file into two files. So I have had to instruct the user to add the underscore when typing in their name. Can anyone explain why this is or how I can get around it? I will add my code so you can see what I am doing.
I accept input for agreeing to the use of the file. It will accept either upper or lower case. Is there a way to change it to upper case?
Again, I appreciate your mentoring.
Regards,
Marcus
You can create a underscore in the filename easily by using
set installer=%installer: =_%
so the users do not need to use underscores.
However, why the space is a problem is not obvious to me. You are using the quoted filename all way long, so there shouldn't be any issue with it ...
I see you did not adopt my echo redirection recommendation. Maybe you like that one more:
>> %varSaveFile% echo.
>> %varSaveFile% echo Don't echo this at home!
>> %varSaveFile% echo.
It puts the file redirection into direct attention, and looks better (more schematic).
Regarding the "agree" part:
It has a flaw, because ANY answer but Y or y will result in "DoesNotAgree". It should only allow for a clear Y or N, IMHO. You could do that with this code:
Would you explain the set installer=%installer: =_%
How does it know to look for the space in this variable to put the underscore symbol?
I did not understand the echo redirection. Are you saying you can say... the following
echo. >> somefile.txt
echo This is the start of the file >> somefile.txt
by typing
>> somefile.txt echo.
>> somefile.txt This is the start of the file.
In other words just change my order around? If so I had no idea you could do that.
Your loop for the agree section. Is this how you can set it so that it will only accept input of one or the other? Since they don't enter a Y or a N they will just stay in a continuous loop. And here I was thinking there would be a lot of code for that. That's pretty simple.
One more thing I have been struggling with is to capture errors. From what I have read you will use "2>&1". I am currently using two echo statements. One to capture to a text file and one to display on the screen. Is there a way to combine the two into one statement? With what I have is this the proper way to write the code?
I am learning a lot and appreciate your time.
Marcus
Here is an example of how I envision using it.
I was thinking of something more along these lines:
::========================
:: Computer-RX Installations
::
:: Automated Script
::========================
:main
@echo off
set spaces=[ ]
set spc16=%spaces:~1,16%
set spc26=%spaces:~1,26%
::------------------------
:: GET DRIVE LETTER
::------------------------
set varDriveLetter=
call :get_driveletter
if not %errorlevel%==0 (
call :display_titlescreen
echo.
echo %spc16%Cannot locate application
echo.
set /p .=%spc16%<nul
pause
cls
exit /b 1
)
::------------------------
:: GET INSTALLER
::------------------------
set varErrorCode=0
:loop1
call :display_titlescreen
set varInstaller=
call :get_installer %varErrorCode%
call :validate_installer "%varInstaller%"
set varErrorCode=%errorlevel%
if %varErrorCode% gtr 0 goto loop1
::------------------------
:: Substitue space with underline
::------------------------
set varInstaller=%varInstaller
::------------------------
:: GET GL CODE
::------------------------
set varErrorCode=0
:loop2
call :display_titlescreen
set varGL=
call :get_glcode %varErrorCode%
call :validate_glcode %varGL%
set varErrorCode=%errorlevel%
if %varErrorCode% gtr 0 goto loop2
call :initialise_variables
::------------------------
:: GET AGREEMENT
::------------------------
:loop3
call :display_titlescreen
call :display_agreement
echo.
set varAgree=
set /p varAgree=%spc26%Do you agree to this [Y/N]:
if not defined varAgree goto loop3
if /i "%varAgree%"=="y" (
goto agreed
) else (
if /i "%varAgree%"=="n" (
goto disagreed
) else (
goto loop3
)
)
:: Some more code may go here
:: Some more code may go here
::------------------------
:: END PROGRAM
::------------------------
call display_titlescreen
echo %spc16%Thank you.
echo.
pause
cls
exit /b
::========================
:: DISPLAY TITLE SCREEN
::========================
:display_titlescreen
cls
echo.
echo __________________________
echo.
echo Automated Script written for
echo Computer-RX Installations by Marcus Horner 2009
echo Server 2008 Image Preparation
echo __________________________
echo.
echo.
echo.
echo.
echo.
echo.
exit /b 0
::========================
:: GET DRIVE LETTER
::========================
:get_driveletter
set varPrograms=Installation_F
set varDriveLetter=
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist "%%a:\%varPrograms%\" set varDriveLetter=%%a:
)
if not defined varDriveLetter exit /b 0
exit /b 0
::========================
:: GET INSTALLER
::========================
:get_installer
if %1==0 (
echo.
) else (
if %1==1 (
echo %spc16%ERROR: You must enter your name...
) else (
if %1==2 (
echo %spc16%ERROR: Name contains Invalid characters...
) else (
if %1==3 (
echo %spc16%ERROR: Invalid name...
)
)
)
)
echo.
echo.
echo.
set /p varInstaller=%spc16%Please
exit /b 0
::========================
:: GET GL CODE
::========================
:get_glcode
if %1==0 (
echo.
) else (
if %1==4 (
echo %spc16%ERROR: You must enter a GL code...
) else (
if %1==5 (
echo %spc16%ERROR: Invalid GL code...
) else (
if %1==6 (
echo %spc16%ERROR: Not enough digits...
) else (
if %1==7 (
echo %spc16%ERROR: Too many digits...
)
)
)
)
)
echo.
echo.
echo.
echo %spc16%Please enter your name: %varInstaller%
echo.
echo.
set /p varGL=%spc16%Please enter your store's 4-digit GL code:
exit /b 0
::========================
:: DISPLAY AGREEMENT
::========================
:display_agreement
echo Welcome %varInstaller%. I hope you are having a nice day.
echo.
echo.
echo This program is an installation and functions as the installer's
echo.
echo digital checklist for verifying they have installed and configured
echo.
echo the various tasks presented in the Server 2008 Image Preparation.
echo.
echo.
echo By agreeing to these terms you are signing off that you will have
echo.
echo completed and verified the accuracy of all steps in this installation.
echo.
echo.
exit /b 0
::========================
:: AGREED
::========================
:agreed
call :display_titlescreen
echo %spc16%Unfinished section...
echo.
echo %spc16%[%varInstaller%]
echo.
echo %spc16%[%varGL%]
echo.
echo %spc16%[%varAgree%]
echo.
set /p .=%spc16%<nul
pause
exit /b
::========================
:: DISAGREED
::========================
:disagreed
rem set varNOTsigned="c:\Installat
rem set varNOTsignedTemp="c:\Insta
set varNOTsigned="%temp%\%dude
set varNOTsignedTemp="%temp%\%
call :display_titlescreen
echo Well %varInstaller% that is unfortuntate. Please explain why you refuse to
echo accept the installer's user agreement.
echo.
echo.
echo I will open a file for your to report your reasons.
echo.
echo.
echo. When finished simply save and close the file.
echo.
set /p .= <nul
pause
(:redirect
echo My reason for not accepting the installers user agreement is:
echo.
echo.
echo.
)>%varNOTsignedTemp%
notepad %varNOTsignedTemp%
(:redirect
echo USA Drug IT Department Digital Installation Form for Store: %varGL%
echo.
echo Local Date: %date%
echo Local Time: %time%
echo.
echo Installer: %varInstaller%
echo Computer: %computername%
echo.
echo %varInstaller% has chosen not to sign their digital signature for %computername% Phase 2 Server 2008 C-Rx Installation for the following reasons:
echo.
echo.
echo Reasons...
echo.
)>%varNOTsigned%
copy %varNOTsigned% + %varNOTsignedTemp% %varNOTsignedTemp% >nul
move %varNOTsignedTemp% %varNOTsigned%
(::redirect
echo.
echo.
echo Signed:
)>>%varNOTsigned%
pushd %cd%
cd "%varDriveLetter%\Installa
sendemail.exe -u "Phase 2 Installer Agreement Issue" -t "mhorner@usadrug.com" -f "%varInstaller% <installer@usadrug.com>" -o message-file="%varNOTsigne
popd
set /p .= <nul
pause
call :display_titlescreen
echo %spc16%This program will now close.
echo.
set /p .=%spc16%<nul
pause
cls
exit /b
::========================
:: VALIDATE INSTALLER
::========================
:validate_installer
setlocal enabledelayedexpansion
set varErrorCode=0
set varInstaller=%~1
::------------------------
:: If blank, exit with code 1
::------------------------
if not defined varInstaller exit /b 1
::------------------------
:: Save name to file & Get length of name -1
::------------------------
echo %varInstaller%>len
for %%a in (len) do set /a len=%%~za - 3
::------------------------
:: Check name is alpha and space
::------------------------
for /l %%l in (0,1,%len%) do (
set character=!varInstaller:~%
if /i not "!character!" equ " " (
if /i "!character!" lss "a" set varErrorCode=2
if /i "!character!" gtr "z" if /i "!character!" lss "A" set varErrorCode=2
if /i "!character!" gtr "Z" set varErrorCode=2
)
)
if %varErrorCode%==2 exit /b 2
::------------------------
:: Search for valid employee name - DISABLED
::------------------------
:: findstr "\<%varInstaller%\>" installers.txt >nul
:: if not %errorlevel%==0 exit /b 3
exit /b 0
::========================
:: VALIDATE GL CODE
::========================
:validate_glcode
set varErrorCode=0
set varGL=%~1
if not defined varGL exit /b 4
::------------------------
:: Check length is 4
::------------------------
echo %varGL%>len
for %%a in (len) do set /a len=%%~za-2
if %len% lss 4 exit /b 6
if %len% gtr 4 exit /b 7
::------------------------
:: Search for GL code - DISABLED
::------------------------
:: if not %errorlevel%==0 exit /b 5
exit /b 0
Thank you all. Holy smokes t0t0! I see you've done this a time or two. I will have to research everything you are doing in your example and see what I can understand. My initial questions are answered but now you've brought up new ones. I will look at your examples and see what I can pick-up on. I may post another series of questions for clarificaiton. I have learned a lot from this thank you.
Regards,
Marcus
Business Accounts
Answer for Membership
by: t0t0Posted on 2009-09-01 at 09:00:10ID: 25232929
The elements you need are:
TE:~3,2%%D ATE:~0,2%
ME:~3,2%
TE:~3,2%%D ATE:~0,2% ME:~3,2% SERNAME%_% InstallDat e%_%Instal lTime%.txt
1_1658.txt
%COMPUTERNAME%
%USERNAME%
%DATE%
%TIME%
The first two are environment variables, the last two are dynamic variables.
The first two are straight forward however, we only need specific parts of %DATE% and %TIME%.
SET InstallDate=%DATE:~6,4%%DA
So, if your date is normally displayed as dd/mm/yyyy then the above line will set InstallDate to yyyymmdd.
SET InstallTime=%TIME:~0,2%%TI
This returns: hhmm %InstallTime% by extracting just the first two digits and 'minute' digits of the time (where the time format is usually hh:mm:ss.cc).
Putting this together then, you get:
SET InstallDate=%DATE:~6,4%%DA
SET InstallTime=%TIME:~0,2%%TI
SET FileName=%COMPUTERNAME%_%U
and %FileName% contains the correctly formatted filename eg:
Computer01_Freddie_2009090
Hope that's helpful to you