Fileaze
PrepLogic.com

FAQ

(Frequently Asked Questions)

  1. * (all questions)
  2. E-mail replies
  3. Off-line versions of my site?
  4. Support for my scripts
  5. Link exchange
  6. Checking command line arguments
  7. Setting variables in FOR loops (or in code blocks)
  8. I want to read ... (some system property)
  9. Use the date in a directory or file name
  10. Hide a batch file window
  11. Empty the Recycle Bin
  12. Remove temporary files and clear IE cache
  13. Unattended FTP file transfers
  14. "Bulk" rename files or folders

 

Did you try the Search entry in the menu?

Or one of these?

Google

 www.robvanderwoude.com  Web
 
Add a search engine to YOUR web site!

 www.robvanderwoude.com  Web

These search boxes are identical to the ones on the Search page, which can be opened using the menu.

 

Back to the top of the page ...

 

Sponsored link:
Windows grep software to search (and replace) through files and folders on your PC and network
Windows grep software to search (and replace) through files and folders on your PC and network

 

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, 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.

 

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 ...

 

Off-line versions of my site?

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

I haven't.

 

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 ...

 

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 ...

 

Checking command line arguments

DOS has been around for 27 years now (writing 2007), 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 27 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.

 

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 ...

 

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 WMI Code Generator. It can generate code for a subset of the available properties, and it can generate the code in (NT) batch, JScript, KiXtart, Object Rexx, Perl, PowerShell (formerly known as MSH or Monad), Python 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 Windows XP Professional and Server 2003 users, 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 detailes on command line use.
Or use the WMI Code Generator's "Create Batch Code" button 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, Windows XP, Windows Server 2003 and Vista, available as add-on for Windows 95/98 and Windows NT 4).

 

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...

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:

First remove the leading day of the week:

FOR %%A IN (%Date%) DO SET Today=%%A

Then remove the delimiters and change the order.
If your computer uses mm/dd/yyyy format:

FOR /F "tokens=1-3 delims=/-" %%A IN ('ECHO.%Today%') DO SET SortDate=%%C%%A%%B

or for dd-mm-yyyy format:

FOR /F "tokens=1-3 delims=/-" %%A IN ('ECHO.%Today%') DO SET SortDate=%%C%%B%%A

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%.*

 

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, it is impossible in batch all by itself. However, there are several ways to minimize or even hide the window immediately after opening, so you'll only see the window in a flash.

Run a batch file minimized (it will still be visible in the task bar):

START /MIN "your title" "d:\some dir\yourbatch.bat"

See this JSIFaq tip on how to make a batch file start another batch file hidden (uses a temporary VBScript file).

Or download and use my runNhide.vbs.

Make a batch file minimize its own window:

TITLE MinimizeMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "MinimizeMePlease"') DO CMDOW %%A /MIN

Make a batch file hide its own window:

TITLE HideMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "HideMePlease"') DO CMDOW %%A /HID

Start a batch file hidden:

CMDOW /RUN /HID "d:\some dir\yourbatch.bat"

or:

PSEXEC -d CMD /C "d:\some dir\yourbatch.bat"

Or if you don't mind a 1 minute delay:

SOON 60 "d:\some dir\yourbatch.bat"

Or use AT or PMSOON instead of SOON.
You can also create a scheduled task and run the task with JT or SCHTASKS.

Use an alternative scripting language:

To hide a batch file window you can also use KiXtart or VBScript.

My runNhide.vbs runs any command specified on the command line in a completely hidden window.

Due to limited command line parsing, you'll need a different approach in KiXtart.
Just insert these lines at the top of your batch code, right after the @ECHO OFF line:

> Temp.kix ECHO SetConsoleˆ("Hide"ˆ)
KIX32.EXE Temp.kix
DEL Temp.kix

The batch file will still start with a "black window", but in a fraction of a second that window will become invisible.
Start it minimized if possible, and you'll hardly notice it starting at all.

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

An alternative approach that completely hides the command window is to hardcode a batch command in a KiXtart script that uses KiXtart's Run command, and then use WKIX32.EXE instead of KIX32.EXE to run the script.

 

The JT and SOON utilities mentioned here are part of the Windows 2000 Resource Kits.
CMDOW and PSEXEC are third party tools.
PMSOON is my own "poor man's version" of SOON.
START and AT are native commands in Windows NT 4 and later.
SCHTASKS is native in Windows XP (and maybe Server 2003).
KiXtart can be downloaded from kixtart.org
VBScript is native in as of Windows 98 and 2000, though it isn't installed by default on Windows 98 or NT 4.
Besides, you may want to download and use only the latest version.

 

Note: Both CMDOW and PSEXEC may sometimes be (wrongly) accused of being spyware by some Anti-Virus and Anti-Spyware scanners.
They are not, but they certainly can be abused for "less honorouble" purposes.

 

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-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. BinManager by Tim Tabor. BinManager is both a shell extension for the Recycle Bin, and a scriptable component for managing the Recycle Bin.
    My own DelTrash2.vbs script uses BinManager to either empty the Recycle Bin completely, or remove only files older than a specified number of days.

 

Back to the top of the page ...

 

Remove temporary files and clear IE 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.

 

Back to the top of the page ...

 

Fileaze banner

 

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 with Fileaze (a third party GUI tool): it now takes only 5 mouse clicks to upload all changes (and it wouldn't require a lot of effort to make that completely unattended). And the uploads are fast! An average upload now only takes a couple of seconds, instead of the minutes I had got used to.

You can try the Fileaze full version for 30 days, or use the free LITE version indefinitely -- but the latter does not support FTP.

 

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:

  1. All files need to get a new extension:

    REN *.* *.new

    This will fail if duplicate names exist (e.g. if both File1.txt and File1.rtf exist, only one of them can be renamed to File1.new)
  2. Files of a certain type need to get an alternative extension:

    REN *.log *.txt

    This too will fail if duplicate names exist (e.g. if File1.txt aleready exists, File1.log cannot be renamed)
  3. If you don't care if files are overwritten in the renaming process, consider using MOVE /Y instead of REN:

    MOVE /Y *.log *.txt

    If both sample.txt and sample.log existed before issuing this command, the original sample.txt will be overwritten by the renamed sample.log (works only in Windows 2000 and later).
  4. "Numbered" files need to be reorderd, say to close "gaps" in the number sequence, or to order on file size, last modified date, or whatever:

    SETLOCAL ENABLEDELAYEDEXPANSION
    SET Count=0
    FOR /F "tokens=*" %%A IN ('DIR /OD /B *.JPG') DO (
        REN "%%~A" !Count!.*
        SET /A Count = !Count! + 1
    )
    ENDLOCAL


    This will rename the oldest JPG file to 0.JPG, the oldest but one to 1.JPG, etc.
    This will fail if numbered JPG files already exist. In that case, first rename them to !Count!.### instead of !Count!.*, and then use REN *.### *.JPG to restore the original extension.
  5. Files need to be renamed after the current date or have (part of) the date or time appended to their names
  6. For more complex bulk renaming, try a third party bulk renamer, like:
  7. Or use regular expressions or any other means to create temporary batch files that, after being checked and approved "manually", will perform the actual renaming process.

 

Back to the top of the page ...