Page 1 of 1

Reading text from a file into a DOS command.

Posted: 29 Sep 2010 09:50
by rkhad89
Hi all,

I have not done much DOS in the past so apologies if this is a simple question, however I have not seen anything like it across other forums.

Currently, I am generating daily zip files with a random password and pushing them into a directory for others to download. Each of these zip files generate a new random password every day which is output into a log file that users have access to.

I have been tasked to change the way this works and generate a new random password only every month now. I am unsure on how I do this.

My main idea thus far is;

perform an if function which says;
>> if the initial part of the date(token %a) is "01" //the first day of the month
- then generate a new random password and output it to logfile.txt
- upload the zip file for the day to relevant directory.

else
>> put file "...zip" (prompting password) -p %password into relevant directory
\\ c:\directory\7za\ a "c:\%archivedate%.zip" -p%password% "c:\relevantdirectory\.."
\\ where %password% would be the password read from the logfile.

**directories arent correct. archivedate is defined previously in the script and varies daily.

Does anyone have any ideas?

Regards,

rk

Re: Reading text from a file into a DOS command.

Posted: 29 Sep 2010 14:06
by aGerman
The format of the date and the way how to split it depends on your international settings (registry values). You should tell us what
echo %date%

would display on the command prompt.

Regards
aGerman

Re: Reading text from a file into a DOS command.

Posted: 29 Sep 2010 18:54
by amel27
rkhad89 wrote:then generate a new random password and output it to logfile.txt
...and what format of logfile.txt used?.. You store only last password, add password to end of file or save date & password?

Re: Reading text from a file into a DOS command.

Posted: 30 Sep 2010 03:51
by rkhad89
aGerman,

It is in the format dd/mm/yyyy. This is UK settings. Is this information required so that you can work out how to parse the input?

Regards.

-----

amel27,

It would just be a basic text file which stores passwords continuously. i.e. it will have the passwords and date echoed into it for the relevant months sequentially.

01/08/2010 password is *password*
01/09/2010 password is *password*
01/10/2010 password is *password*

Above is how the echo I am using works. You would therefore be requried to read from the last line of the text file. I believe this can be done using something like " ... = /l " but in what context and if this can be done within the else part of an existing DOS script.

Cheers for your help.

Re: Reading text from a file into a DOS command.

Posted: 30 Sep 2010 07:56
by alan_b
Do you guarantee that the script will ALWAYS be run every day without fail ?
What happens if the first day of the month is a weekend or bank holiday ?

It may be simpler to detect when the month has CHANGED, not when the DAY is unity.

That would also automatically prevent the creation of a new password 3600 times per hour on day one !

Alan

Re: Reading text from a file into a DOS command.

Posted: 30 Sep 2010 08:29
by rkhad89
alan_b wrote:Do you guarantee that the script will ALWAYS be run every day without fail ?
What happens if the first day of the month is a weekend or bank holiday ?


At the end of every day a zip files is pushed to a user download area to be downloaded at the users discretion. Currently this is with different passwords each day. This will happen every day regardless of public holidays.

alan_b wrote: It may be simpler to detect when the month has CHANGED, not when the DAY is unity.


How do you suggest this be done? Would you approach it in a similar manner as I have suggested at the beginning. i.e. tokenize the date field and look for a certain value? or change in value? I do not understand how I can implement this in this manner.

alan_b wrote: That would also automatically prevent the creation of a new password 3600 times per hour on day one !


How do you mean 3600 passwords? The script will be ran at the end of each day, and will be ran only once. I aplogise if that is not clear in my initial question.

Thanks for your input though mate. How do you suggest we detect the change in month?

Regards,

Re: Reading text from a file into a DOS command.

Posted: 30 Sep 2010 10:42
by alan_b
I did not know if this was running continuously to cover weekends,
in which case launching a new password and guessing it takes 1 second to create and send would be 3600 passwords per hour !

If this is launched automatically and once per day then using a day of unity is fine
This will split the date into day, month, and year, as shown by ECHO.
All you need now is something which I call Create_and_Issue_Password_etc
Enjoy

Code: Select all

FOR /F "tokens=1-4 delims=/" %%d in ("%DATE%") do (
  ECHO %%d
  ECHO %%e
  ECHO %%f
  IF %%d==01 ( Create_and_Issue_Password_etc. )
)

NB I am urgently working on another project but I am sure others can advise upon password code
Alan

Re: Reading text from a file into a DOS command.

Posted: 30 Sep 2010 12:06
by aGerman
rkhad89 wrote:aGerman,

It is in the format dd/mm/yyyy. This is UK settings. Is this information required so that you can work out how to parse the input?

Regards.

There is no native function or variable to get the day or month or year directly. You have to split the content of %date%. Thats why you need the order and the separator.
Of course, you could also read this information in the registry:

Code: Select all

@echo off &setlocal
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v iDate') do set "iDate=%%a"
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sDate') do set "sDate=%%a"
for /f "tokens=1-4 delims=%sDate% " %%a in ("%date%") do (
  if "%%d"=="" (
    if %iDate%==0 (set /a M=1%%a-100 , D=1%%b-100 , Y=%%c)
    if %iDate%==1 (set /a D=1%%a-100 , M=1%%b-100 , Y=%%c)
    if %iDate%==2 (set /a Y=%%a , M=1%%b-100 , D=1%%c-100)
  ) else (
    if %iDate%==0 (set /a M=1%%b-100 , D=1%%c-100 , Y=%%d)
    if %iDate%==1 (set /a D=1%%b-100 , M=1%%c-100 , Y=%%d)
    if %iDate%==2 (set /a Y=%%b , M=1%%c-100 , D=1%%d-100)
  )
)

echo Year  %Y%
echo Month %M%
echo Day   %D%
pause

... but it's easier to know the format to get it using a simple FOR loop.


BTW I agree with alan_b. It's not safe to check if it is the first day of a month. A better way would be to check if a password was created for the current month or not.

Try something like that:

Code: Select all

@echo off &setlocal enabledelayedexpansion

REM get the current month and year
for /f "tokens=2,3 delims=/" %%a in ("%date%") do set "CurrentMonthYear=%%a%%b"

REM get the last month and year saved in password.txt
for /f "usebackq tokens=2,3 delims=/ " %%a in ("password.txt") do set "OldMonthYear=%%a%%b"

REM check if both variables are different
if "%CurrentMonthYear%" neq "%OldMonthYear%" (
  REM if different, call :CreatePass. Arguments are number of characters the password should have
  REM  and the variable name for the password
  call :CreatePass 10 pwd
  REM append the new password to password.txt
  >>"password.txt" echo %date% password is !pwd!
)

REM get the last password saved in password.txt
for /f "usebackq tokens=4" %%a in ("password.txt") do set "password=%%a"

echo current password: !password!
echo Here you should place your further stuff.
pause
goto :eof


:CreatePass NumberOfCharacters VariableName
setlocal enabledelayedexpansion
set "pass="
for /l %%i in (1,1,%~1) do (
  set /a x=!random! %% 94 + 33
  cmd /c exit !x!
  set "pass=!pass!!=exitcodeascii!"
)
endlocal &set "%~2=%pass:!=^!%"
goto :eof

Regards
aGerman

[EDIT: Changed the CreatePass function. set /a x=!random! %% 93 + 34 to set /a x=!random! %% 94 + 33 /]

Re: Reading text from a file into a DOS command.

Posted: 06 Oct 2010 03:30
by rkhad89
Cheers mate, thats been a big help.