Normal Topic Renaming a file with Sbasic (Read 844 times)
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Renaming a file with Sbasic
Sep 13th, 2011 at 10:02pm
Print Post Print Post  
I had used @localfiledialog and @AsynchShell  on a command button to present a selection choice of files to a user and once selected open the file. (The files are PDF)

It worked well except if more than one user was working there was the problem of more than 1 person working on the same pdf file.

So I figured the simplest thing to do was once a user selected the file I would use CreateAProcess() or @AsynchShell  and rename it and then move it to another directory.

My problem is I cannot get the rename or RN command to work properly from sbasic. I can open a cmd box and type my commands and all is good, but from @AsynchShell  or CreateAProcess my commands fail.

I believed the problem may have been the need of quotes when called from sbasic and I attempted to use them many ways including with  \ to make them literal but I could not figure it out.

I am sure it is something simple I am missing (forgetting) but...    Any Thoughts?

var Vresult as string
Var n as int
var Vrname as string

Vrname = ""C:\sesame\script\test1234.pdf" "done.pdf""
vresult = @localfiledialog("choose a Script to work with", "*.pdf")

//WriteLn(vresult)

scriptnameChosen = (vresult)

// call a pdf
n = @AsynchShell(vresult)

// rename a file
// n = @AsynchShell("rename C:\sesame\script\test1234.pdf done.pdf")
// n = @AsynchShell(vrname)
//n = @AsynchShell("\"ren"\" + "\" + vresult + " \"done.pdf\"")

//WriteLn(vrname)


  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Renaming a file with Sbasic
Reply #1 - Sep 14th, 2011 at 1:22pm
Print Post Print Post  
You have at least three issues here.

1. @AsynchShell won't do what you want.
2. The OS will stop you from renaming the file while it is open in another program.
3. Use @Chr(34) instead of a backslash and quote.

You can trick Sesame by using double quotes where you want a quote to appear inside of a string. So, something like this will actually work, but is not recommended:
n = @Shell("ren ""orig name.pdf"" ""new name.pdf""")

You really should use something like this:
@Shell( "ren " + @Chr(34) + "orig name.pdf" + @Chr(34) + " " + @Chr(34) + "new name.pdf" + @Chr(34) )

(BTW, a benefit of using @Shell is that it will return a "0" if it is successful, and a "1" if it is not.)
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Renaming a file with Sbasic
Reply #2 - Sep 14th, 2011 at 2:14pm
Print Post Print Post  
BOBSCOTT wrote on Sep 13th, 2011 at 10:02pm:
I had used @localfiledialog and @AsynchShell  on a command button to present a selection choice of files to a user and once selected open the file. (The files are PDF)

It worked well except if more than one user was working there was the problem of more than 1 person working on the same pdf file.

So I figured the simplest thing to do was once a user selected the file I would use CreateAProcess() or @AsynchShell  and rename it and then move it to another directory.
*Snip*


Hello Robert,

The problem that you are running into is that Rename is a DOS command, so you need to be in command window to run it. So to run it you're gonna wanna use @Shell. But, just kinda thinking out loud here, since the problem you're running into is multiple people working at the same time, renaming the file to a set name isn't going to work as when the second person goes to open another file their file is also going to be renamed to the same thing. And when would you rename it back?

Now one solution I can think of is to just use the DOS Move command instead and move the PDF to a folder named In_Use and then open it from there. That way the file no longer appears in the file dialog. But the problem then becomes when do we move it back? We could use @RedirectProcess to launch Adobe Reader to open the PDF file which would cause Sesame to wait until the PDF file was closed but that's no good if the user needs to switch back to Sesame and reference something while the PDF file is open. Another option would be to have another button that moves the file back from the In_Use directory but that's another button for the user's to click, or forget to click. We could whenever the button is clicked do a batch move of all the *.PDF files in the In_Use directory back into the main directory. But the question is are all of the PDF files stored in the same directory?

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Renaming a file with Sbasic
Reply #3 - Sep 14th, 2011 at 4:38pm
Print Post Print Post  
Ok I realize I still need to put sbasic  logic in to make sure there is an Id before continuing and  that the done directory does not already contain a file of the same name. however the below code seems to work well (at least in some quick tests) to pop up file selection, rename file, move file to different directory and then open the file.  Ray you are correct about why it was  important for me not to use re-direct,  because the users have dual screens so when the pdf is opened it opens on 1 screen and the user enters information into sesame app on the other that they are reading from the pdf.

Thanks again Carl and Ray for the help!

var Vresult as string
Var m as int
Var n as int
var Vrname as string

// pops up selection of pdf files in directory and Variable holder for file selected
vresult = @localfiledialog("choose a Script to work with", "*.pdf")

// Sets the name to use during renaming, moving and displaying pdf
vrname = (PatientId + "script.pdf")


// sets an element to original file chosen so a record of original file can be added to history
scriptnameChosen = (vresult)

// Renames pdf from original to new name of patient ID plus word script
n = @Shell( "ren " + @Chr(34) + vresult + @Chr(34) + " " + @Chr(34) + vrname + @Chr(34) )

// moves from script directory to done
m = @Shell( "move " + @Chr(34) + "c:\sesame\script\" + vrname + @Chr(34) + " " + @Chr(34) + "c:\sesame\done" + @Chr(34) )
     
// opens and displays the pdf
n = @AsynchShell("c:\sesame\done\" + vrname)
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Renaming a file with Sbasic
Reply #4 - Sep 14th, 2011 at 10:05pm
Print Post Print Post  
I have this working really nicely now but  I was wondering if there is a method of telling  @localfiledialog("choose a Script to work with", "*.pdf") the specific directory to look in?  is there a way for me to path over to c:\sesame\scripts and then call  @localfiledialog("choose a Script to work with", "*.pdf") or is there a way for me in application programming or ini file to specify where it opens from?
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Renaming a file with Sbasic
Reply #5 - Sep 16th, 2011 at 2:17pm
Print Post Print Post  
BOBSCOTT wrote on Sep 14th, 2011 at 10:05pm:
I have this working really nicely now but  I was wondering if there is a method of telling  @localfiledialog("choose a Script to work with", "*.pdf") the specific directory to look in?  is there a way for me to path over to c:\sesame\scripts and then call  @localfiledialog("choose a Script to work with", "*.pdf") or is there a way for me in application programming or ini file to specify where it opens from?


Hello Robert,

Currently in 2.5.2, there is not a command to change the directory it opens from. However in the next release of Sesame, which is 2.5.3, there is a command LocalCWD() that can be used to change the directory that @LocalFileDialog() displays when opened.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged