7 Replies Latest reply: Feb 13, 2015 10:30 AM by Srikanth P RSS

    Writing .bat file through script

    Lennaert van den Brink

      Hello all,

       

      I am trying to write a qlikview script that (amongst other things) moves a group of files from one place to another. The general idea is that in the script I generate lines of cmd commands (MOVE [from] [to]) in a table. Then I want to export this table using:

       

      Store [::CMD] FROM cmdtable INTO .\batchfile.bat (txt);

       

      However, the problem is that my [from] and [to] filepaths contain white spaces. In cmd script this is solved by surrounding the path with double quotes:

       

      MOVE "C:/Some Dir" "C:/Other Dir"

       

      However, when I write a line like above to txt using the store command, the output looks like this:

       

      "MOVE ""C:/Some Dir"" ""C:/Other Dir"""

       

      Anybody has an idea how I can force qlikview to write to file without escaping? Any help is appreciated!

       

      Regards, Lennaert van den Brink

        • Re: Writing .bat file through script
          Srikanth P

          Hello, According to your script the cmdtable have [::CMD] derivied FROM & TO fields. use the single quotes to generate the respective cmd command. Please find the below sample script.

           

          cmdtable:

          LOAD

            * ,

            'MOVE "'&FROM&'" "'&TO&'"' AS [::CMD]

          ;

          LOAD * INLINE [

          FROM , TO

          C:\ , C:\Other

          D:\Other Than , C:\New Than

          ];

          • Re: Writing .bat file through script
            Massimo Grossi

            some help here

            http://community.qlik.com/message/493224#493224

            look at last answer, Qlik Tips blog

            • Re: Writing .bat file through script
              Lennaert van den Brink

              To clarify my question, I have a table like this in qlikview:

               

              BATWriteToFile:

              MOVE "C:\From 1\file1.txt" "C:\To 1\file1.txt"

              MOVE "C:\From 2\file2.txt" "C:\To 2\file2.txt"

              MOVE "C:\From 3\file3.txt" "C:\To 3\file3.txt"

               

              So I know how to write the lines in such a manner that the QV table contains the correct commands. However, the problem occurs when I call

               

              STORE [::CMD] FROM BATwriteToFile INTO .\cleanup.bat (txt);

               

              Because this results in the following text in the cleanup.bat file:

               

              ::CMD

              "MOVE ""C:\From 1\file1.txt"" ""C:\To 1\file1.txt"""

              "MOVE ""C:\From 2\file2.txt"" ""C:\To 2\file2.txt"""

              "MOVE ""C:\From 3\file3.txt"" ""C:\To 3\file3.txt"""


              So the STORE INTO command automatically escapes all the qoutes and als escapes the entire row. I want to just write the plain string without the automatic escaping, so my question is: how do I make the STORE INTO command stop escaping the output?

               

              The linked qlik tips blog is almost the exact solution I have right now, but that code does not work when your file paths contain blank spaces or other characters that need to be escaped...

              • Re: Writing .bat file through script
                Simen Kind Gulbrandsen

                use [C:\Path With Spaces\Batchfile.bat](txt)

                • Re: Writing .bat file through script
                  Lennaert van den Brink

                  None of the above solutions is what I am looking for, but I believe I have a solution myself.

                   

                  The idea is now that I construct each move command in a table with 3 columns:

                   

                  BATWriteToFile:

                  [::cmd1], cmd2, cmd3

                  'MOVE', '"C:\From 1\file1.txt"', '"C:\To 1\file1.txt"'

                  'MOVE', '"C:\From 2\file1.txt"', '"C:\To 2\file1.txt"'

                  'MOVE', '"C:\From 3\file1.txt"', '"C:\To 3\file1.txt"'


                  Then when storing the command, I write to text file using spaces as delimiter. The result:

                  ::cmd1 cmd2 cmd3

                  MOVE """C:\From 1\file1.txt""" """C:\To 1\file1.txt""""

                  MOVE """C:\From 2\file2.txt""" """C:\To 2\file2.txt""""

                  MOVE """C:\From 3\file3.txt""" """C:\To 3\file3.txt""""

                   

                  So the filenames are surrounded by triple ". However, "" is ignored by cmd, so effectively """ is the same as " for cmd.exe

                   

                  I can not test this currently on my live environment, but I believe this might do the trick...

                    • Re: Writing .bat file through script
                      Srikanth P

                      I got your problem exactly. Instead of doing tripe double quotes ("), while store the file use the " as delimiter. Please find the below script.

                       

                      First I am constructing the Table with all the fields can create a move command.

                       

                      cmdtable:

                      LOAD

                        'MOVE ' AS [::Cmd] ,

                        FROM AS Cmd1 ,

                        ' ' AS Cmd2 ,

                        TO AS Cmd3 ,

                        ' ' AS Cmd4

                      ;

                      LOAD * INLINE [

                      FROM , TO

                      C:\ , C:\Other

                      D:\Other Than , C:\New Than

                      ];

                       

                      STORE cmdtable into [batchfile.bat](txt,delimiter is '"') ;

                       

                      While storing I am using double quote as delimiter. The file creating required below output.

                       

                      ::Cmd"Cmd2"Cmd3"Cmd4"Cmd5

                      MOVE "C:\" "C:\Other"

                      MOVE "D:\Other Than" "C:\New Than"

                       

                      Hope this will help you ...