开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜