Hello guys

Hello guys
i'm having a problem width passing spaces into CMD command !!
im trying to dump my PostgreSQL Database
here is my code:
---------------------------------------------------------------

PathBC:= ExtractFilePath(BackupDbSDlg.FileName);
NamePathBC:= StringReplace((ExtractFileName(BackupDbSDlg.FileName)) , #32 , '_', [rfReplaceAll]);

ShellExecute(0,'open',PChar('cmd.exe '),
Pchar('/c "C:\Program Files (x86)\PostgreSQL\9.6\bin\pg_dump.exe" -U postgres -F c DBNAME > '
+ PathBC + NamePathBC +'.backup'),nil,SW_HIDE);

---------------------------------------------------------------
this works fine but what if the user decide to create a folder have spaces in it name !!
I tried adding quotation marks like this
--------------------------------------
""C:\Program Files (x86)\PostgreSQL\9.6\bin\pg_dump.exe" -U postgres -F c DBNAME > '
+ PathBC + NamePathBC +'.backup"'
--------------------------------------

but this will create an empty backup file .

Comments

  1. did you try using QuotedStr function?

    ReplyDelete
  2. Yeah , didn't work ,it will create an empty file

    ReplyDelete
  3. Am I the only one missing a leading quotation mark before PathBC in the second attempt?

    ReplyDelete
  4. Hamza Benzaoui I think you need to double quote the "*.backup" file name, i.e.
    "C:/path/to/backups space here/now.backup"
    so, your entire string should be something like

    [single quote][double quote]C:/Program FIles (x86)/PostgreSQL/9.6/bin/pg_dump.exe[double quote] -U postgres -F c [space][double quote for backup file name][single quote] + EnsureTrailingPathDelimiter(PathBC) + NamePathBC + [single quote].backup[double quote for backup file name][single quote]

    ReplyDelete
  5. Dorin Duminica​ Attila Kovacs​ this didn't do anything, It creates nothing , even if there is no spaces .
    Ps : i tried that too sorry didn't mentioned it above

    ReplyDelete
  6. yeah, this is how it looks like . if i open CMD and and run it it works fine , but don't through Delphi
    https://plus.google.com/photos/...

    ReplyDelete
  7. Well, i had to use CreateProcess and it woks like a charm without adding any double quote . thanks guys

    ReplyDelete
  8. as you said there is no pipe with shellexecute, so i had to pipe the output to a file and that what i did :
    -------------------
    NamePathBC:= StringReplace((ExtractFileName(BackupDbSDlg.FileName)) , #32 , '_', [rfReplaceAll]) +'.backup' ; PathBC:=ExtractFilePath(BackupDbSDlg.FileName);
    cmd := 'C:\Windows\System32\cmd.exe';
    input := '/c "C:\Program Files (x86)\PostgreSQL\9.6\bin\pg_dump.exe" -U postgres -F c MyDBName > '+ NamePathBC;

    CreateOk := CreateProcess(PChar(cmd), PChar(input), nil, nil, false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,nil,Pchar(PathBC), StartInfo, ProcInfo);

    if CreateOk then
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);

    ReplyDelete
  9. yeah. the ">" here's for redirecting the command output to a file

    ReplyDelete

Post a Comment