Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: system ("");  (Read 4872 times)

0 Members and 1 Guest are viewing this topic.

UNLISTED

  • Guest
system ("");
« on: December 11, 2004, 12:55:30 PM »
I was trying to make a program in c++ that would example, insert a copy of its self on the desktop (windows 98) so i put this:

#include <iostream.h>
#include <stdlib.h>

int main()
{

     system("copy %0 c:\windows\desktop\Prog.exe");
}

but when I compile it, it displays the message 'unknown escape sequence \w \d \p'
what do I do?

skipper

  • Guest
Re: system ("");
« Reply #1 on: December 12, 2004, 12:00:48 AM »
The \ is an escape character in double quoted strings.  It preceeds a character selected for a special meaning.  For example, \t is the tab character and \f is the form feed characters, things that are otherwise quite hard to display in strings.  Since \w is not a special character, you get the complaint you got.

What you want to do is DOUBLE the \ characters, e.g.
system("copy %0 c:\\windows\\desktop\\Prog.exe");

Alternatively, use forward slashes, which are not special, and which Windows *may* properly interpret.  On UNIX systems, where C originated, file system names use forward slashes.  On those systems the backslash is used for "escaping" in numerous situations including C/C++.

Don't know why DOS chose to ignore the "standard" of that day and age (late 1970's).

Regards,
Skipper

ComPnoob

  • Guest
Re: system ("");
« Reply #2 on: December 18, 2004, 08:32:10 PM »
isnt .h for C? i thought c++ is only <iostream>

UNLISTED

  • Guest
Re: system ("");
« Reply #3 on: December 19, 2004, 12:27:16 PM »
Thats great thanks! BUT it will not work with:

%0

it sais 'file not found' so i replace %0 with file.exe and rename it file.exe and it works.. does

'copy %0'

only work in batch files?

skipper

  • Guest
Re: system ("");
« Reply #4 on: December 21, 2004, 07:45:56 AM »
I'm not sure what you want %0 to be.  Assuming you are trying to refer to the "current job" in JOB CONTROL, the reference makes no sense, since the curent job is the full text of your command, and you would want just a file name.

In general, the system command runs in a subshell where the only context is the text you are executing, plus whatever ENVIRONMENT variables are defined.

Trying to refer to past commands is fruitless as there are none.

Also, on many UNIX systems the system command will be run in 'sh', not 'bash' unless you take steps otherwise.  There is no job control in plain old 'sh'.

UNLISTED

  • Guest
Re: system ("");
« Reply #5 on: February 21, 2005, 09:06:02 AM »
i meant the complete file name.
EX: (WINDOWS 98)
say i want to have a file located at C:\windows\thefile.exe
and its a cpp program and im using the SYSTEM("") command. i want it to copy itself onto the desktop. but the SYSTEM("") will allow me to enter DOS commands into the program. i dont want to just put

SYSTEM("COPY C:\\WINDOWS\\THEFILE.EXE C:\\WINDOWS\\DESKTOP\\THEFILE.EXE");

I want it to be copied even if the filename is changed (renamed) I know the DOS command for that is:

COPY %0 C:\windows\desktop\filename.exe

But when i put that:

SYSTEM("COPY %0 C:\\WINDOWS\\DESKTOP\\FILENAME.EXE");

it wont understand the '%0' part. im using DEV C++
any help please?

UNLISTED

  • Guest
Re: system ("");
« Reply #6 on: February 21, 2005, 09:07:26 AM »
i meant windows 98. sorry for the 8)

skipper

  • Guest
Re: system ("");
« Reply #7 on: February 21, 2005, 09:47:49 AM »
The %0 in DOS is shorthand for "the filename of the program currently running".  The %0 is interpreted by the DOS batch processing program.  What it does is call some system routine or look at some system global variable (not sure which) and replace the %0 with that text.

If you are programming, you have to do the interpretation replacement yourself.

Look for a library routine or system global variable that is "the filename of the currently running program".  That routine/variable should return a string, which should be the name you want.

The exact nature of the call will depend on your execution context (.NET, not .NET, Win2K, Win98, C++, C#, C, Java, Perl, etc.).

It should take no more than a few minutes to find the correct call.  

Here is one web site I found in 7 minutes of web search that may or may not apply to the context you are executing.  It should give you some ideas on how to do the "move" even while the file is executing (it is easy in Unix/Linux, harder in Windows):
  http://www.catch22.net/tuts/selfdel.asp

It mentions the following call, which may be the translation function you are looking for:
 TCHAR szEXEPathname[_MAX_PATH];
 GetModuleFileName(NULL, szEXEPathname, _MAX_PATH);


Regards