Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Ron1
Partner - Creator
Partner - Creator

File Copying From N Folders and Sub folders

I want to copy files from one location to another(exact same folder structure)(Can be N level of folders and Sub folders)

Condition has to be file created or modified date has to be current date. Also if the file exist in target,first back up of the file will be created at target the modified files will be copied.

Kindly help

1 Solution

Accepted Solutions
Kushal_Chawda

@Ron1  try below

Assumptions:

1) All the folders and sub folders name are identical in root folder of source and target (Consider Uat & Prod)

2) You may need to run Batch file to do this, so assuming the windows users account should have access to run the batch file (file with copy commands)

3) Machine on which Batch file placed, assuming user account running the Batch should have read/write access to file and folders on both UAT and Prod folders.

4) Backup folder is not under the Prod or UAT root folder. It should be separate file server. Id it is separate file server it should be accessible from both Prod & UAT.(Mainly from the server where Batch file will run)

You may need to run the Batch commands from QlikView using execute statements for which you need to do some settings in QlikView file and settings in server. Look at the below path for more details

https://help.qlik.com/en-US/qlikview/April2020/Subsystems/Client/Content/QV_QlikView/Scripting/Scrip...

let vUATQvdFolderRoot = 'C:\Work\Qlik\View\UAT'; // UAT root folder path

let vProdQvdFolderRoot = 'C:\Work\Qlik\View\PROD'; // Prod Root folder path

let vBatchFileLocation = 'C:\Work\Qlik\View\UAT\Batch'; // Batch file placement path

let vBackupFolderPath = 'C:\Work\Qlik\View\Backup'; // Backup root folder path

sub ScanFolder(Root)

          for each FileExtension in 'qvd'

                    for each FoundFile in filelist( Root & '\*.' & FileExtension)
                             // Capture file location and file last update time
                              FileList:
                              LOAD '$(FoundFile)' as SourceFile,
                                   filetime('$(FoundFile)') as FileTime

                              AutoGenerate 1;

                    next FoundFile

          next FileExtension

          for each SubDirectory in dirlist( Root & '\*' )

                    call ScanFolder(SubDirectory)

          next SubDirectory

end sub

Call ScanFolder('$(vUATQvdFolderRoot)') ; // Scan all UAT folders and sub folders inside root folder

Uat_Prod_qvd_path:
LOAD SourceFile as Uat_qvd_path,
     FileTime as Uat_qvd_filetime,
     '$(vProdQvdFolderRoot)'&TextBetween(SourceFile,'$(vUATQvdFolderRoot)','') as Prod_qvd_path // convert UAT folder path to Prod folder path
Resident FileList
where floor(FileTime)=floor(today());

DROP Table FileList;

Call ScanFolder('$(vProdQvdFolderRoot)') ; // Scan all Prod folders and sub folders inside root folder

// inner join used to check common qvds between uat and prod so that we can take backup of that on prod
find_existing_qvd_prod1:
LOAD Distinct mid(Uat_qvd_path,Index(Uat_qvd_path,'\',-1)+1,Index(Uat_qvd_path,'.',-1)-Index(Uat_qvd_path,'\',-1)-1) as QVD  // UAT Qvd names
Resident Uat_Prod_qvd_path;

Inner Join(find_existing_qvd_prod1)
LOAD Distinct SourceFile as Prod_qvd_path,
      mid(SourceFile,Index(SourceFile,'\',-1)+1,Index(SourceFile,'.',-1)-Index(SourceFile,'\',-1)-1) as QVD, // Prod QVD names
      mid(SourceFile,1,Index(SourceFile,'.',-1)-1)&'_'& date(Today(),'YYYYMMDD')
      & mid(SourceFile,Index(SourceFile,'.',-1),4) as Prod_qvd_name_with_current_time // Prod QVD names with current time appended for backup
Resident FileList;

DROP Table FileList;

find_existing_qvd_prod:
LOAD Prod_qvd_path,
     '$(vBackupFolderPath)'&TextBetween(Prod_qvd_name_with_current_time,'$(vProdQvdFolderRoot)','') as Prod_backup_qvd_path
Resident find_existing_qvd_prod1;

DROP Table find_existing_qvd_prod1;

// Create windows batch command to copy qvds from uat to prod folders
Copy_qvd_uat_to_prod:
LOAD 'xcopy /y '&Uat_qvd_path&' '&Prod_qvd_path&'*' as Copy_Uat_to_Prod_Commands
Resident Uat_Prod_qvd_path;

DROP Table Uat_Prod_qvd_path;

// create batch file to run commands
STORE Copy_qvd_uat_to_prod into $(vBatchFileLocation)\Copy_qvd_uat_to_prod.bat(txt) ;

DROP Table Copy_qvd_uat_to_prod;

// Create windows batch command to copy qvds from prod to backup folders
Backup_existing_prod_qvd:
LOAD 'xcopy /y '&Prod_qvd_path&' '&Prod_backup_qvd_path&'*' as Copy_to_Backup_Commands
Resident find_existing_qvd_prod;

DROP Table find_existing_qvd_prod;

// create batch file to run commands
STORE Backup_existing_prod_qvd into $(vBatchFileLocation)\Backup_existing_prod_qvd.bat(txt) ;

DROP Table Backup_existing_prod_qvd;


Execute cmd.exe /C "$(vBatchFileLocation)\Backup_existing_prod_qvd.bat"; // First run backup batch file

Execute cmd.exe /C "$(vBatchFileLocation)\Copy_qvd_uat_to_prod.bat";

 

View solution in original post

6 Replies
Ron1
Partner - Creator
Partner - Creator
Author

Can anyone help on this? This is a bit challenging to achieve

Taoufiq_Zarra

@Ron1  .... to another(exact same folder structure) ....

the target folder and subfolder are already created ?

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Ron1
Partner - Creator
Partner - Creator
Author

@Taoufiq_Zarra 

Yes it is already created in Target location.

Kushal_Chawda

@Ron1  try below

Assumptions:

1) All the folders and sub folders name are identical in root folder of source and target (Consider Uat & Prod)

2) You may need to run Batch file to do this, so assuming the windows users account should have access to run the batch file (file with copy commands)

3) Machine on which Batch file placed, assuming user account running the Batch should have read/write access to file and folders on both UAT and Prod folders.

4) Backup folder is not under the Prod or UAT root folder. It should be separate file server. Id it is separate file server it should be accessible from both Prod & UAT.(Mainly from the server where Batch file will run)

You may need to run the Batch commands from QlikView using execute statements for which you need to do some settings in QlikView file and settings in server. Look at the below path for more details

https://help.qlik.com/en-US/qlikview/April2020/Subsystems/Client/Content/QV_QlikView/Scripting/Scrip...

let vUATQvdFolderRoot = 'C:\Work\Qlik\View\UAT'; // UAT root folder path

let vProdQvdFolderRoot = 'C:\Work\Qlik\View\PROD'; // Prod Root folder path

let vBatchFileLocation = 'C:\Work\Qlik\View\UAT\Batch'; // Batch file placement path

let vBackupFolderPath = 'C:\Work\Qlik\View\Backup'; // Backup root folder path

sub ScanFolder(Root)

          for each FileExtension in 'qvd'

                    for each FoundFile in filelist( Root & '\*.' & FileExtension)
                             // Capture file location and file last update time
                              FileList:
                              LOAD '$(FoundFile)' as SourceFile,
                                   filetime('$(FoundFile)') as FileTime

                              AutoGenerate 1;

                    next FoundFile

          next FileExtension

          for each SubDirectory in dirlist( Root & '\*' )

                    call ScanFolder(SubDirectory)

          next SubDirectory

end sub

Call ScanFolder('$(vUATQvdFolderRoot)') ; // Scan all UAT folders and sub folders inside root folder

Uat_Prod_qvd_path:
LOAD SourceFile as Uat_qvd_path,
     FileTime as Uat_qvd_filetime,
     '$(vProdQvdFolderRoot)'&TextBetween(SourceFile,'$(vUATQvdFolderRoot)','') as Prod_qvd_path // convert UAT folder path to Prod folder path
Resident FileList
where floor(FileTime)=floor(today());

DROP Table FileList;

Call ScanFolder('$(vProdQvdFolderRoot)') ; // Scan all Prod folders and sub folders inside root folder

// inner join used to check common qvds between uat and prod so that we can take backup of that on prod
find_existing_qvd_prod1:
LOAD Distinct mid(Uat_qvd_path,Index(Uat_qvd_path,'\',-1)+1,Index(Uat_qvd_path,'.',-1)-Index(Uat_qvd_path,'\',-1)-1) as QVD  // UAT Qvd names
Resident Uat_Prod_qvd_path;

Inner Join(find_existing_qvd_prod1)
LOAD Distinct SourceFile as Prod_qvd_path,
      mid(SourceFile,Index(SourceFile,'\',-1)+1,Index(SourceFile,'.',-1)-Index(SourceFile,'\',-1)-1) as QVD, // Prod QVD names
      mid(SourceFile,1,Index(SourceFile,'.',-1)-1)&'_'& date(Today(),'YYYYMMDD')
      & mid(SourceFile,Index(SourceFile,'.',-1),4) as Prod_qvd_name_with_current_time // Prod QVD names with current time appended for backup
Resident FileList;

DROP Table FileList;

find_existing_qvd_prod:
LOAD Prod_qvd_path,
     '$(vBackupFolderPath)'&TextBetween(Prod_qvd_name_with_current_time,'$(vProdQvdFolderRoot)','') as Prod_backup_qvd_path
Resident find_existing_qvd_prod1;

DROP Table find_existing_qvd_prod1;

// Create windows batch command to copy qvds from uat to prod folders
Copy_qvd_uat_to_prod:
LOAD 'xcopy /y '&Uat_qvd_path&' '&Prod_qvd_path&'*' as Copy_Uat_to_Prod_Commands
Resident Uat_Prod_qvd_path;

DROP Table Uat_Prod_qvd_path;

// create batch file to run commands
STORE Copy_qvd_uat_to_prod into $(vBatchFileLocation)\Copy_qvd_uat_to_prod.bat(txt) ;

DROP Table Copy_qvd_uat_to_prod;

// Create windows batch command to copy qvds from prod to backup folders
Backup_existing_prod_qvd:
LOAD 'xcopy /y '&Prod_qvd_path&' '&Prod_backup_qvd_path&'*' as Copy_to_Backup_Commands
Resident find_existing_qvd_prod;

DROP Table find_existing_qvd_prod;

// create batch file to run commands
STORE Backup_existing_prod_qvd into $(vBatchFileLocation)\Backup_existing_prod_qvd.bat(txt) ;

DROP Table Backup_existing_prod_qvd;


Execute cmd.exe /C "$(vBatchFileLocation)\Backup_existing_prod_qvd.bat"; // First run backup batch file

Execute cmd.exe /C "$(vBatchFileLocation)\Copy_qvd_uat_to_prod.bat";

 

Ron1
Partner - Creator
Partner - Creator
Author

Great.. It works.. 

Thank you soo much

Kushal_Chawda

Glad that it worked