Moving file to another folder

bangswitbangswit Member Posts: 265
hi, how to move another file from folder A to Folder B ?
like cut and paste
thanks

Answers

  • kash387kash387 Member Posts: 111
    Hi Bangswit...
    Use the concept of File.

    FILE.COPY(FromName,ToName); // FromName is your file name with current location (path), ToName is the new location with File name

    FILE.ERASE(From);// This will erase your file with previous location...

    So your file will be cut and paste on new folder...!!!!
    Thanks,

    Kashyap
  • bangswitbangswit Member Posts: 265
    kash387 wrote:
    Hi Bangswit...
    Use the concept of File.

    FILE.COPY(FromName,ToName); // FromName is your file name with current location (path), ToName is the new location with File name

    FILE.ERASE(From);// This will erase your file with previous location...

    So your file will be cut and paste on new folder...!!!!

    like you said
    i add code like this
    FILE.COPY(FileName,'C:\');
    FILE.ERASE(FileName);
    

    but it was said
    the operating system cannot find the directory specified for the file C:\
    please check that the drive,directory and file names are correct
  • chowyunfatchowyunfat Member Posts: 11
    bangswit wrote:
    kash387 wrote:
    Hi Bangswit...
    Use the concept of File.

    FILE.COPY(FromName,ToName); // FromName is your file name with current location (path), ToName is the new location with File name

    FILE.ERASE(From);// This will erase your file with previous location...

    So your file will be cut and paste on new folder...!!!!

    like you said
    i add code like this
    FILE.COPY(FileName,'C:\');
    FILE.ERASE(FileName);
    

    but it was said
    the operating system cannot find the directory specified for the file C:\
    please check that the drive,directory and file names are correct

    you must specify the file path completely.
    copy('c:\a.txt','c:\b.txt');
  • bangswitbangswit Member Posts: 265
    chowyunfat wrote:
    bangswit wrote:
    kash387 wrote:
    Hi Bangswit...
    Use the concept of File.

    FILE.COPY(FromName,ToName); // FromName is your file name with current location (path), ToName is the new location with File name

    FILE.ERASE(From);// This will erase your file with previous location...

    So your file will be cut and paste on new folder...!!!!

    like you said
    i add code like this
    FILE.COPY(FileName,'C:\');
    FILE.ERASE(FileName);
    

    but it was said
    the operating system cannot find the directory specified for the file C:\
    please check that the drive,directory and file names are correct

    you must specify the file path completely.
    copy('c:\a.txt','c:\b.txt');

    but how can get that a.txt
    because fileName --> path + file name (example : C:\a.txt)
  • chowyunfatchowyunfat Member Posts: 11
    Maybe you can use the function Openfile in codeunit 412.
    It will open a window and return the complete path of which file you selected.

    ReturnParth:=FileDialog.OpenFile('',DefaultPath,0,'',0);
  • kash387kash387 Member Posts: 111
    but how can get that a.txt
    because fileName --> path + file name (example : C:\a.txt)

    that you have to write codes to read .txt file from that folder...

    for that you can use File (record) concept of NAV....

    like...
    RecFile.SETRANGE(Path,yourpath); // your path will be like, c:\abc like that abc is folder... 
    RecFile.SETRANGE("Is a file",TRUE);
    if RecFile.findfirst then repeat
     // here do coding that you will get file that is having .txt extension
     // then go for those two lines that i mentioned in first post
     // your complete code will be in this loop only...!!!
    until RecFile.next = 0;
    

    this is what i have done for one of my client...!!!
    Thanks,

    Kashyap
  • kinekine Member Posts: 12,562
    You can use this:
    CREATE(Shell);
    Path := Shell.GetParentFolderName(FullFileName)+'\';
    FileName := Shell.GetFileName(FullFileName);

    Where Shell is automation of type "'Windows Script Host Object Model'.FileSystemObject".
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • bangswitbangswit Member Posts: 265
    kash387 wrote:
    but how can get that a.txt
    because fileName --> path + file name (example : C:\a.txt)

    that you have to write codes to read .txt file from that folder...

    for that you can use File (record) concept of NAV....

    like...
    RecFile.SETRANGE(Path,yourpath); // your path will be like, c:\abc like that abc is folder... 
    RecFile.SETRANGE("Is a file",TRUE);
    if RecFile.findfirst then repeat
     // here do coding that you will get file that is having .txt extension
     // then go for those two lines that i mentioned in first post
     // your complete code will be in this loop only...!!!
    until RecFile.next = 0;
    

    this is what i have done for one of my client...!!!

    The path is undefined, so the user will select the file from any folder
    and recFile is table???
  • bangswitbangswit Member Posts: 265
    kine wrote:
    You can use this:
    CREATE(Shell);
    Path := Shell.GetParentFolderName(FullFileName)+'\';
    FileName := Shell.GetFileName(FullFileName);

    Where Shell is automation of type "'Windows Script Host Object Model'.FileSystemObject".

    i'm using this code like you gave me
    CREATE(Shells);
    Path := Shells.GetParentFolderName(FullFileName) +'\';
    FileName := Shells.GetFileName(FullFileName);
    MESSAGE('%1 - %2 = %3',Path,FileName,FullFileName);
    

    but in message box only appear --> \ - =
  • bangswitbangswit Member Posts: 265
    it works
    my full code is like this
    CREATE(Shells);
    Path := Shells.GetParentFolderName(FileName) +'\';
    FileTxt := Shells.GetFileName(FileName);
    FILE.COPY(FileName,'C:\' + FileTxt);
    FILE.ERASE(FileName);
    

    but it was said that the file is in used, and cannot be deleted
  • bangswitbangswit Member Posts: 265
    done already
    just like this
    CREATE(Shells);
    Path := Shells.GetParentFolderName(FileName) +'\';
    FileTxt := Shells.GetFileName(FileName);
    FILE.COPY(FileName,'C:\' +'\' + FileTxt);
    JFFile.CLOSE();
    FILE.ERASE(FileName);
    
  • bangswitbangswit Member Posts: 265
    after using automation
    my code in another form is error
    like i setup path for export is C:\Folder Name
    but after i export it... it is not C:\Folder Name\File Name.txt
    but C:\Folder NameFileName.txt
    is it because this automation?
    please help...
  • SogSog Member Posts: 1,023
    your code in the previous post did not include the path.
    And after the copy you should check if the copy is complete before erasing the file.
    something like a do until copycomplete;
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • bangswitbangswit Member Posts: 265
    Sog wrote:
    your code in the previous post did not include the path.
    And after the copy you should check if the copy is complete before erasing the file.
    something like a do until copycomplete;

    no.... i have 2 files
    1st.... import (the code i show here) -> it successfuly
    but in another form... while i exported it... it become an error
    because it doesn't read as a path, but a file name
  • kinekine Member Posts: 12,562
    like i setup path for export is C:\Folder Name
    you need to use C:\Folder Name\ (or add \ where appropriate in the process)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • FranklinFranklin Member Posts: 253
    Hi!!

    I have a dataport and i want cut the file when it have been imported. But when i try to do this NAV launch me an error.

    OnPostDataItem()
    vRutaOrigen := vRutaConfiguracion+'\LOCLPED.TXT';
    vRutaDestino := vRutaConfiguracion + '\Historico\LOCLPED.TXT';
    FILE.COPY(vRutaOrigen,vRutaDestino);
    FILE.ERASE(vRutaOrigen);

    Error:
    the system can not perform the operation because the file is in use


    I have write FILE.ERASE(vRutaOrigen) on PostDataport too but i have the same error. Why??
  • kinekine Member Posts: 12,562
    Because the file is still opened in the dataport. You need to do itin OnAfterDataport or close it manually through CurrFile.Close.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • FranklinFranklin Member Posts: 253
    kine wrote:
    Because the file is still opened in the dataport. You need to do itin OnAfterDataport or close it manually through CurrFile.Close.

    Sorry i have written CurrFile.ERASE but i want to write CurrFile.CLOSE

    My problem was i had written CurrFile.Close in other trigger. [-X

    Thx!!
  • bangswitbangswit Member Posts: 265
    kine wrote:
    like i setup path for export is C:\Folder Name
    you need to use C:\Folder Name\ (or add \ where appropriate in the process)
    that's right Kine :)
    thanks
  • MrJackalMrJackal Member Posts: 29
    This is an exception to running a routine on the nas using movefile.

    This message is for C/AL programmers:

    An exception was raised in method MoveFile. The OLE control or Automation server has returned error (HRESULT) -2147352567.
    The component did not provide the exception description.


    Help ?
  • SavatageSavatage Member Posts: 7,142
    OnPostDataport()
    CurrFile.CLOSE; //Close The Imported File
    SavedFileName := FORMAT(WORKDATE,0,'<Year4>'+'-'+'<Month Text,3>'+'-'+'<Day,2>'+'-'+DELCHR((FORMAT(TIME)),'=',':'))+'.txt'; //Create a New Filename for Imported File
    FILE.COPY(CurrDataport.FILENAME,FilePath+SavedFileName); //Copy File To New Location
    ERASE(CurrDataport.FILENAME);//Delete Original File
  • julkifli33julkifli33 Member Posts: 1,087
    hi all
    this is my code
    GLSetup.GET;
    SFolder.SETRANGE(Path,GLSetup."Import Path" + 'Item\');
    SFolder.SETRANGE("Is a file",TRUE);
    IF ISCLEAR(Shells) THEN
    CREATE(Shells);
    IF SFolder.FINDSET THEN
    REPEAT
    SFile.TEXTMODE(TRUE);
    SFile.WRITEMODE(FALSE);
    SFile.OPEN(GLSetup."Import Path" + 'Item\' + SFolder.Name);
    .
    .
    .
    .
    
     //Move File To History Folder
      Path := Shells.GetParentFolderName(GLSetup."Import Path" + 'Item\' + SFolder.Name);
      FileTxt := Shells.GetFileName(GLSetup."Import Path" + 'Item\' + SFolder.Name);
      FILE.COPY(GLSetup."Import Path" + 'Item\' + SFolder.Name,GLSetup."History Path" + 'Item\' + FileTxt);
      SFile.CLOSE();
      FILE.ERASE(GLSetup."Import Path" + 'Item\' + SFolder.Name);
    UNTIL SFolder.NEXT =0;
    
    if execute this code unit is just fine
    but if execute using webservice from C#,it will prompt me an error as in attachment
    it was said about I/O Exception
    is it about my shells?
Sign In or Register to comment.