-1

when I try to move the "HelloWorld" folder to an usb drive it says acces denied in the command line

#include<iostream>
#include<string.h>

using namespace std;
int main()
{
  char src[50],dest[50],cmd[100],penPath[1];
  
  cout<<"\nENTER LETTER OF THE FLASH MEMORY (raw letter.  E.g: I,H,F,...): ";
  cin>>penPath;

  strcpy(src,"%USERPROFILE%\\Desktop\\HelloWorld");
  strcpy(dest,penPath);
  strcat(dest,":\\");
 
  strcpy(cmd,"move ");
  strcat(cmd,src);                                          
  strcat(cmd," ");
  strcat(cmd,dest);
  system(cmd);
  return 0;
}

I think that the problem its related to the usb path that is given, but i dont know how to solve

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
mrdarip
  • 3
  • 2
  • What's happening if you do that in a CMD terminal manually? That problem has barely to do with your c++ code, but exactly what the error message says: You don't have access rights for the target folder. Try running your program jwith admin privileges. – πάντα ῥεῖ Feb 19 '21 at 18:31
  • Don't waste your time with `system`. You're running a program that runs another program when you could do the deed directly. Since this is a Windows PC, [use `MoveFileExA`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexa) – user4581301 Feb 19 '21 at 18:46

1 Answers1

0

Based on this path "%USERPROFILE%\Desktop\HelloWorld" it seems like you are running this on Windows.

This question has been answered here: how do i fix: 'access denied' with the move command in windows 7?

The move command cannot move folders to another drive, you will have to use the robocopy command instead.

Your command should be something like this:

robocopy %USERPROFILE%\Desktop\HelloWorld F:\ /E /MOVE

where F is replaced by whatever drive letter is entered. It seems like you have no trouble concatenating strings, so I'll let you do it.

the /E flag tells robocopy to copy all subdirectories and the /MOVE flag tells it to delete the original after copying it to the new location.

jdabtieu
  • 534
  • 5
  • 20