gpt4 book ai didi

Windows批处理文件将空白字段更改为值

转载 作者:可可西里 更新时间:2023-11-01 11:12:54 24 4
gpt4 key购买 nike

我正在编写一个 Windows 脚本来读取一个包含 9 列数据的 txt 文件我想将此数据写入一个 csv 文件以加载到数据库中。

我发现有时有一栏是空白的,所以我想输入代码“rdp”另一列主要是数字,但出于某种原因,某些值是“。” (大概表示值小于 1。我想将这些值更改为“0”

我的代码

  for /F %%b in (c:\ts_users\newfiles_list.txt) do (
for /F "tokens=1,2,3,4,5,6,7,8,9" %%i in (%%b) do (
echo %%i,%%j,%%k,%%l,%%m,!idle!,%%o %%p,%%q >>%%~nb.csv
)
)

这设法读入 txt 文件,然后将其写为 csv。k 列有时为空,n 列有时有一个“.”

我试过各种变体

    set var=rdp
if %%k="" then set !k:=!var!

这不起作用,所以我有点难过(在互联网上搜索了几天之后)

当前输入

201401241611 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME eu1ptsw002  
201401241611 julie.noble rdp-tcp#9 3 Active 18 24/01/2014 14:24 eu1ptsw002
201401241611 svc.perfmon18 7 Disc 3 24/01/2014 14:15 eu1ptsw002
201401241611 reto.hofstetter 10 Disc 40 24/01/2014 10:57 eu1ptsw002
201401241611 lester.valentin 14 Disc 1:16 24/01/2014 11:53 eu1ptsw002
201401241611 philippe.bachmann 15 Disc 2 24/01/2014 12:45 eu1ptsw002
201401241611 patrik.soderlund rdp-tcp#2 21 Active 24 24/01/2014 07:42 eu1ptsw002

电流输出

201401241611,USERNAME,SESSIONNAME,ID,STATE,,TIME LOGON,TIME 
201401241611,julie.noble,rdp-tcp#9,3,Active,,24/01/2014 14:24,eu1ptsw002
201401241611,svc.perfmon18,7,Disc,3,,14:15 eu1ptsw002,
201401241611,reto.hofstetter,10,Disc,40,,10:57 eu1ptsw002,
201401241611,lester.valentin,14,Disc,1:16,,11:53 eu1ptsw002,
201401241611,philippe.bachmann,15,Disc,2,,12:45 eu1ptsw002,
201401241611,patrik.soderlund,rdp-tcp#2,21,Active,,24/01/2014 07:42,eu1ptsw002

要求的输出

201401241611,USERNAME,SESSIONNAME,ID,STATE,,TIME LOGON,TIME 
201401241611,julie.noble,rdp-tcp#9,3,Active,18,24/01/2014 14:24,eu1ptsw002
201401241611,svc.perfmon18,rdp,7,Disc,3,24/01/2014 14:15,eu1ptsw002
201401241611,reto.hofstetter,rdp,10,Disc,40,24/01/2014 10:57,eu1ptsw002,
201401241611,lester.valentin,rdp,14,Disc,1:16,24/01/2014 11:53,eu1ptsw002
201401241611,philippe.bachmann,rdp,15,Disc,2,24/01/2014 12:45,eu1ptsw002
201401241611,patrik.soderlund,rdp-tcp#2,21,Active,24,24/01/2014 07:42,eu1ptsw002

最佳答案

@echo off &setlocal disableDelayedExpansion
for /f "delims=" %%a in (file) do (
set "line=%%~a"
setlocal enabledelayedexpansion
set "line=!line:,.,=,0,!"
set "line=!line:,,=,rdp,!"
echo(!line!
endlocal
)

关于Windows批处理文件将空白字段更改为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21334589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com