dir /b command output in one line with DELIMITER added Batch file programming
I wan开发者_StackOverflow社区t dir /b command line output in one line separated by a delimiter for example Outout of
dir c:\test
file1
file2
file3
file4
I want it as
file1;file2;file3;file4
How to do this in batch programming.
@echo off
setlocal enabledelayedexpansion enableextensions
set LIST=
for %%x in (*) do set LIST=!LIST!;%%x
echo %LIST:~1%
This won't quote file names that happen to contain a ;
, though. The following does, though:
@echo off
setlocal enabledelayedexpansion enableextensions
set LIST=
for %%x in (*) do (
echo %%x|findstr ";">nul 2>&1
if errorlevel 1 (set LIST=!LIST!;%%x) else (set LIST=!LIST!;"%%x")
)
echo %LIST:~1%
Maybe pipe it through a python script such as:
import sys
print ';'.join(sys.stdin.read().split())
In your batch file:
dir /b | thatscriptjustabovehere.py
精彩评论