none
How to use FOR command to deal with NULL in batch script? RRS feed

  • Question

  • I extracted a piece of text with quickbms and wrote it into a .txt file. It contains several file names seperated by NULL (viz. "\0"):

    VO_E1_S05_CHLOE_004_M VO_E1_S05_CHLOE_007_M VO_E1_S05_CHLOE_008_M                          

    My aim is to save these file names by line.

    At frist, I thought the gaps between these file names are SPACE (viz. "\x20"), so the batch script as below is used:

    @echo off&setlocal enabledelayedexpansion
    for /f "delims=" %%a in (output\filenamelist.txt) do (
        set "foo=%%a"
        for %%b in ("!foo: =" "!") do echo,%%~b
        )>"output\filenamelist.tx_"
    move "output\filenamelist.tx_" "output\filenamelist.txt"



    The script above can replace space with line feed perfectly. However, it's actually null instead of space. When this program was run, only "VO_E1_S05_CHLOE_004_M" could be identified.
    My question is:
    1. Is there any chance FOR command can deal with NULL?
    2. Is there any symbol of NULL that can serve in the line below?
    for %%b in ("!foo:<symbol of NULL>=" "!") do echo,%%~b
    Thanks.
    Note: vbs program can open and save the file in question with notpad, where NULL can be turned into SPACE. However, it's a devious solution.


    • Edited by sprhere Sunday, March 24, 2019 12:19 PM
    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:24 PM Abandoned
    Sunday, March 24, 2019 12:16 PM

All replies

  • Why don't you use something other than NULL as the delimiter?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Sunday, March 24, 2019 2:13 PM
  • BOfH_666:

    Thank you for your attention.

    It's the original file that uses NULL as delimiter. What I'm trying to do is just to get ride of it.



    • Edited by sprhere Sunday, March 24, 2019 3:02 PM
    Sunday, March 24, 2019 3:01 PM
  • Files with nulls are usually exports from a mainframe.  The nulls are not delimiters,  they are field terminators.  They cannot be used as delimiers in a CSV.

    With PowerShell these can be replaced.

    (Get-Content-Raw) -replace '\x00','|'


    \_(ツ)_/

    Sunday, March 24, 2019 5:38 PM
  • jrv:

    Thanks for you help. I made it with PowerShell. It's a totally new language to me though.

    Your script is developed by me as below:

    (Get-Content "output\filenamelist.txt") | Foreach-Object {$_ -replace "`0","`r`n"} | Out-File "output\filenamelist.txt" -encoding ascii

    Tuesday, March 26, 2019 12:40 PM