Wonderful batch processing code

Source: Internet
Author: User
Tags echo date
Wonderful Batch ProcessingCode

Receiving keyboard input stringProgram

: Get user input-related des error check
@ Echo off
Echo E102 'set % 1 = '> % Temp %. \ t1.dat
For %? In (w Q) Do echo %?> % Temp %. \ t1.dat
: Input
Echo.
Echo type input:
FC con NUL/lB1/n | find "1:"> % Temp %. \ t2.bat
: Error Checking begins here
Find "|" % Temp %. \ t2.bat> NUL
If not errorlevel = 1 goto Error
Find "=" % Temp %. \ t2.bat> NUL
If not errorlevel = 1 goto Error
Find "NUL
If not errorlevel = 1 goto Error
Find ">" % Temp %. \ t2.bat> NUL
If not errorlevel = 1 goto Error
: Error Checking end
Debug % Temp %. \ t2.bat NUL
Call % Temp %. \ t2.bat string
For %? In (t1.dat t2.bat) Do del % Temp %. \ %?
Echo string = % string %
Goto EOF
: Error
Echo.
Echo @ prompt invalid input. invalid characters: $ q $ L $ G $ B $ _> % Temp %. \ t2.bat
% Comspec %/C % Temp %. \ t2.bat | find ":"
Goto Input
: EOF

 

A program that modifies the update date of a file

@ Echo off
Rem save system time
Echo. | date> sysdate. bat
Echo. | time> same ime. bat
Rem modifies the system time to a specified time. The date is parameter 1, the time is parameter 2, and the file name is parameter 3.
Date % 1
Time % 2
Rem file modification time
Copy % 3 +,> NUL
Rem recovery system time
Echo date % 4> current. bat
Call sysdate
Echo Time % 3> current. bat
Call your ime
Completed REM Program Execution
Echo success.
Del systime. bat
Del sysdate. bat
Del current. bat
Echo on

Ping all IP addresses in a certain segment

Enter. bat 0 1 2 3 4 5 6 7 8 9, you can display all IP addresses in a certain segment, 192.168.1.0-192.168.1.99, and change the echo to ping, the choice and if statements are waiting for the user to determine whether to continue and can be deleted, but it is troublesome to interrupt the program.
Code: [copy to clipboard]
--------------------------------------------------------------------------------

@ Echo off
: Redo
If "% 1" = "" Goto end
For % A in (1%,) Do echo 192.168.1.%
Choice continue
If errorlevel 2 goto end
Shift
Goto redo
: End

A timer Program

The current. bat in the program can be replaced with the content you need to execute. You can set the time for the "find" clause based on your own needs, or use the parameter % 1 instead. You can also change the time to date to implement the scheduled execution function.

------------------------------

Quote:
: Current. bat
@ Echo off
Echo Haha, it's the time to go !!

: Timer. bat
@ Echo off
: Start
Echo. | time | find "10:30:00"> Run. bat
Choice/C: Y/T: Y, 59/n> NUL
Call run. bat
Goto start
 

ASCII assembly code, latency precision is 0.001 seconds, applicable platform for MS-DOS/Win9x/winnt.

German Herbert kleebauerprovided a general solution to achieve latency through asciiassembler indirectly using a sleep.exe program. This program is divided into dos and win modules, respectively calling DOS system interruption services and windowsapi.
Code: [copy to clipboard]
--------------------------------------------------------------------------------

: Sleep. Bat-sleep/delay/Wait n seconds
: Herbert kleebauer (Germany)-2005/05/29
: Modified by will sort-2005/06/02, 07-25
@ Echo off
Echo q | debug> NUL
Echo Bj @ jzh '0x-'/ppppppa (De (DM (do (DH (LS (LU (LX (lezrr] eeeuyrx2dx => sleep.com
Echo 0 dxfp, 0xx. t0p, = xtgsb4o $? Piyu wwx0gwuy WV; ovbx2gv0exgiuht6> sleep.com
Echo T} {z ~~ @ Gwkbg @ oekcut '~} @ Mqqbsy? Sehb ~ _ Phxr? @ Zab 'lrpeyodt @ CJ?> Sleep.com
Echo Pky_jn @ qekpet @ Ij? Jysjn @ rekpet @ JJ? Jygjn @ sekkjtlgunw? P @ pjirz> sleep.com
Echo Lfvaurq? Oyltq @@?~ Qcool ~ Rdu @? Au? @ {Qoq? @} Ikunwpe ~ Fpestmwh? Vkk> sleep.com
Echo _ Gsqocvh {ojeoseiqrma @ knefb? P ?? Mcjnne ~ B? M ?? Qhetlbgbphexh @ E => sleep.com
EchoEsogwtlblk? Sfu '? Ldod @ k @ XO? Suuda? _ FKJ @ n? KD @? UA ?? O} hcqoq ?? R> sleep.com
Echo _ Oqos? ? CEU? _ Fu? UAQ? Ubd? Loc? Oro? UOL? Uod? Ooi? Ugl? Lor @ yuo?> Sleep.com
Echo dsmsqswdor [bqaq? Lua? _ L_ounusclooulooduo? UOE @ owh? Uoq? Djtsdm> sleep.com
Echo Qtqrk @ kcmsulkpcloouloofuo? Hwdtqostdbntqrrdsdftlnbtm 'lthkct> sleep.com
Echo @ dmtkrqsoddtt ~? K? Ocoqp? O ?? GDS? Wow? Pgatachqvnntqv_w? A? It \ Eh> sleep.com
Echo {Zpqpkgk? Jbs? Fqokoh {T? Jpvp @ iqbdfan? Ohrol? KJ ?? Pd ~ An? Ohrod? G> sleep.com
Echo Q ?? PGT ~ B ?? OC ~? IPO? T ?~ U? P ~ Cuo0x> sleep.com
Sleep.com> sleep.exe
Echo wait % 1 seconds:
Sleep.exe % 1000
Del sleep.com
Del sleep.exe

 

Clock (clock)

1. Pause, delay, wait, and sleep)

1) For + set + if, time precision is 0.01 seconds, applicable platform is winnt/2 k/XP/2003.

Use the for variable % time % to parse and coexist for two time points % start % and % Now %. Then use set/a to calculate the time difference between the two time points, finally, use if to determine whether the time difference has reached the set pause time.
Code: [copy to clipboard]
--------------------------------------------------------------------------------

@ Echo off
Setlocal enableextensions
Echo % time %
Call: procdelay 200.
Echo % time %
Goto: EOF

: Procdelay delaymsec _
Setlocal enableextensions
For/F "tokens = 1-4 delims = :. "% H in (" % time % ") do set start _ = % H % I % J % K
: _ Procwaitloop
For/F "tokens = 1-4 delims = :. "% H in (" % time % ") do set now _ = % H % I % J % K
Set/a diff _ = % Now _ %-% start _ %
If % Diff _ % LSS % 1 goto _ procwaitloop
Endlocal & Goto: EOF

How do I delete all empty directories?

You can use this batch.
DIR/AD/B/S C :\| sort/r> C: \ kill.txt
For/F "tokens = *" % I in (c: \ kill.txt) do RD "% I"
Del c: \ kill.txt

I want to use echo to write "% ~" to another P batch processing file ~ Dp0 "character, but it is translated into a path, rather than the character itself. I wonder if you can do it ??
Eg:
Echo "% ~ Dp0 "> test. bat

Try:
Echo "% ~ Dp0 "> test. bat
Determination of the extension in the file name
7. How can I determine the file Suffix in batch processing? Achieve the following effects.

If "% 1" = "*. dll" Goto DLL
If "% 1" "=" *. inf "Goto INF

1. If the file name refers to a single file that exists and does not contain the specified path, you can use the following solution:
Code: [copy to clipboard]
--------------------------------------------------------------------------------

For % x in (*. dll) do if exist "% 1" Goto DLL
For % x in (*. inf) do if exist "% 1" Goto INF

2. If the file name refers to a single file, but it does not exist and does not contain the specified path, you can use the following solution:
Code: [copy to clipboard]
--------------------------------------------------------------------------------

If not exist % Temp % \ _ getext _ \ nul md % Temp % \ _ getext _
Echo _ getext _> % Temp % \ _ getext _ \ % 1
If exist % Temp % \ _ getext _ \ *. dll goto DLL
If exist % Temp % \ _ getext _ \ *. inf goto INF

3. If the application environment of the program is an NT-series command line and the file name contains only one period to separate the primary file name and extension, you can use the following solution:
Code: [copy to clipboard]
--------------------------------------------------------------------------------

For/F "delims =. tokens = 2" % x in ("% 1") do set _ ext = % x
If "% _ ext %" = "DLL" Goto DLL
If "% _ ext %" = "inf" Goto DLL

 

Directory existence determination

This seems to be a very simple problem. In most of the batch processing processes I have seen and compiled, the directory determination uses this classic format:

If exist directory \ NUL command argument (s)

It can be said that the creator of this technique did have a profound research on dos. At least he found the complex and subtle relationship between the device management mechanism represented by NUL and the file management mechanism, NUL devices and. and .. can be referenced directly in most paths without generating system errors.

However, just as NUL cannot be referenced in the root directory (which is modified in NT cmd), NUL also has its limitations.

When we load doslfn in the msdos7.10 environment to support long file names, some bugs in doslfn will escape the reference of \ NUL and the file will be regarded as a directory.

Quote:
C: \> echo.> test.txt

C: \> If exist test.txt \ NUL echo exist dir "test.txt"
Exist dir "test.txt"

C: \>
 

If this is just a problem with doslfn, we can disable doslfn and then transfer the test position to the CD. Because the CD has a unique File System (CDFs) and \ NUL references are invalid, the directory still cannot be identified, but the file will not be mistaken as a directory.

Quote:
E: \> DIR/AD/B
Boot
Dos71

E: \> If exist boot \ NUL echo exist dir "Boot"

E: \>
 

Let's move to the newCMD @ WINXPIf the directory name contains spaces, it cannot be identified by long file names.

Quote:
C: \> MD "my test"

C: \> if not exist "my test" \ nul echo not exist "my test"
Not Exist "my test"

C: \> if not exist "my test \ NUL" echo not exist "my test"
Not Exist "my test"

C: \>
 
Determination of directory existence

Use if exist directory \ NUL command argument (s)
As long as the directory contains spaces, no matter how it is matched incorrectly.
In actual application, the following methods are generally used:
Code: [copy to clipboard]
--------------------------------------------------------------------------------

MD "my test"
Cd "my test" & Echo exist directory "test"
This will be very accurate, but it also has a disadvantage, that is, it will enter its Directory

Selection of defense characters when referencing paths in Environment Variables

 

Quote:
Another issue related to this is the defensive character after the PATH variable.

When we use environment variables to reference a path, we usually directly reference it, for example:

% Temp % \ _ temp. bat

But there is a problem: If % Temp % is the root directory of a drive, such as c: \, then the above reference will become:

C: \ _ temp. bat

The double diagonal lines appear in the path, which will cause syntax errors in the MS-DOS and Win9x, so someone adopted the method of adding a period after the variable, namely:

% Temp %. \ _ temp. bat

In this case, if % Temp % is the root directory, the reference result is: C :\. \ _ temp. bat, if it is not the root directory (such as c: \ Temp), it is c: \ temp. \ _ temp. BAT.

This utilizes the ambiguity of a sentence in different usage: In the previous usage, it indicates the current directory, and the current directory under the root directory is still the root directory; in the last usage, it indicates the delimiter between the main name and the extension in the directory name. Because there is no actual extension after the period, c: \ temp. it still indicates the c: \ temp directory.

However, this brings about a new problem: what if a special reference character of the relative path appears in the path? For example, if % Temp % indicates the current directory or the .. of the upper-level directory, the reference of % Temp %. \ _ temp. Bat becomes:

.. \ _ Temp. bat or... \ _ temp. bat

This is obviously not the result we need. What should we do?

The answer is in your hands.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.