Computing.Net > Forums > Programming > Simple Batch File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Simple Batch File

Reply to Message Icon

Name: x2aberry
Date: June 3, 2009 at 15:28:26 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi Guys,

I am trying to write a batch file that will:
1. Read a text document(Ex:loginlog.txt)
2.find the comma place and save all the text before the comma into a variable
%computerName% and then save the name into a file.

here are some lines from the file im trying to pull the computer names from:
((((((((((((((((((((((((((((((
W2BMHLABTEMP,11/6/2007 8:51:55
CDCOX1,11/8/2007 3:47:46
BIRMETRO1,11/9/2007 8:14:58
)))))))))))))))))))))))))))))))

Any suggestions?

Thanks in advance

Adam



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: June 3, 2009 at 19:18:27 Pacific
Reply:

if you have Python on Windows

for line in open("logfile.txt"):
    if "," in line:
        computername=line.strip().split(",")[0]
        print computername


0

Response Number 2
Name: x2aberry
Date: June 4, 2009 at 05:45:37 Pacific
Reply:

unfortunatly, I do not have python.
This batch file will be ran on close to 10,000 PCs so im trying to keep from having to install anything extra. =T

Thanks for the suggestion though!


0

Response Number 3
Name: Mechanix2Go
Date: June 6, 2009 at 02:08:16 Pacific
Reply:

@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=," %%a in (thefile) do (
echo %%a>> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: x2aberry
Date: June 10, 2009 at 10:05:58 Pacific
Reply:

Thank you so much Mech!!
The code worked beautifully!!!

By chance would you know how to reverse the order of the computers in the list?

for instance:

CDCOX1
BIRMETRO1
BIRMETRO1
INBOUND2
-------------------

This is the list I am getting.. I would like to list the bottem one first(Inbound2) because it is the last computer that was logged into. Woul it be possible to reverse the order?


0

Response Number 5
Name: ghostdog
Date: June 10, 2009 at 18:06:02 Pacific
Reply:

2 ways you can do reverse, one , do it with your original file, which i believe you have no control over it. second use arrays and some routine to reverse order.

I know you can't install but here's the solution in Python anyway, just to show the concept.

import sys
logfile=sys.argv[1]
computers=[]
for line in open(logfile):
    if "," in line:
        computername=line.strip().split(",")[0]
        computers.append(computername)
for items in reversed(computers):
    print items

output
C:\test>more file.txt
W2BMHLABTEMP,11/6/2007 8:51:55
CDCOX1,11/8/2007 3:47:46
BIRMETRO1,11/9/2007 8:14:58

C:\test>python test.py file.txt
BIRMETRO1
CDCOX1
W2BMHLABTEMP


>>>>> This batch file will be ran on close to 10,000 PCs so im trying to keep from having to install anything extra. =T

here's the executable if you are interested (tested on XP,W2k) which you can bring to your PCs using your deployment tool.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: June 10, 2009 at 19:23:29 Pacific
Reply:

@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=," %%a in (thefile) do (
echo %%a>> newfile
)
===============================

I guess the output would be better called: reversed; but you get the idea.

set N=0

for /f "tokens=1 delims=" %%a in (newfile) do (
set /a N+=1
set v!N!=%%a
)

@echo off > sorted.txt

for /L %%i in (!N! -1 1) do (
echo !v%%i!>> sorted.txt
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: ghostdog
Date: June 10, 2009 at 19:45:59 Pacific
Reply:

@m2, can it be done without creating intermediate file (ie newfile) ? it will be more efficient that way.


0

Response Number 8
Name: Mechanix2Go
Date: June 10, 2009 at 19:58:05 Pacific
Reply:

Hi ghostdog,

Good question.

============================
@echo off > newfile & setLocal EnableDelayedExpansion

set N=0

for /f "tokens=1 delims=," %%a in (thefile) do (
set /a N+=1
set v!N!=%%a
)

for /L %%i in (!N! -1 1) do (
echo !v%%i!>> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: x2aberry
Date: June 11, 2009 at 06:51:28 Pacific
Reply:

Mech thanks, once again the code worked great!
I'm now moving onto the next step of the batch and instead of making many post with diffrent things/questions, I am going to just write in sudo code what I am trying to do and if you guys have any suggestions or code that might help out please let me know =).

-------------------------------
The purpose of this batch file is to decompress a users profile on the server that is experiancing encryption/compression issues.
--------------------------------
1. prompt user for their user id
2.look at the log files on the server and find out the last computer or computers(if the last one they logged into is not online check the next).
3.From the last PC they logged in, remotly get an enviroment variable and/or registry key that list their profile servername (commanline example: echo %homeserver% = alxaptr2)
(Registry example: [HKEY_CURRENT_USER\Environment]
"HOMESERVER"="AGERPFS15")
4. Once their homeserver has been found, run the following command to decompress their profile:
compact /a /u %homeserver%/%username%.USR/*.*
---------------------------------------------------------------
___________________________________________
Somewhat sudo code
(exactly whats written above except Simplified)

1.Prompt for userID
2.get logfile from network (example path:\\logs\%user%.txt)
3.find last PC logged into
4 ping that PC to see if it is online
5. if so then goto 6 if not then goto next pc logged into and ping it, if it is online goto 6 if not goto next pc logged in(repeat until we find one that is online)
6.get homeserver name from last PC via registry or wmic
7.decompress profile on homeserver


---------------------------------------------------------------------
ok so mabey this batch file isnt so simple =T

Thanks for any and all help though guys (Mech!)


0

Response Number 10
Name: x2aberry
Date: June 11, 2009 at 06:53:21 Pacific
Reply:

I meant to post what I Had so far:

______________________________________
@echo off > c:\tempfile & setLocal EnableDelayedExpansion
cls
echo --------------------------------------------------------------
echo BLT Profile Decompression Tool v3.1
echo --------------------------------------------------------------
SET /P Choice=Type in the user ID that is having profile problems:
for /f "tokens=1 delims=," %%a in (\\logs.blah.com\logs$\UserLogs\Logins\%Choice%.txt) do (
echo Retreaving Computer Name: %%a
echo %%a>> %Choice%.txt
)


set N=0
for /f "tokens=1 delims=" %%a in (%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
)
@echo off > Loginlog.txt
for /L %%i in (!N! -1 1) do (
echo !v%%i!>> Loginlog.txt
echo Sorting Computer Name: %%i
)


____________________________________


0

Response Number 11
Name: x2aberry
Date: June 11, 2009 at 08:00:51 Pacific
Reply:

The code I just posted is what I have so far and takes care of steps 1-3 in my sudo code example, above the previous post.


0

Response Number 12
Name: Mechanix2Go
Date: June 11, 2009 at 10:24:59 Pacific
Reply:

Looks like you've got it right so far. Now code the ping.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 13
Name: x2aberry
Date: June 11, 2009 at 12:42:23 Pacific
Reply:

ok so I do like a
C:\>Ping %computerName% > pingstats.txt

and somehow check the 4th line to see if it has the word
Reply in it. If so, GOTO BlahBlah if not, goto next computer name. Or how do you think I should check in the code if it was successful or not?

------------------------------------------------------------
Failed Attempt
-------------------------------------------------------------

Pinging birmetro1.southernco.com [172.25.102.39] with 32 bytes of data:

Request timed out.

Request timed out.

Request timed out.

Request timed out.

Ping statistics for 173.25.102.39:

Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),


--------------------------------------------------------------------------
Sucessful Attempt
--------------------------------------------------------------------------
Pinging birmetro2.southernco.com [172.25.88.185] with 32 bytes of data:

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Ping statistics for 172.25.88.185:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 0ms, Maximum = 0ms, Average = 0ms


0

Response Number 14
Name: Mechanix2Go
Date: June 11, 2009 at 12:56:25 Pacific
Reply:

Usually best to avoid creating files.

ping localhost | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 15
Name: x2aberry
Date: June 11, 2009 at 13:42:31 Pacific
Reply:

Good call..
would it be possible to do this...

if NOT errorlevel 1 goto :PASSED
instead of :if errorlevel 1 goto :failed

I can't seemd to get the failed working correctly:


-------------
What Im workin with--------
------------------------------------------------------------------------
@echo off > c:\loginlog.txt
for /L %%i in (!N! -1 1) do (

echo !v%%i!>> c:\loginlog.txt
echo Pinging: !v%%i!
ping -w 1 !v%%i! | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK
:failed
echo Can't Connect to !v%%i!
pause
)

pause
exit /b
:OK
echo Can Connect to:!v%%i!
pause
--------------------------------------------------------


0

Response Number 16
Name: Mechanix2Go
Date: June 11, 2009 at 15:22:57 Pacific
Reply:

I see nothing wrong with your code.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 17
Name: x2aberry
Date: June 12, 2009 at 06:24:09 Pacific
Reply:

well I changed the code a little to this: and it seems that when it fails the ping..it goes out of the loop and dosent try the next computername.

--------------------------------
code
----------------------------------
set N=0
for /f "tokens=1 delims=" %%a in (c:\%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
)
@echo off > c:\loginlog.txt
for /L %%i in (!N! -1 1) do (

echo !v%%i!>> c:\loginlog.txt

echo Pinging: !v%%i!
ping !v%%i! | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK
:failed
echo Nooooooooooooooooooo cant get !v%%i!
pause
)

exit
pause

:OK
echo woooooooooooooo connected !v%%i!
pause
--------------------------------
output
-------------------------------
Retreaving Computer Name: DLMACON1
Sorting.....
Please Wait
Pinging: DLMACON1
Nooooooooooooooooooo cant get
Press any key to continue . . .

(At this point, I press enter and the command window closes without checking the next computer
------------------------------------------------------------

any thoughts? =T)


0

Response Number 18
Name: Mechanix2Go
Date: June 12, 2009 at 07:04:26 Pacific
Reply:

I think your goto :failed is busting out of the for loop.

Try changing it to:

call :failed

and put :failed outside the loop.

[not tested]


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 19
Name: x2aberry
Date: June 12, 2009 at 07:18:45 Pacific
Reply:

where would I need to put failed outside of the loop to where it would continue the the loop until it found one that was online?

would it be possible to use

if NOT errorlevel 1 CALL/GOTO :success


0

Response Number 20
Name: x2aberry
Date: June 12, 2009 at 07:32:31 Pacific
Reply:

I tested..and IF NOT DOES WORK!!!!!! w0oohoo!!!
next problem though:

its reading from the list like this:
_________________
birmetro1
birmetro1
birmetro1
cdcox2
cdcox2
jghou7
____________________

so it pings birmetro1 3 times before it moves to cdcox2

What kind of if statment could I use to check and see if the new computer name matches the previously used computer name.

example

If %computername% == %oldcomputername% goto :skip
else
(continue with pinging)
---------------------------------------------------------------
This is what I have so far that seems to be working
----------------------------------------------------------------

set N=0
for /f "tokens=1 delims=" %%a in (c:\%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
echo %%a


)
@echo off > c:\loginlog.txt
for /L %%i in (!N! -1 1) do (

echo !v%%i!>> c:\loginlog.txt


echo Pinging: !v%%i! .....
ping -w 10 !v%%i! | find "Reply" > nul
if not errorlevel 1 GOTO :OK
echo Nooooooooooooooooooo cant get !v%%i!
pause

)
echo loop end
pause
exit


:OK
echo woooooooooooooo connected !v%%i!
pause


0

Response Number 21
Name: Mechanix2Go
Date: June 12, 2009 at 08:57:58 Pacific
Reply:

set N=0
for /f "tokens=1 delims=" %%a in (c:\%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
echo %%a
)

@echo off > c:\loginlog.txt

for /L %%i in (!N! -1 1) do (
ping -w 10 !v%%i! | find "Reply" > nul
if not errorlevel 1 GOTO :OK
if errorlevel 1 call :failed
)

goto :eof

:OK
echo connected !v%%i!
goto :eof

:failed
echo failed
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Simple Batch File

Simple Batch file www.computing.net/answers/programming/simple-batch-file/10948.html

Need help to make a simple batch file www.computing.net/answers/programming/need-help-to-make-a-simple-batch-file/20286.html

simple batch file prog q.. www.computing.net/answers/programming/simple-batch-file-prog-q/14278.html