Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I would like to take
the following data from a text file called c:\userexp.txt:
===========================================
username1 - Expiration date: Sat Aug 15 00:00:00 PDT 2009
username2 - Expiration date: Tue Jan 01 00:00:00 PST 2030
username3 - Expiration date: Wed Aug 17 00:00:00 PDT 2011
===========================================
and parse out needed information to a batch file called c:\userexpimport.bat
that needs to look like the lines below:
============================
net user username1 /expires:aug,15,2009
net user username2 /expires:jan,01,2030
net user username3 /expires:aug.17,2011
============================
Bishopz

You didn't say which language you wanted to use to do the parsing. Here's a Perl script. If you want to use input/output redirection, I can reduce it by 4 lines and just have the while loop. Or, it can even be redueced to a single line executed from the command line instead of the script.
#!perl -w
open (SRC, "c:/userexp.txt") || die $!;
open (BAT, ">c:/userexpimport.bat") || die $!;while(<SRC>) {
$_ = lc($_);
/^(\S+) [^:]+: \w+ (\w+) (\d\d).*(\d{4})$/;
print BAT "net user $1 /expires:$2,$3,$4\n";
}
close SRC;
close BAT;

FM strikes again :*)
=====================================
If at first you don't succeed, you're about average.M2Go

Posts like these make me miss my perl days...It's amazing what a pro like FM can get done in one line of code.
-SN

Thanks for the compliment.
Perl is a great language and I enjoy writing the scripts, but you guys definitely have me beat in other areas, such as PHP and batch scripts. I've seen some impressive batch scripting from M2Go and IVO.

here's a python script.
import os
root = "c:\\"
inputfile = os.path.join(root,"userexp.txt")
outputfile = os.path.join(root,"userexpimport.bat")
a = open(inputfile)
b = open(outputfile,"a")
while 1:
line = a.readline().strip()
if line == '':
break
user = line.split(':',1)[0].split()[0]
month,day = line.split(':',1)[1].strip().split( )[1:3]
year = line.split(':',1)[1].strip().split( )[-1]
b.write("net user %s /expires: %s,%s,%s\n" %(user,month,day,year))
a.close()
b.close()

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |