Search code examples
filegnuplotwhitespacegnuplot-iostream

gnuplot: spaces in file path


How correctly you can use filenames with spaces?

My code:

files = system("dir /b \"d:\\data\\my data\\*.dat\"")

do for [name in files]{

    inputPath = "d:/data/my data/".name
    outputPath = "d:/data/".name.".png"

    set output outputPath
    plot inputPath using 1:3 with lines ls 1 notitle
}

If there are spaces in the file name, the script does not work correctly. For example:

d:/data/my data/data1.csv - all correct

d:/data/my data/data 2.csv - error, 0 size file "data.png" is created and the graph is not created

How to solve this problem?


Solution

  • Basically, you need to replace "\n" with space and put your filenames into quotes ''. The following code might be one way to do this. By the way, your code would generate output names like "Data1.dat.png" and not "Data1.png" from "Data1.csv". Also be aware of the difference of single and double quotes.

    ### File list with space in filenames (Windows)
    reset session
    
    InputPath = 'D:\data\my data\'
    OutputPath = 'D:\data\'
    SearchExp = 'dir /b "' . InputPath . '*.dat"'
    # print SearchExp
    LIST = system(SearchExp)
    # print LIST
    
    LIST = LIST eq "" ? LIST : "'".LIST."'"  # add ' at begining and end
    FILES = ""
    do for [i=1:strlen(LIST)] {
        FILES = (LIST[i:i] eq "\n") ? FILES."' '" : FILES.LIST[i:i]
    }
    # print FILES
    print sprintf("The list contains %d files", words(FILES))
    
    do for [FILE in FILES] {
        InputFile = InputPath.FILE
        OutputFile = OutputPath.FILE[1:strlen(FILE)-4].".png"
        print InputFile
        print OutputFile
        # or plot your files 
    }
    ### end of code