Little Rusty on Batch File Commands - Need some help

Status
Not open for further replies.

EvilAlchemist

2[H]4U
Joined
Jan 11, 2008
Messages
2,730
I found myself needing to create a batch file, but realized how rusty I am at it ..
So any help I can get would be great ......

Here is what I need it to do : If I *think* i know the code, I will put it up for you to look over

Rename 192.168.1.200\server.txt --> Server1%datehere%.txt
Code:
@echo off
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "\\192.168.1.200\status.txt" \\192.168.1.200\Server1-%%e-%%f-%%g.txt


Move 192.168.1.200\Server1-12-30-2008.txt --> 192.168.1.100\Server1-12-30-2008.txt
Code:
move \\192.168.1.200\Server1-12-30-2008.txt \\192.168.1.100\Server1-12-30-2008.txt

Search 192.168.1.100\Server1-12-30-2008.txt for the line "System_Error"
If yes, copy 192.168.1.100\Server1-12-30-2008.txt --> 192.168.1.100\ErrorLogs\Server1-12-30-2008.txt
If no, do nothing.
Code:
@echo off
findstr /L "SYSTEM_ERROR" Server1-12-30-2008.txt > NUL
if %errorlevel%==0 (
copy \192.168.1.100\Server1-12-30-2008.txt  \192.168.1.100\Error\Server1-12-30-2008.txt
)else (
echo No Error on Remote System
)

Create new/empty server.txt file in orignial location of 192.168.1.200\server.txt
Code:
type NUL >\\192.168.1.200\server.txt

I think I have most of it correct, but I have no clue on how to do the *search* part.

*Update* I think I got the search part figured out ... for the most part..

I will have to change the Server1-12-30-2008 examples to *.txt to make it work over multiple systems. Let me know if you see something wrong with this code.


Thanks
 
Does anyknow know how to copy , lets say, the last 50 line of a text file to a new txt file?

I found a bat file that lets you specify the number of lines to keep.

Usage: Count 4 C:\MyDir\Myfile.txt lastlines.txt
Code:
Example:
@Echo Off

:: COUNT.BAT Syntax: Count #_of_lines FileName Output_File

Echo.
If %2.==. Echo Missing source file & GoTo :EOF
If not exist %~f2 Echo File %~f2 not found & Goto :EOF
If %3.==. Echo Missing output destination & GoTo :EOF
Echo Extracting last %1 lines from %~f2 into %~f3

If exist %~f3 Del %~f3

For /F "tokens=3 delims=:" %%A in ('Find /V /C "#~#" %~f2') Do Set RN=%%A
Set /A SN=%RN%-%1
If %SN% leq 0 Echo Too many lines [File contains %RN% lines] & GoTo :EOF
Echo Skipping %SN% lines from %RN%
For /F "tokens=* skip=%SN% delims=" %%A in (%~f2) Do Echo %%A>> %~f3
Set RN=
Set SN=

How would I modify this code to allow a set number (50) and specify a file name rather then calling another bat file?

**Added **

I also found this code .. might be easier to use .. but I am not sure where to modfiy it to put my static variables for # of lines & which txt files to read from ????
Code:
@echo off & setlocal
for /F "delims=" %%j in ('type "%*"') do set line=%%j
echo.%line%> "%*"
 
Code:
@Echo Off

:: COUNT.BAT Syntax: Count #_of_lines FileName Output_File

Echo.
If %2.==. Echo Missing source file & GoTo :EOF
If not exist %~f2 Echo File %~f2 not found & Goto :EOF
If %3.==. Echo Missing output destination & GoTo :EOF
Echo Extracting last %1 lines from %~f2 into %~f3

If exist %~f3 Del %~f3

For /F "tokens=3 delims=:" %%A in ('Find /V /C "#~#" %~f2') Do Set RN=%%A
Set /A SN=%RN%-%1
If %SN% leq 0 Echo Too many lines [File contains %RN% lines] & GoTo :EOF
Echo Skipping %SN% lines from %RN%
For /F "tokens=* skip=%SN% delims=" %%A in (%~f2) Do Echo %%A>> %~f3
Set RN=
Set SN=
How would I modify this code to allow a set number (50) and specify a file name rather then calling another bat file?

For the first part (number) you set a constant to substitute the first parameter (%1) and rearrange parameter naming like this (code tested and working, you can c&p):

Code:
Echo Off

:: COUNT.BAT Syntax: Count #_of_lines FileName Output_File

set LinesToExtract=50

Echo.
If %1.==. Echo Missing source file & GoTo :EOF
If not exist %~f1 Echo File %~f1 not found & Goto :EOF
If %2.==. Echo Missing output destination & GoTo :EOF
Echo Extracting last %LinesToExtract% lines from %~f1 into %~f2

If exist %~f2 Del %~f2

For /F "tokens=3 delims=:" %%A in ('Find /V /C "#~#" %~f1') Do Set RN=%%A
Set /A SN=%RN%-%LinesToExtract%
If %SN% leq 0 Echo Too many lines [File contains %RN% lines] & GoTo :EOF
Echo Skipping %SN% lines from %RN%
For /F "tokens=* skip=%SN% delims=" %%A in (%~f1) Do Echo %%A>> %~f2
Set RN=
Set SN=
set LinesToExtract=
Of course, now the syntax is:

Code:
count C:\MyDir\Myfile.txt last50lines.txt
For the second part (file) in which way you need to specify a filename, you want to make it fixed like the number of lines? If yes which one of the files you want to hardcode, in, out or both? The rest of the code I have seen should do exactly what expected (only partially tested) a part for a little problem here, where the remote filenames should be probably prepended by double backslash instead of single:

Code:
@echo off
findstr /L "SYSTEM_ERROR" Server1-12-30-2008.txt > NUL
if %errorlevel%==0 (
copy \192.168.1.100\Server1-12-30-2008.txt  \192.168.1.100\Error\Server1-12-30-2008.txt
)else (
echo No Error on Remote System
)
So keep on posting because you are very close to a solution, "little rusty" :D

TJ
 
Status
Not open for further replies.
Back
Top