string variable and command prompt

I know you can use a string variable with cd such as dir = 'C:Users\User\Documents\Matlab' then cd(dir). Is that same type of command possible using the system command such as [status] = system('move dir\file.txt newlocation')

2 comentarios

Adam
Adam el 20 de Jun. de 2018
I would have though you could try it and get an answer far faster than on here! As far as I know the string you give to the system function will be interpreted in the context of the command line so if it is valid in that context then it would work, otherwise not.
Stephen23
Stephen23 el 20 de Jun. de 2018
Editada: Stephen23 el 20 de Jun. de 2018
Why do you need to system for this? movefile is simpler and faster.

Iniciar sesión para comentar.

 Respuesta aceptada

Philip Borghesani
Philip Borghesani el 20 de Jun. de 2018

0 votos

Short answer: yes.
Long answer: Any MATLAB function that takes one or more character vectors as input can be called in command mode see command-vs-function documentation for more information.
I am a bit surprised you had to ask is something not working the way you expect it to?

4 comentarios

Keith Holmlund
Keith Holmlund el 20 de Jun. de 2018
it is not finding the file that I am wishing to move, would I need to strcat(dir\file.txt)
Adam
Adam el 20 de Jun. de 2018
Editada: Adam el 20 de Jun. de 2018
Ah, I see your problem now. You would need to do something like this:
filename = fullfile( dir, 'file.txt' );
newLocation = ... % Similar to filename - create the full file path here
cmd = sprintf( 'move %s %s', filename, newLocation );
system( cmd );
to use dynamic strings within a command like that. There may be a slightly neater way than what I posted, but that is the kind of idea, to put the string together in that manner and then pass it to the system command.
Keith Holmlund
Keith Holmlund el 20 de Jun. de 2018
thank you both, that ended up working
Philip Borghesani
Philip Borghesani el 20 de Jun. de 2018
You might try movefile for this instead. Error handling will be simpler, it can work on all platforms, and it is probably faster.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Etiquetas

Preguntada:

el 20 de Jun. de 2018

Editada:

el 20 de Jun. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by