Reading text from a file into a DOS command.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rkhad89
Posts: 4
Joined: 29 Sep 2010 09:36

Reading text from a file into a DOS command.

#1 Post by rkhad89 » 29 Sep 2010 09:50

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 29 Sep 2010 14:06

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

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

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

#3 Post by amel27 » 29 Sep 2010 18:54

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?

rkhad89
Posts: 4
Joined: 29 Sep 2010 09:36

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

#4 Post by rkhad89 » 30 Sep 2010 03:51

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.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

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

#5 Post by alan_b » 30 Sep 2010 07:56

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

rkhad89
Posts: 4
Joined: 29 Sep 2010 09:36

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

#6 Post by rkhad89 » 30 Sep 2010 08:29

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,

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

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

#7 Post by alan_b » 30 Sep 2010 10:42

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#8 Post by aGerman » 30 Sep 2010 12:06

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 /]

rkhad89
Posts: 4
Joined: 29 Sep 2010 09:36

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

#9 Post by rkhad89 » 06 Oct 2010 03:30

Cheers mate, thats been a big help.

Post Reply