Skip to main content
Announcements
NEW: Seamless Public Data Sharing with Qlik's New Anonymous Access Capability: TELL ME MORE!
cancel
Showing results for 
Search instead for 
Did you mean: 
Chewy
Contributor
Contributor

Delete all files except one specific

Hello together,

I am looking for a code which deletes all files in a directory except one.
Unfortunately I have not found anything regarding an exception in a statement, so I am asking for help here.

My current code looks like this.

if ScriptErrorCount=0 then

       execute cmd.exe /C del ..\Path\to\file\*.csv;


Here I want to exclude a specific file that is in the same directory.



 

Labels (3)
1 Solution

Accepted Solutions
marcus_sommer

Here some examples which I use since ages:

EXECUTE cmd /C "X:\QV_Production\DataSynchronisationPlandaten.bat";

EXECUTE schtasks.exe /Run /TN "QlikView\PrintAndMail";

The last one using the windows task-planner has benefits if you want to run the batches with another user and/or with a higher privilege and/or repeating the task n times or ...

Currently I have no PowerShell which I run in this way but within the community should be also multiple examples.

View solution in original post

8 Replies
marcus_sommer

I think it's not directly possible with del-command, see:

del | Microsoft Learn

So you may look for other tools like xcopy or robocopy or similar if the provide the needed parameters.

An alternatively might be to move this file at first to a tmp-folder, performing then the delete-action and reversing the move back. It might be further useful to add sleep-statements between the multiple statements to give the OS and the network/storage enough time for the execution.

Chewy
Contributor
Contributor
Author

Thanks for the link, I had already seen that as well.
Only I have roughly in memory that it goes with do and loops anyway.


If not everything deceives me something in this direction:

 

execute cmd.exe /C for %I in ("..\Path\to\file\*.csv") do if /I not "%~nxI"=="test123.csv" del "%~I"

marcus_sommer

I'm not sure if you could apply more complex logic which may also include several statements and/or line-breaks within an EXECUTION statement and if the syntax could become quite tricky in regard to quote another quoted parts. Therefore I think it would be more expedient if you don't execute the code directly else starting any batch or task which does the job.

Chewy
Contributor
Contributor
Author

Thank you 😃

What is the best way to call a batch or PowerShell file within Qlikview?

 

Can i write:

 

if ScriptErrorCount=0 then

       execute cmd.exe /C "C:\Path\to\batch\test.bat";

marcus_sommer

Here some examples which I use since ages:

EXECUTE cmd /C "X:\QV_Production\DataSynchronisationPlandaten.bat";

EXECUTE schtasks.exe /Run /TN "QlikView\PrintAndMail";

The last one using the windows task-planner has benefits if you want to run the batches with another user and/or with a higher privilege and/or repeating the task n times or ...

Currently I have no PowerShell which I run in this way but within the community should be also multiple examples.

Chewy
Contributor
Contributor
Author

Is the Execute command also possible for network paths?

I have just executed my script in Qlikview, which also finished without errors, but the batch file was not executed.

Is there anything to consider here?

Currently my execute command looks like this:
Execute cmd.exe /C "\\Network\Path\to\file\test.bat";

Chewy
Contributor
Contributor
Author

Or can i only do this like here:

// Set the network path to the batch file
let vBatchFile = '\\server\folder\test.bat';

// Execute the batch file using the CMD shell
EXECUTE cmd.exe /c "$(vBatchFile)";

 

If i would have multiple batch files i can just add a "vBatchFile2" and another execute.

Is that the better option here?

marcus_sommer

I'm not absolutely sure but I think cmd doesn't support unc-paths. A workaround might be to call a local batch which starts another batch which used other tools which are then performing the final tasks. In the old days I used such logic to execute the final batches with another user or if they had to consider extra parameters which couldn't directly applied. Also the above provided detour by starting a windows task might be helpful for such things.

Nevertheless you may try it with PowerShell by following the hints from:

EXECUTE PowerShell with Administrator Rights - Qlik Community - 960128