Rob van der Woude's Scripting Pages

FAQ

(Frequently Asked Questions)

 

Did you try the Search entry in the menu?

Or this one?

This search box is identical to the one on the Search page, which can be opened using the menu.

 

Back to the top of the page ...

 

E-mail replies

It still amazes me each time it happens, but there are still people who hide or mask their e-mail address and still somehow seem to expect an answer to their questions.

This is a waste of time for both of us!

If you do not receive a reply within say 3 days, check my Failed Mail page.
If your answer isn't there, resend the message and do make sure a reply to your e-mail message is possible.

If in doubt, append your e-mail address manually in the body of the message. A (blind) carbon copy to yourself may also help you test your own reply address.

 

Another reason you might not get an answer is when you're on my blacklist.
If you ever abused my e-mail address to send unsolicited e-mail (spam), like "invitations" to join some group, you probably are on my blacklist, and all e-mails from the address you used for that unsolicited e-mail are deleted from my inbox automatically.
I'm very strict in this; having a published ("public") e-mail address I know it is stored in many people's address lists, and thus it has been and probably will be abused very often. Coping with the flood of relevant e-mails is more than enough work already.

 

Back to the top of the page ...

 

Malware?

Recently (April 2018) my AntiVirus software started sabotaging my script source code.
My batch files for generating this site's sitemaps and my scheduled backup scripts were all removed.

When contacting the AV software vendor, they blamed the BitDefender engine, and told me to add the script files to the exclusion list.
That requires a long list of exclusions!
Maybe the guys at BitDefender should acquire some scripting knowledge instead of attacking everything they don't understand 😉

I don't know about other AV engines, yet, but to me all AV software using the BitDefender engine is now on my blacklist.

Find out which AV software uses the BitDefender engine in this list of AV vendors.

 

Back to the top of the page ...

 

Link exchange

I do not exchange links.

If I think your site may be of interest to visitors of my own site, I will add a link, without any obligation for you to add a "reverse link".
Please do not link to my site unless it may be relevant to the visitors of your site.

 

Back to the top of the page ...

 

Translations of my site?

I'm sometimes asked if I can translate my site to Dutch, French, Spanish....

Sorry, I won't do it, it would require far too much time to translate all 493 pages.
Besides, I only speak 2 languages more or less fluently, and I can understand most of what is written in 2 more languages.

You can use Google to translate pages into almost any language.
Assume you want to translate https://www.robvanderwoude.com/ to Polish, then you could use the following URL:

https://translate.google.com/translate?hl=pl&sl=en&u=https://www.robvanderwoude.com/

This will open a translated web page, with all links leading to translated pages too.

To build your own URL:

Warning: Always be careful with machine-generated translations.
When I did a quick test of German translations, I found some funny but not-quite-correct results.
Do not use translated commands or code!
 
Side note: I'm sometimes asked for permission to translate a single page into some "obscure" (no offense intended) language that I cannot check, in return for a link to the translation.
The first time I was rather flattered, and fell for it. It sounded harmless enough not to suspect anything.
Thanks to James Petterson, who sent me some information on the SEO scam behind it, I know better now.
From now on I won't link to translations of my site anymore.

 

Back to the top of the page ...

 

Off-line copies of my site?

I'm sometimes asked if I have an off-line copy of my site available.

I haven't.

I can't, and won't try to, stop you copying my site to your local harddisk for private use.
However, you are not allowed to publish copies of my site, or parts of it, in any way without my prior written consent.

If you keep copies of my pages on-line for archival purposes, please exclude these copies from indexing by search engines (spiders).
My Google Webmaster logs are flooded with errors caused by indexed expired copies of my own pages.
You can use robots.txt to deny spiders access to certain files or folders.

 

Back to the top of the page ...

 

Support for my scripts

My sample scripts are provided as is, without any guarantee that they'll work on your computer. They are provided to teach and demonstrate scripting techniques, to enable you to write your own scripts.

Feel free to modify any script provided here, but don't expect me to debug your modified script for you if it doesn't work. That is not a free service.
I can and will try to help you understand the code provided here, but you'll have to make your own modifications and do your own debugging.

 

Back to the top of the page ...

 

Debugging a script

Not always will scripts run flawlessly on all systems, from the start.

I wrote some instructions on debugging batch files and debugging VBScript.
Please follow these instructions before yelling for help.

 

Back to the top of the page ...

 

Checking command line arguments

DOS has been around for 44 years now, and a lot has changed since the first MS-DOS version.

One question I got recently was about SHIFT and command line arguments.
In fact, the question was about checking for empty command line arguments, which 44 years ago was different from today in that now command line arguments may contain spaces (and double quotes).
In the example on my SHIFT page I used:

IF "%1"=="" GOTO Continue

which worked more or less OK in MS-DOS, but may cause problems in Windows.
Since arguments may contain spaces, double quotes are often used. Assume %1 equals "my 1st argument" then the check for empty arguments will become:

IF ""my 1st argument""=="" GOTO Continue

which will certaily cause an error message: 1st was unexpected at this time.
In the past, I used to solve this by modifying the check to:

IF [%1]==[] GOTO Continue

CMD.EXE (Windows NT 4 and later) makes this a lot easier:

IF "%~1"=="" GOTO Continue

%~1 equals %1 but without surrounding double quotes. If %1 didn't have double quotes, %~1 equals %1.

Note that this won't work in COMMAND.COM (MS-DOS and Win9x/ME).

Back to SHIFT, which was introduced as a work-around for the limit of 9 command line arguments (%1 .. %9).
I'm not sure if the following trick will work in the ancient MS-DOS versions, but it does work in more recent versions:

FOR %%A IN (%*) DO (
	•
	• Now your batch file handles %%A instead of %1
	•
)

No need to use SHIFT anymore — provided the arguments don't contain wildcards; if they do, the FOR loop will treat them as file names instead.

(wildcard exception added by Kevin Reagan)

 

Back to the top of the page ...

 

Setting variables in FOR loops

or in code blocks

... why can't I set a variable in a for loop?
... somehow it doesn't remember the variables set in the for loop.

Let me set this straight right away: you can set variables within FOR loops, but you cannot read them back within that same loop unless you use delayed variable expansion.

Delayed variable expansion is available since NT 4 SP*? and is explained here.

If your NT 4 version does not support delayed variable expansion, use subroutines for the variable manipulation instead. Type CALL /? for more details, or have a look at the CALL section of an AllHelp generated HTML file.

If your DOS version doesn't support subroutines, use CALLs to secondary batch files instead.

If your DOS version doesn't support CALL (pre MS-DOS 3.3) ... I give up.

 

Back to the top of the page ...

 

Read lines from a text file

We often need to process a list of computers, or a list of IP addresses, or a list of users, or...

In "true" MS-DOS (COMMAND.COM) this wasn't possible without the help of third party utilities.

In OS/2 MS-DOS box the (internal) READLINE command would do the trick.
Unfortunately, this command was never implemented by Microsoft in the "real" COMMAND.COM.

In Windows NT 4 (CMD.EXE) the (internal) FOR command was extended with several new command line switches.
One of these, the /F switch, makes FOR read a file line by line and store the current line in a temporary variable.
Unfortunately, FOR /F is far from intuitive, it may take a lot of practice to understand its inner details.

Not only files can be read using FOR /F, it can also be used to read lines from another batch command's screen output. Read FOR's on-screen help, or my FOR page, for more details.

A large percentage of my sample batch files uses FOR /F on other commands' output, so you may want to look at those sample batch files.
I also wrote a detailed explanation of FOR /F's tokens and delims parameters, so you may also want to read that, and get some hands-on experience with the real-world example it uses.

 

Back to the top of the page ...

 

Display messages on screen and

write them to a log file simultaneously

This is a regularly asked question lately, I wonder if it has anything to do with a batch course in progress...

In UNIX there is a native command to write to file and simultaneously display on screen: tee
Since I found it an extremely useful command, I tried to "port" the command to Windows using scripts, see my UNIX ports page.
If you prefer executables, try the links under Related Stuff on that page.

 

Back to the top of the page ...

 

I want to read ... (some system property)

Is there a way to read the video card type?
... screen resolution?
... MAC address?
... amount of physical memory?
... BIOS manufacturer?
... removable disk drives?
... network adapter's speed?
... list of available printers?
... installed fixes?
... laptop battery model and status?

And the list continues ...

Most of these properties can be read through WMI (Windows Management Instrumentation).
I won't explain WMI here (even if I could ...).
I will point you to some useful tools that can generate the code required to read these system properties, and they all use WMI.

The first one is Microsoft's Scriptomatic tool.
It will help you find the properties you want, generate and even execute the required code in VBScript, JScript, Perl or Python.

Based on the Scriptomatic is my own WMIGen. It can generate code for a subset of the available properties, and it can generate the code in (XP) batch, C#, JScript, KiXtart, Object Rexx, Perl, PowerShell, Python, Ruby, VB .NET and VBScript.

If it is only the basic hardware properties you want to read every once in a while, you may want to try my Basic Hardware Inventory tool. It too was inspired by and built with the aid of the Scriptomatic tool.

For users of Windows XP Professional and later versions, there is a fourth option: WMIC.EXE.
WMIC is a native tool in these Windows versions. It can be used in interactive mode, or in command line mode.
For occasional use, the interactive mode is most appropriate. Run WMIC in interactive mode by starting it without command line arguments.
For batch files the command line mode is perfect. Type WMIC /? for more details on command line use.
Or use WMIGen's "Batch" language choice to generate some WMIC command line examples.
You can even use simple SQL style filters on the WMI queries in WMIC, as is demonstrated in my DISKSPC.BAT and HARDWARE.BAT.
WMIC is well explained in this book: Understanding WMI Scripting by Alain Lissoir.

Scriptomatic, WMIC and Hardware.hta can all be used to read the properties of the local system, as well as remote systems, provided that WMI is installed on those remote systems (default on Windows 2000 and later versions; unfortunately, downloads for older systems are no longer available).

 

Back to the top of the page ...

 

Use the date in a directory or file name

I often get questions from people who need to create a directory with the date in its name, or rename a file to include the date in its name:

hi, is there a way to rename a file to today's date. for example, backup.txt to backup060814.txt where 060814 is yymmdd.
 
I'd like to script and schedule two tasks.
1. Create a new directory with the current date in the directory name and copy some files to it
2. Rename the [backup] image file to something with the date in it.
 
I need a .bat file that makes posible to get in a variable the actual day and month and use it to make a dir on an automatic directory that is created on my C:\files\2401 <— Today example ( daymonth ).

Well, you get the idea.

 

The quick-and-dirty solution for Windows NT 4 and later:
MD "C:\Backup\%Date:/=%"
or for file renaming:
REN myfile.doc "myfile_%Date:/=%.*"
Do not forget the quotes, since the new name may contain a space.
And you may have to replace the forward slash in %Date:/=% by your own computer's date delimiter.

 

The quick solution for Windows 9x/ME:
Use RealDate by Gabor Funk.
REALDATE /f="MD C:\Backup\CCYYMMDD" > tmp.bat
CALL tmp.bat
DEL tmp.bat
or for file renaming:
REALDATE /f="REN myfile.doc myfile_CCMMYYDD.*" > tmp.bat
CALL tmp.bat
DEL tmp.bat

 

For those of you who

read on.

• Different countries use different date formats like m/d/yyyy or dd-mm-yyyy.
To make matters worse, different Windows versions may or may not use leading zeroes and/or prefix the %Date% value with the day of the week.
Speaking of the day of the week, this is displayed in the system's selected language...
And then the year may be in 2 or 4 digits...

So where the value of the environment variable %Date% may be Tue 3/19/2024 on one computer, it may be 19-03-2024 on another one.

For sorting purposes, the ideal format for the %Date% variable would be 20240319 (yyyymmdd) or 2024-03-19 (yyyy-mm-dd).

If you know for certain that your batch file will always encounter the same date format, and that this date format will always be of the same length (i.e. uses leading zeroes), you can use this two step process to get the sorted date in a variable:

If you cannot be certain of the date format, then use my SortDate.bat to generate a 100% fool proof %SortDate% variable:

CALL SortDate.bat

Finally, use the %SortDate% variable to give the directory or file its new name:

SET BackupDir=%SortDate%
MD "C:\Backup\%BackupDir%"

or for file renaming:

REN myfile.doc "myfile_%SortDate%.*"

 

• As for TIME, this may be a little more complicated than DATE, because of AM/PM suffixes.

Harry Teufel and myself have kept ourselves busy for quite a while trying to make my SortTime.bat fool proof.
I dare say we succeeded, so we can now offer a solution to get both date and time in a file or direcory name:

CALL SortDate.bat
CALL SortTime.bat

Then use the %SortDate% and %SortTime% variables to give the directory or file its new name:

SET BackupDir=%SortDate%_%SortTime%
MD "C:\Backup\%BackupDir%"

or for file renaming:

REN myfile.doc "myfile_%SortDate%_%SortTime%.*"

 

• Lately I was asked if it is possible to add only the month and day, instead of the entire date.
The technique is almost the same, but you need to use another batch file, DateFmt.bat:

CALL DateFmt.bat mm dd /LZ
MD Logs\%DateFmt%

or for file renaming:

CALL DateFmt.bat mm dd /LZ
REN myfile.doc myfile_%DateFmt%.*

 

• A recent addition to the date/time tool set is DEBUG.
DEBUG itself has been around since the birth of MS-DOS, but recently I found I could use it to read the CMOS RTC.
Unfortunately, though, since DEBUG is a 16-bit command, it can be run on 32-bit Windows, but NOT on 64-bit Windows!
You can use Today.bat (date only) or TodayNow.bat (date and time) to create a directory:

FOR /F %%A IN ('TODAY.BAT') DO MD Logs\%%A

or for file renaming:

FOR /F %%A IN ('TODAYNOW.BAT') DO REN myfile.doc myfile_%%A.*

For month and day only:

FOR /F "tokens=1,2" %%A IN ('TODAY.BAT ˆ" ˆ"') DO MD Logs\%%A%%B

or for file renaming:

FOR /F "tokens=1,2" %%A IN ('TODAY.BAT ˆ" ˆ"') DO REN myfile.doc myfile_%%A%%B.*

 

• The latest development: SetDate.vbs, a simple script that sets a couple of environment variables with date values that can be used by batch files to (re)name files and directories.

These environment variables are (for today, Tuesday, March 19, 2024):

Date.Day=19
Date.DoW=3
Date.DoY=79
Date.Month=03
Date.Quarter=1
Date.Week=12
Date.Year=2024

They can be used in batch files like this:

MD "C:\Backup\%Date.Year%%Date.Month%%Date.Day%"

or for file renaming:

REN myfile.doc "myfile_%Date.Year%%Date.Month%%Date.Day%.*"

To be of any use, SetDate.vbs must be scheduled to run every midnight plus at every system start!

 

WMIC can also be used to query date and time, or its "components".
Try:

WMIC.EXE Path Win32_LocalTime Get /Format:LIST

and you'll get something like:

Day=19
DayOfWeek=3
Hour=8
Milliseconds=
Minute=46
Month=3
Quarter=1
Second=29
WeekInMonth=3
Year=2024

To store these values in variables, use:

FOR /F "tokens=1,2 delims==" %%A IN ('WMIC.EXE Path Win32_LocalTime Get /Format:LIST') DO (
	REM This second FOR loop removes trailing linefeeds from the values
	FOR %%C IN (%%B) DO (
		SET LocalTime.%%A=%%C
	)
)

Run the code above, and use the command SET LocalTime. to test the results:

LocalTime.Day=19
LocalTime.DayOfWeek=3
LocalTime.Hour=8
LocalTime.Minute=46
LocalTime.Month=3
LocalTime.Quarter=1
LocalTime.Second=29
LocalTime.WeekInMonth=3
LocalTime.Year=2024

Oops, we still have to add leading zeroes:

FOR /F "tokens=1,2 delims==" %%A IN ('SET LocalTime.') DO (
	REM Skip values 10 and higher
	IF %%B LSS 10 (
		REM Skip DayOfWeek
		IF NOT %%A==LocalTime.DayOfWeek (
			REM Skip Quarter
			IF NOT %%A==LocalTime.Quarter (
				REM Skip WeekInMonth
				IF NOT %%A==LocalTime.WeekInMonth (
					REM Add a leading zero
					SET %%A=0%%B
				)
			)
		)
	)
)
Note: If you are not interested in DayOfWeek, Quarter nor WeekInMonth, you can simplify this to:
  FOR /F "tokens=1,2 delims==" %%A IN ('SET LocalTime.') DO (
    IF 1%%B LSS 20 SET %%A=0%%B
)

Run the code above, and use the SET LocalTime. command again to test the results:

LocalTime.Day=19
LocalTime.DayOfWeek=3
LocalTime.Hour=08
LocalTime.Minute=46
LocalTime.Month=03
LocalTime.Quarter=1
LocalTime.Second=29
LocalTime.WeekInMonth=3
LocalTime.Year=2024

These variables can be used in batch files like this:

MD "C:\Backup\%LocalTime.Year%%LocalTime.Month%%LocalTime.Day%"

or for file renaming:

REN myfile.doc "myfile_%LocalTime.Year%%LocalTime.Month%%LocalTime.Day%.*"

 

Back to the top of the page ...

 

Hide a batch file window

Sometimes it would be nice to run a batch file hidden, without that "annoying black screen".

To tell you the truth, you can make the console window disappear immediately after appearing, but it is impossible to hide it completely with a 100% batch solution.

 

The quick and not that dirty solution:
Download and use RunNHide.exe
RUNNHIDE.EXE d:\path\yourbatch.bat

 

Make sure yourbatch.bat closes its own window in all circumstances, because you won't be able to see if it does.

For more techniques and detailed explanations, read this page: Hide the Console in Batch Files.

 

Back to the top of the page ...

 

Empty the Recycle Bin

There are several ways to empty the Recycle Bin without being prompted for confirmation.

  1. Quick-and-really-dirty batch file (NT 4 and later):
    		PUSHD C:\RECYCLER
    		IF /I "%CD%"=="C:\RECYCLER" RD /S /Q "C:\RECYCLER" 2>NUL
    		POPD
    
    		PUSHD D:\RECYCLER
    		IF /I "%CD%"=="D:\RECYCLER" RD /S /Q "D:\RECYCLER" 2>NUL
    		POPD
    		
    (assuming you have a C: and D: partition)

    Carefull, though, I'm not sure if you will be able to restore any files deleted after running this batch file, unless you empty the Recycle Bin manually, in which case you cannot restore any previously deleted files...
  2. FlushBin.bat (for Windows XP) uses Windows' Disk Cleanup Tool (CLEANMGR.EXE) to completely automate flushing temporary files, IE's cache, leftover ChkDsk files and the Recycle Bin.
    Read the comments in the batch file to learn how to modify the list of selected actions.
  3. DelTrash.exe by yours truly.
    Read its help text for optional command line arguments.

 

Back to the top of the page ...

 

Remove temporary files

and clear IE and Firefox cache

Temporary files can consume a lot of unnecessary disk (and backup) space, and having a large number of temporary files can significantly slow down your computer.

So how can one delete these files?

The quick-and-dirty way (NT 4 and later):

		PUSHD "%UserProfile%\Local Settings\Temp"
		IF /I "%CD%"=="%UserProfile%\Local Settings\Temp" (
			RD /S /Q "%UserProfile%\Local Settings\Temp" 2>NUL
		)
		POPD

		PUSHD "%UserProfile%\Local Settings\Temporary Internet Files"
		IF /I "%CD%"=="%UserProfile%\Local Settings\Temporary Internet Files" (
			RD /S /Q "%UserProfile%\Local Settings\Temporary Internet Files" 2>NUL
		)
		POPD
		

(this will clear both the Temp directory and the Internet Explorer cache)

Carefull, though, this should be done only if all applications have been closed, or you risk losing some data!

I wrote DelTemp.vbs (in VBScript), which has a Safe Mode option that will not delete TEMP files created after the last reboot.

An alternative (for Windows XP) is FlushBin.bat, which uses Windows' Disk Cleanup Tool (CLEANMGR.EXE) to completely automate flushing temporary files, IE's cache, leftover ChkDsk files and the Recycle Bin.
Read the comments in the batch file to learn how to modify the list of selected actions.

Another alternative for Windows XP, which requires Internet Explorer 7, is the following RUNDLL32 command:

		RUNDLL32.EXE inetcpl.cpl,ClearMyTracksByProcess 8
		

This command deletes Internet Explorer 7's temporary files (cache), not Windows' temporary files.

More options for this command can be found on my RUNDLL page.

Firefox users, have a look at my FfxCAC.bat and FfxCache.bat, two batch files that will clear all Firefox caches in all profiles (FfxCAC), or just the current user's (FfxCache).
They use techniques similar to the quick-and-dirty one described above.
In Windows XP, they use TASKLIST to check if Firefox is still active, and abort if so.

 

Back to the top of the page ...

 

Unattended FTP file transfers

Unattended file transfers are a hot item.

Third party tools can do this for you, but so can Windows' built-in FTP command.
All you have to do is "capture" (or log, or write down) one single manual FTP session and use that captured session as a script next time.

I explained it all on my FTP page.

But, er... can you keep a secret?
I upload my own web pages "manually" with FileZilla, a third party interactive GUI tool.

Check my Automations tools page for alternatives that can be scripted.

 

Back to the top of the page ...

 

"Bulk" rename files or folders

You can use REN (or RENAME) to rename files or, in Windows NT 4 and later, individual folders.

An alternative approach is using MOVE, which can rename and move files all in one go.

To bulk rename files (rename multiple files with a single command), there are lots of possible scenarios:

 

Back to the top of the page ...


page last modified: 2023-01-31; loaded in 0.0082 seconds