"Frequently Asked Questions about CMD.EXE Command Line Scripts"

T

Timo Salmi

Dear Readers,

Before posting a CMD.EXE command line script related question please be
aware of the information and solutions material already available, such
as

91623 Mar 18 2004 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

In case of grave difficulties in getting this file from Garbo archives
see
Sites with permission to mirror Garbo archives
http://www.uwasa.fi/~ts/garbinfo/garbmirr.html

The contents of the FAQ-like script tips collection is given below.
For more command line script or near information sources please see

http://www.uwasa.fi/~ts/http/http2.html#cmdscript

This file TSCMDIDX.TXT lists the contents of

1CMDFAQ.TXT NT/2000/XP CMD.EXE Script Tricks
in ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Alternatively available with the version number as
ftp://garbo.uwasa.fi/pc/ts/tscmd19.zip

--------------------------------------------------------------------
1} How get today's date elements into environment variables?
2} On Windows XP how do you stop the script files from closing?
3} How can I customize the Command Prompt Window with script commands?
4} How can I test is it on MSDOS/Win3..9x/Me or NT/2000/XP?
5} How do I get a fully qualified path name of my script?
6} How does one get yesterday's date?
7} How can I test if two files agree in size and datetime stamp?
8} How do I find all the files made at of after YYYYMMDD HHMM?
9} How can I suppress script error messages altogether?
10} How can I change the environment variable values within a FOR loop?
11} How do I retain my original code page in these scripts?
12} How can I rename all my .JPG files sequentially in a folder?
13} How do I get the current day of the week?
14} Help! My old batch files won't work since there is no choice.
15} Is it possible to echo without linefeed like the Unix echo -n?
16} How can I get the time without having to press enter?
17} How can I avoid the potential "ECHO is off." in echoing %myvar%
18} How does one build re-usable script function modules?
19} How can one build a delay / sleep / wait procedure for a script?
20} Is is possible to right-justify numbers or words in a script?
21} Do you have pointers to additional script material and tips?
22} How do I capture the current folder name into a variable?
23} How do I get the n:th, the first and the last line of a text file?
24} How do I get the m:th item on the n:th line of a text file?
25} How can I quote with e.g. "> " an entire message?
26} How do I get the length of a string into a variable?
27} How can I remove all the blank lines from a text file?
28} How to convert a file written in IBM PC characters into LATIN1?
29} How do I drop the first two characters of a string, and so on?
30} Can one calculate the difference between two times in a script?
31} How many days ago was 31.12.2003? What date it was a 100 days ago?
32} How can I convert a decimal number to binary, octal, hexadecimal?
33} How can I convert a binary, octal, hexadecimal into a decimal?
34} How can I remove the quote characters from a line?
35} How can I substitute the second field on each line of my file?
36} I start a program from my script and it hogs my command window.
37} How can I insert a line in the middle of a file with a script?
38} How can I set and test the errorlevel within a script file?
39} Is there a subroutine to convert a variable into uppercase?
40} How do I get the number of arguments given to a script?
41} How can I create a zero-byte file? How do I test for it?
42} What is the subroutine for testing a filename for wildcards?
43} Is there a way of counting the number of files in a folder?
44} How do I get the first 68 columns from a text file?
45} How can I find out how many days old a file is?
46} Is a program available in the default folder or at path?
47} Is it possible to echo the redirection symbol in a script?
48} Why do some comment lines cause errors? What can I do about it?
49} I need to reverse a text file. How do I do that fairly quickly?
50} How do I get the position of a substring in a string?
51} How can I echo lines in different colors in NT scripts?
52} How can I enter a password into a variable without echoing it?
53} How can I test if a disk device is ready or not?
54} How can I get the type of a disk device?
55} How to get the creation, last modified and last access of a file?
56} How to find and move more recent files from one folder to another?
57} How do I get a list of all my empty folders on c:\ ?
58} How do I best combine two quoted arguments into one quoted string?
59} How do I find if a folder exists? How about visible files in it?
60} How do I delete all but most recent 20 files in a folder?
61} How can one devise a command line calculator?
62} How can I compare within a script if two files are identical?
63} How to perform a command on each line of file as an argument?
64} How to count the number of lines in a file, empty lines inclusive?
65} How do I add text in front and after each line in a text file?
 
S

see

Dear Readers,

Before posting a CMD.EXE command line script related question please be
aware of the information and solutions material already available, such
as

91623 Mar 18 2004 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
[snip]

After several unsuccessful attempts to contact Mr. Salmi via email,
I've decided to post my bug report here.

Subject: "How can one build a delay" bug fix (from tscmd.zip)

Hello Timo,
From (e-mail address removed) Mon Feb 02 01:00:19 2004
Subject: 19) How can one build a delay / sleep / wait procedure for a script?
Date: Mon, 02 Feb 2004 01:00:19
From: (e-mail address removed) (Timo Salmi)

@echo off
setlocal enableextensions
echo %time%
call :procDelay 250
echo %time%
goto :EOF

:procDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=1%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=1%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS %1 goto _procwaitloop
endlocal & goto :EOF
[snip]

The above code needs to be altered as shown. The addition of the 1s are
necessary due to an error which occurs from 00:00:00 to 09:59:59, since
the leading hour digit '0' is interpreted as an octal number prefix.
See 'set /?' for details.

Also note that the delay value isn't really in milliseconds.
This can be seen as follows:

start_/now_
500 ms = 00:00:00.50 -> 100000050 OK
1 second = 00:00:01.00 -> 100000100 OK
59 seconds = 00:00:59.00 -> 100005900 OK
60 seconds = 00:01:00.00 -> 100010000 **this would be 100006000**
**if it was milliseconds.**

The delay value might be better described as concatenated
Hours-Minutes-Seconds-Milliseconds (e.g. sans the :s and .).

Any thoughts?

Thanks
 
G

guard

Subject: "How can one build a delay" bug fix (from tscmd.zip)

[snip]

The above code needs to be altered as shown. The addition of the 1s are
necessary due to an error which occurs from 00:00:00 to 09:59:59, since
the leading hour digit '0' is interpreted as an octal number prefix.
See 'set /?' for details.

Also note that the delay value isn't really in milliseconds.
This can be seen as follows:

start_/now_
500 ms = 00:00:00.50 -> 100000050 OK
1 second = 00:00:01.00 -> 100000100 OK
59 seconds = 00:00:59.00 -> 100005900 OK
60 seconds = 00:01:00.00 -> 100010000 **this would be 100006000**
**if it was milliseconds.**

The delay value might be better described as concatenated
Hours-Minutes-Seconds-Milliseconds (e.g. sans the :s and .).

Any thoughts?

The ".Wait" command is FREE and works with
all regional date and time settings.

%.Wait% [SecondsToWait]

See (http://TheSystemGuard.com/MtCmds/CrystalClear/Wait.htm)

*******

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top