Missing characters when creating .bat file in Julia

Hi everyone!

I have a problem with the file system.

I want to create a .bat file in order to delete all the .png files in the folder.

the .bat file should be:

@echo off
Del %cd%\\*.png
echo delete complete!
pause

while the julia create a .bat file with a missing character “/”

@echo off
Del %cd%\*.png  
echo delete complete!
pause

my code is,

function delete_bat()
io = open("delete_png.bat","w")
write(io,"@echo off
Del %cd%\\*.png
echo delete complete!
pause
")
close(io)
end
delete_bat()

Could you please help me?

Backspaces need to be escaped:

julia> println("@echo off
       Del %cd%\\*.png
       echo delete complete!
       pause
       ")
@echo off
Del %cd%\*.png
echo delete complete!
pause


julia> println("@echo off
       Del %cd%\\\\*.png
       echo delete complete!
       pause
       ")
@echo off
Del %cd%\\*.png
echo delete complete!
pause
2 Likes

Backslash (\) is the ‘escape character’ used to skip control sequences encountered in strings. In this case, you’re escaping another backslash, so you can either use four consecutive backslashes (\\\\) as mentioned above, or a raw string that ignores control sequences and escapes (raw"%cd%\\.png").

4 Likes

Instead of a batch file, you could do it directly from Julia. This would have the advantage that it would also work on Linux or a Mac:

using Glob
function delete_files(;ext="png", directory=pwd())
    ndeleted = 0
    cd(directory) do
        files = glob("*." * ext)
        foreach(rm, files)
        ndeleted = length(files)
        println(ndeleted, " .", ext, " files deleted from ", directory)
    end
    return ndeleted
end
6 Likes

If you just want the string to be written out directly to the file, and don’t need special escape sequences (\n, \t, etc.) or Julia variables inside the string, then you can just add raw at the beginning of the string:

function delete_bat()
io = open("delete_png.bat","w")
write(io, raw"@echo off
Del %cd%\\*.png
echo delete complete!
pause
")
close(io)
end
delete_bat()

Within raw strings, Julia does no special interpretation; what you see is what you get.

3 Likes

Really thank you guys! my problem solved.
but why it would be // when println("//")? Is there anything special?

Roughly it goes like this. A string is something that starts and ends with quotation marks. Now suppose you want to have a string which contains quotation marks, say """", would that be one string containing two quotation marks or two empty strings? There are some possible ways to resolve this but a common approach is to introduce an “escape” character, commonly backslash, meaning that a quotation mark following the escape character is a quotation mark within the string and not a string delimiter. Thus a string containing two quotation marks is encoded as "\"\"". But how would you now write a string containing a backslash? If you try "\" the second quotation mark is an escaped quotation mark and not the end of the string. The solution is to let the escape character escape itself, so "\\" means a string containing a single backslash. Then when we have an escape convention it’s natural to introduce more escape codes for useful things, like "\n" meaning a newline.

1 Like

Because in Julia the escape character is \ and not / :slight_smile:

2 Likes

my mistake :sweat_smile:… thanks