1

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?

Zhihar
  • 1,306
  • 1
  • 22
  • 45
  • Did you try to go through all indices instead of filenames? – Bracula Jan 19 '19 at 14:31
  • Vladimir, I probably gave a bad example. In the folder there can be files with different names: "my data.dat", "2015 year statistics.dat" etc. Not indices. And the problem is that Gnuplot cannot correctly open files containing spaces in names, and correctly create files containing problems in names. So I ask - can I solve this problem. Извиняюсь за мой английский - в папке могут содержаться файлы, содержащие в именах проблемы (например состоящие из нескольких слов) и gnuplot дает сбой, когда пытается считать из таких файлов данные или когда пытается создать файлы с такими именами :( – Zhihar Jan 19 '19 at 15:52
  • 2
    не волнуйся, твой английский вполне ок. this is awkward because I tried to open files with spaces in names and gnuplot worked just fine. However, it can be a platform-specific behaviour since I'm a Linux user. P.S. do not forget to mark you problem as answered. – Bracula Jan 20 '19 at 12:23

2 Answers2

2

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
theozh
  • 22,244
  • 5
  • 28
  • 72
1

Found a problem:

the command do for [name in files] splits the list of filenames into words (space as a separator), and not into lines (\r\n as a separator)

Therefore, it is necessary to select strings from the list, not words.

Zhihar
  • 1,306
  • 1
  • 22
  • 45