Move and rename files

dalvarezpenarandadalvarezpenaranda Member Posts: 4
Hi! I wanna take a 300 files from a folder, I know their extensions but I dont Know what is their name. I would like how to do to rename each one and move into another folder. I found 2 post where explains the code but I can run it into navision. Can I do it with codeunit or with dataport?

Does anyone help?
A lot of thanks!

Comments

  • krikikriki Member, Moderator Posts: 9,112
    global variable:
    recFile record : File
    // This code serves to reset the variable. 
    // For some or other reason, the next times you filter on the same subdir, Navision does NOT update the information
    recFile.RESET;
    recFile.SETCURRENTKEY("Path,"Is a file",Name);
    recFile.SETRANGE("Path,'C:\');
    IF recFile.FINDFIRST THEN ;
    
    // Now the real code:
    recFile.RESET;
    recFile.SETCURRENTKEY("Path,"Is a file",Name);
    recFile.SETRANGE("Path,'C:\Some subdir');
    recFile.SETRANGE("Is a file",TRUE);
    recFile.SETFILTER(Name,'*.txt');
    IF recFile.FINDSET THEN
      REPEAT
        MESSAGE('Filename=%1',recFile.Name);
      UNTIL recFile.NEXT = 0;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ta5ta5 Member Posts: 1,164
    1) use the file-table (virtual table in navision), set the appropriate filters
    2) loop through the file-table
    3) use the navision rename command

    hope this helps

    thomas
  • snehanshusnehanshu Member Posts: 9
    Boss, The 'File' system table is readonly table. You can not modify it;
  • krikikriki Member, Moderator Posts: 9,112
    The table is indeed read-only. You can use it to search for files, then you need use the rename-command of File (F5=>File=>Functions=>RENAME) and the NOT of the record.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • snehanshusnehanshu Member Posts: 9
    Hi,

    I believe, there are 2 tasks are involved.

    1. Renaming the file
    2. Moving (same as copy pasting) the file to a new location.

    I hv achieved the above tasks with a very simple procedure:

    1. used the file record to traverse through the files.
    2. created a batch file (.BAT) having dos commands (Move %1 %2)
    3. Called the Batch file with navision Shell function.

    Filerec.setrange(Path,<ur location>);
    IF Filerec.FINDFIRST THEN
    Repeat
    Shell('<Batch file>',Filerec.Path + '\'+Filerec.name + ' ' + <New file name with path of new location>);
    Until Next = 0;
    Message(Processing Complete');

    Thanx

    Snehanshu
  • krikikriki Member, Moderator Posts: 9,112
    Better use http://www.mibuso.com/forum/viewtopic.php?t=12417 in stead of the SHELL-command.
    With this you can use "cmd.exe /C move ..." and you can avoid the dos-box.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • StLiStLi Member Posts: 83
    i know this is old but i stubled over it thanks to google. So.. in case someone else has the same fate:

    you could just loop through the files with the help of the virtual file table as kriki described and then use the "File.rename"-command somehow like this:
    vars:
    ShortFileName[text]
    GLSetup[rec of general ledger setup]
    
    ArchiveFile(_Filename : Text[1024])
    ShortFileName := _Filename;
    
    WHILE STRPOS(ShortFileName, '\') <> 0 DO
      ShortFileName := COPYSTR(ShortFileName, 1 + STRPOS(ShortFileName, '\'));
    
    FILE.RENAME(_Filename, GLSetup."Archive Folder" + ShortFileName);
    

    p.s. i recomend to always use the absolute path 'cause nav tends to mess with the workingfolder.
Sign In or Register to comment.