Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Syntax of commands in BAT file

5 views
Skip to first unread message

Pamela

unread,
Jun 18, 2022, 5:16:09 AM6/18/22
to
I use the commands shown below in a .BAT file in order to run Mark
Russinovich's "Contig" defragger on a file called SIGNATURE.DBM.

What is the correct syntax to add a second file to be defragged when
this .BAT file runs?

This runs on WinXP/SP3.

----

cmd /k; "C:\Program Files\Contig defragger\Contig.exe" -v "c:
\SIGNATURE.DBM"

R.Wieser

unread,
Jun 18, 2022, 5:45:21 AM6/18/22
to
Pamela,

> What is the correct syntax to add a second file to be defragged
> when this .BAT file runs?

Your batchfile is a bit special. It doesn't just run that defragging, but
is ment to be used from a Windows shortcut. The "cmd /k" part makes sure
the console window stays open after the defragging program terminates (so
you can check if everything went well).

You could try to append a command using the "&" symbol :

cmd /k; "C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
& "C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"

, but that might run into the command-line max length limit (about 260
chars)

IOW, that "cmd /k" method is not really ment for multiple commands.


There are a few solutions though:

1) If you only need to run the defragging and than close the console window
you could remove the "cmd /k;" part and put a "pause" command at the end :

"C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
"C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
pause

The two instructions will be executed, and a "Press any key to continue ..."
message will be shown. After pressing that "any key" the console window
will close itself.

...which might not be what you want.

2) You can use two batchfiles, the first calling the second :

-- first batchfile --

cmd /k second.bat

-- second.bat file --

"C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
"C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"

This way the first batchfile will open a console window that stays open,
after which the second batchfile will execute whatever you like.

Hope that helps.

Regards,
Rudy Wieser


J. P. Gilliver (John)

unread,
Jun 18, 2022, 6:56:35 AM6/18/22
to
On Sat, 18 Jun 2022 at 11:45:06, R.Wieser <add...@not.available> wrote
(my responses usually FOLLOW):
>Pamela,
>
>> What is the correct syntax to add a second file to be defragged
>> when this .BAT file runs?
>
>Your batchfile is a bit special. It doesn't just run that defragging, but
>is ment to be used from a Windows shortcut. The "cmd /k" part makes sure
>the console window stays open after the defragging program terminates (so
>you can check if everything went well).

I am wondering what would happen if the "cmd /k" was put on a line by
itself, rather than the same line with a semicolon; would that open a
command window but then run subsequent lines in their own window(s)
which then close(s), or would those lines run in the window opened by
the "cmd /k" line?
>
>You could try to append a command using the "&" symbol :
>
>cmd /k; "C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
>& "C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
>
>, but that might run into the command-line max length limit (about 260
>chars)
>
>IOW, that "cmd /k" method is not really ment for multiple commands.
>
>
>There are a few solutions though:
>
>1) If you only need to run the defragging and than close the console window
>you could remove the "cmd /k;" part and put a "pause" command at the end :
>
>"C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
>"C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
>pause

or

C:
cd "\Program Files\Contig defragger"
Contig.exe -v "c:\SIGNATURE.DBM"
Contig.exe -v "c:\otherfile.ext"
pause

(-:
[The target file may not need to be in quotes either, depending on its
path.]

I'm wondering if two versions of contig can run at once. Given it's a
defragger, I suspect not. If it can, you might want to add the "start"
command. (Or the other way round; I forget the details.)
>
>The two instructions will be executed, and a "Press any key to continue ..."
>message will be shown. After pressing that "any key" the console window
>will close itself.
>
>...which might not be what you want.
>
>2) You can use two batchfiles, the first calling the second :
>
>-- first batchfile --
>
>cmd /k second.bat
>
>-- second.bat file --
>
>"C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
>"C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
>
>This way the first batchfile will open a console window that stays open,
>after which the second batchfile will execute whatever you like.

Clever!
>
>Hope that helps.

Ditto.
>
>Regards,
>Rudy Wieser
>
>
John
--
J. P. Gilliver. UMRA: 1960/<1985 MB++G()AL-IS-Ch++(p)Ar@T+H+Sh0!:`)DNAf

> > Won't you come into the garden? I would like my roses to see you. -Richard

R.Wieser

unread,
Jun 18, 2022, 8:23:44 AM6/18/22
to
John,

> I am wondering what would happen if the "cmd /k" put on a line by itself
> rather than the same line with a semicolon; would that open a command
> window but then run subsequent lines in their own window(s) which then
> close(s)

:-) I also thought of that and have tried it. Its the latter, but with a(n
expected) twist : the batchfile which runs the "cmd /k" command (which seems
to ignore the semicolon by the way) and waits for that console window to be
closed before it runs the following line(s).

> I'm wondering if two versions of contig can run at once.

Thats a good question I did not think about. Its Windows program though,
so it /should/ be able to cope with other programs using files running at
the same time.

>>There are a few solutions though:
>> 1)
...

>or
>
> C:
> cd "\Program Files\Contig defragger"

Yep, that would be a further optimalisation. In the case of the OP I
wanted to keep it as simple as possible though (no changes that do not have
anything to do with the question). Too easy to make it, to the OP, look
like a confusing mess.

>>2) You can use two batchfiles, the first calling the second :
...
> Clever!

:-) I'm pretty-much using that method myself to open a console window and
configure it (switching to 80x25 mode, setting some environment variables)

Though if a shortcut is used the whole contents of that first batchfile can
ofcourse just be put inside of it. But again, I didn't want to make any
assumptions (not even educated ones) or any change that would not be
directly related to the solution.

Regards,
Rudy Wieser


JJ

unread,
Jun 18, 2022, 9:30:34 PM6/18/22
to
CONTIG only accept one file command line argument. Either with exact file
name, or with wildcard(s) e.g.

contig.exe "d:\my files\*.dat"

With CONTIG, to defrag multiple files by specifying exact file names, CONTIG
has to be executed multiple times for each exact file name like the first
example given by R.Weiser. The "&" character is for command line separator.

Or, use a FOR loop e.g. (long text warning)

cmd /k for %A in (file.dat "file 2.dat" d:\folder\file.dat) do "C:\Program
Files\Contig defragger\Contig.exe" -v "%~A"

JJ

unread,
Jun 18, 2022, 9:30:59 PM6/18/22
to
>
> You could try to append a command using the "&" symbol :
>
> cmd /k; "C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
> & "C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
>
> , but that might run into the command-line max length limit (about 260
> chars)

Uh... that 260 chars is the limit for file path length. The command line
limit is 8192 chars. Within CMD, that would be after any variables have been
expanded.

JJ

unread,
Jun 18, 2022, 9:50:39 PM6/18/22
to
On Sat, 18 Jun 2022 11:55:19 +0100, J. P. Gilliver (John) wrote:
>
> I'm wondering if two versions of contig can run at once. Given it's a
> defragger, I suspect not. If it can, you might want to add the "start"
> command. (Or the other way round; I forget the details.)

It can, but the defragging algorithm will conflict with each other, since
CONTIG is not designed to be cooperative with its own instances.

e.g. CONTIG#1 is run first and is at 50% of defragging a big file
"d:\abc.dat". Then CONTIG#2 is run for a different file. CONTIG#2 then is
run for a small file "d:\xyz.dat", but CONTIG#2 may sea and use the free
space where the 2nd half of file "d:\abc.dat" would be, as the destination
location for "d:\xyz.dat". So CONTIG#2 may unknowingly sabotage CONTIG#1
defragmentation process.

R.Wieser

unread,
Jun 19, 2022, 3:41:15 AM6/19/22
to
JJ,

> Uh... that 260 chars is the limit for file path length.

Indeed.

> The command line limit is 8192 chars. Within CMD, that would be
> after any variables have been expanded.

Well, that makes sense, knowing that you /should/ be able to type two
looonnnggg filenames to a simple "copy" command. To be honest, I have
wondered about it for quite some time.

Thank you for mentioning it, as I can't remember having ever come across it
: although "cmd /?" shows a /lot/ of information, that tidbid is absent. :-\

(Now lets hope I will keep remembering it)

Regards,
Rudy Wieser


Paul

unread,
Jun 19, 2022, 5:10:26 AM6/19/22
to
Some competing shells, have a 2MB limit on their environment.
Which allows a somewhat longer line :-)

I would wonder if an 8192 limit would be in keeping with
the limits on the most expansive file system option. This is
optional and not the default on install. The funny thing about
this notion, was it was announced for earlier OSes, but never
"realized". And because it's not the default, we won't know
if it actually works or not. The 32,767 characters are likely
2 bytes each as well, rather than being USASCII. That's
so you could type 32,767 Hungarian characters.

https://www.itprotoday.com/windows-10/enable-long-file-name-support-windows-10

"Prior to Windows 10, the maximum supported file name length was 260 characters.

In Windows 10, that maximum was extended to 32,767 characters."

"(although you lose a few characters for mandatory characters
that are part of the name)"

HKLM\SYSTEM\CurrentControlSet\Control\FileSystem
LongPathsEnabled DWORD 1

Now imagine what your "copy from to" command looks like :-)

Paul


J. P. Gilliver (John)

unread,
Jun 19, 2022, 7:57:05 AM6/19/22
to
On Sun, 19 Jun 2022 at 08:50:34, JJ <jj4p...@gmx.com> wrote (my
responses usually FOLLOW):
That's the sort of thing I had in mind; basically (assuming the two
files are on the same partition!), I can't see it being possible to run
two instances of the same defragger at the same time - not unless there
are extra parameters about which part of disc, block sizes, and the
like.

Hence my wondering about whether START was needed (or is the opposite of
what's needed). I guess it depends on how the command itself - and the
batch shell - acts, in terms of returning control before it's completed.
--
J. P. Gilliver. UMRA: 1960/<1985 MB++G()AL-IS-Ch++(p)Ar@T+H+Sh0!:`)DNAf

There are things that we should be free by law to say but choose not to. A
right to offend doesn't mean a duty to offend.
- Timothy Garton Ash, RT 2016/4/9-15

Pamela

unread,
Jun 19, 2022, 8:30:45 AM6/19/22
to
That works fine for me. Thank you.

> 2) You can use two batchfiles, the first calling the second :
>
> -- first batchfile --
>
> cmd /k second.bat
>
> -- second.bat file --
>
> "C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
> "C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
>
> This way the first batchfile will open a console window that stays
> open, after which the second batchfile will execute whatever you
> like.

This is a bit messier than I would like on account of the additional
file.

R.Wieser

unread,
Jun 19, 2022, 9:56:37 AM6/19/22
to
John,

> I can't see it being possible to run two instances of the same defragger
> at the same time

Thats not too much of a problem as both instances could use some private IPC
method (like a a named pipe) to talk with each other about who's doing what
and where. And seeing the name of who wrote Contig (Mark Russinovich) I
would not be amazed at all if he did such a thing.

> Hence my wondering about whether START was needed (or is the opposite of
> what's needed).

Its the opposite. START can be used in a batchfile as a way to run several
programs next to each other - all in their own windows.

The batchfile mechanism itself already takes care of waiting for a started
program to terminate before advancing to the next line.

Regards,
Rudy Wieser


R.Wieser

unread,
Jun 19, 2022, 10:03:53 AM6/19/22
to
Pamela,

>> "C:\Program Files\Contig defragger\Contig.exe" -v "c:\SIGNATURE.DBM"
>> "C:\Program Files\Contig defragger\Contig.exe" -v "c:\otherfile.ext"
>> pause
...
> That works fine for me. Thank you.

You're welcome.

By the way, if you do not need to know what Contig all has to say than you
could choose to remove the "pause" at the end. The commands will than run,
but than as soon as the last one ends the console window will close too.

>> 2) You can use two batchfiles, the first calling the second :
...
> This is a bit messier than I would like on account of the
> additional file.

Hence the different options to choose from. :-)

Regards,
Rudy Wieser


Paul

unread,
Jun 19, 2022, 1:28:48 PM6/19/22
to
On 6/19/2022 8:30 AM, Pamela wrote:

> This is a bit messier than I would like on account of the additional file.

JKDefrag can defragment enumerated files.

There is a "doc" directory with info on the command line invocation.
Use double-quotes on paths with spaces.

JkDefrag f:\*.log D:\MySQL\Data\*

Although, I might try commands like this:

jkdefrag -a 1 -d 2 "f:\*.log" "D:\MySQL\Data\*" # Type 1, is "analyse only"
jkdefrag -a 2 -d 2 "f:\*.log" "D:\MySQL\Data\*" # Type 2 is "defragment"
jkdefrag -a 5 -d 2 "f:\*.log" "D:\MySQL\Data\*" # Type 3 is "push file toward origin"

Only the middle command is of much use in this case.

Normal usage is more like this:

jkdefrag -a 1 -d 2 Q: # Check Q: for fragmentation

I keep a copy of 3.36.0.2 on every C: drive. On Windows 10
it defragments the files larger than 50MB for me.

There's lots of things it won't defragment, but at least
it does user files.

Paul

JJ

unread,
Jun 19, 2022, 3:38:39 PM6/19/22
to
On Sun, 19 Jun 2022 05:10:14 -0400, Paul wrote:
>
> I would wonder if an 8192 limit would be in keeping with
> the limits on the most expansive file system option. This is
> optional and not the default on install. The funny thing about
> this notion, was it was announced for earlier OSes, but never
> "realized". And because it's not the default, we won't know
> if it actually works or not. The 32,767 characters are likely
> 2 bytes each as well, rather than being USASCII. That's
> so you could type 32,767 Hungarian characters.
>
> https://www.itprotoday.com/windows-10/enable-long-file-name-support-windows-10
>
> "Prior to Windows 10, the maximum supported file name length was 260 characters.
>
> In Windows 10, that maximum was extended to 32,767 characters."
>
> "(although you lose a few characters for mandatory characters
> that are part of the name)"
>
> HKLM\SYSTEM\CurrentControlSet\Control\FileSystem
> LongPathsEnabled DWORD 1

We can actually use file paths longer than 260 chars (up to 32767, AFAIK),
but the path must be using device namespace like the one listed by MOUNTVOL.
e.g.

dir "\\?\Volume{a03841f7-0842-11e6-aba0-806e6f6e6963}\my dir\blah dir"

Or...

type "\\?\Volume{a03841f7-0842-11e6-aba0-806e6f6e6963}\my dir\file.txt"

Or...

dir "d:\my dir" > \\?\Volume{a03841f7-0842-11e6-aba0-806e6f6e6963}\out.txt

The storage volume/partition doesn't even need to have any assigned drive
letter. Though it can't be used for all conditions.

In CMD, some commands would be inapplicable (e.g. VOL) or may fail. The
device namespace path would also be limited by CMD's 8192 chars command line
limit.

And if applications specifically validate a given path based only on Win32
namespace path, or only treat the given path as Win32 namespace path, they
will surely fail. e.g. ATTRIB, EXPLORER.
0 new messages