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 .
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 .
did you try using QuotedStr function?
ReplyDeleteYeah , didn't work ,it will create an empty file
ReplyDeleteAm I the only one missing a leading quotation mark before PathBC in the second attempt?
ReplyDeleteHamza Benzaoui I think you need to double quote the "*.backup" file name, i.e.
ReplyDelete"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]
Dorin Duminica Attila Kovacs this didn't do anything, It creates nothing , even if there is no spaces .
ReplyDeletePs : i tried that too sorry didn't mentioned it above
yeah, this is how it looks like . if i open CMD and and run it it works fine , but don't through Delphi
ReplyDeletehttps://plus.google.com/photos/...
Well, i had to use CreateProcess and it woks like a charm without adding any double quote . thanks guys
ReplyDeleteas you said there is no pipe with shellexecute, so i had to pipe the output to a file and that what i did :
ReplyDelete-------------------
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);
yeah. the ">" here's for redirecting the command output to a file
ReplyDelete