2013-02-21 21 views
1

好的,我有数百个文件中包含一个文本标题,其中包含一个或多个8个不同变量(名称,主机,年份,月份,小时,分钟,秒)。显示值的第一时间,它看起来像:批处理文件提取每个文件中变量名称变化时的值

@name 4 10 
3 4 DHARLAN 

此后每次(和可变集可以出现每​​个文件1至100倍)它显示只有为:

3 4 DHARLAN 

的问题出现在什么恰好是一个4在这种情况下可以是1 & 99之间的任意值,所以在接下来的文件时,它可能是:

3 15 DHARLAN 

所以,真正的eavh变量耳鼻喉科里斯是这样的:

3 ## <value> 

其中##是较早在标题中确定:

@name ## X 

我不明白enoyugh如何FOR/F TOKENS工作得到任何接近。

What I need is to parse a directory and end up with a file something like: 
<filenameA> <name1> <host1> <year1> <month1> <day1> <hour1> <minute1> <second1> 
<filenameA> <name2> <host2> <year2> <month2> <day2> <hour2> <minute2> <second2> 
<filenameB> <name1> <host1> <year1> <month1> <day1> <hour1> <minute1> <second1> 
... 

我有什么至今:

FOR /f "tokens=*" %%i IN ('dir /a-d /s /b') DO call :findfile "%%i" 

:findfile 
REM Print Filename 
FINDSTR /B /M "#UGC">>output.txt 
REM set Name Variable, need 2nd word (7th & if exists (8th) character) from this line only 
FINDSTR /B "@name">%%n 
REM Find all lines with name variable 
FINDSTR /B "3 %n%">>output.txt 

帮助是极大的赞赏,甚至建议一个程序,可以做到这一点。

+0

我认为这将有助于我们知道您的标题中的值是什么意思。标题如何表示名称,主机,年,月,日,小时,分钟和秒钟?是@name的名字还是DHARLAN的名字? – rojo 2013-02-21 19:20:00

+0

是的,它们是用户名,年,月,日等的变量。 – Locker 2013-02-27 23:20:59

回答

0

想法

所以...这就是我如何阅读你的问题。你有许多值很多的文件,并且你想要的每个文件都输出一行所有文件的变量。每个变量由两行组成。 1.变量声明(名称)和2.变量值(DHARLAN)。这些行由一个数字(1到99)关联。它是否正确?

这应该让你开始...

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 

<nul set /p "=">output.txt 
for /f "delims=" %%F in ('dir /a:-d /b /s') do if exist "%%~fF" (
    set "name=" 
    set "namenum=" 
    set "host=" 
    set "hostnum=" 
    set "year=" 
    set "yearnum=" 
    set "month=" 
    set "day=" 
    set "daynum=" 
    set "monthnum=" 
    set "hour=" 
    set "hournum=" 
    set "minute=" 
    set "minutenum=" 
    set "second=" 
    set "secondnum=" 
    set "count=0" 
    for /f "usebackq delims=" %%L in ("%%~fF") do (
     for /f "tokens=1,2,3*" %%X in ("%%L") do (
      if "%%Y"=="!namenum!" set "name=%%Z" & set /a "count+=1" 
      if "%%Y"=="!hostnum!" set "host=%%Z" & set /a "count+=2" 
      if "%%Y"=="!yearnum!" set "year=%%Z" & set /a "count+=4" 
      if "%%Y"=="!monthnum!" set "month=%%Z" & set /a "count+=8" 
      if "%%Y"=="!daynum!" set "day=%%Z" & set /a "count+=16" 
      if "%%Y"=="!hournum!" set "hour=%%Z" & set /a "count+=32" 
      if "%%Y"=="!minutenum!" set "minute=%%Z" & set /a "count+=64" 
      if "%%Y"=="!secondnum!" set "second=%%Z" & set /a "count+=128" 
      if /i "%%X"=="@name" set "namenum=%%Y" 
      if /i "%%X"=="@host" set "hostnum=%%Y" 
      if /i "%%X"=="@year" set "yearnum=%%Y" 
      if /i "%%X"=="@month" set "monthnum=%%Y" 
      if /i "%%X"=="@day" set "daynum=%%Y" 
      if /i "%%X"=="@hour" set "hournum=%%Y" 
      if /i "%%X"=="@minute" set "minutenum=%%Y" 
      if /i "%%X"=="@second" set "secondnum=%%Y" 
      if "!count!" equ "255" (
       echo %%~nxF !name! !host! !year! !month! !day! !hour! !minute! !second!>>output.txt 
       set "name=" 
       set "namenum=" 
       set "host=" 
       set "hostnum=" 
       set "year=" 
       set "yearnum=" 
       set "month=" 
       set "day=" 
       set "daynum=" 
       set "monthnum=" 
       set "hour=" 
       set "hournum=" 
       set "minute=" 
       set "minutenum=" 
       set "second=" 
       set "secondnum=" 
       set "count=0" 
      ) 
     ) 
    ) 
) 

goto :eof 

:End 
endlocal 

更新

这里是脚本的更新版本以上评论。它还应该使用设置部分和cd命令来解决您的注释。

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 

:: Setup 
set "ResultsFolder=results" 
set "ExportFile=output.txt" 

:: Set the Working Directory 
cd data 

:: Verify that the Results folder exists 
if not exist "%ResultsFolder%\*" md "%ResultsFolder%" 
set "OutputFile=%ResultsFolder%\%ExportFile%" 

:: Empty the results file. 
<nul set /p "=">%OutputFile% 

:: Loop through the files /a:-d in the directory and its subdirectories /s. 
for /f "delims=" %%F in ('dir /a:-d /b /s') do if exist "%%~fF" (
    call :Reset 
    rem Loop through the file contents and parse each line. 
    for /f "usebackq delims=" %%L in ("%%~fF") do (
     for /f "tokens=1,2,3*" %%X in ("%%L") do (
      rem Keep track of the variables. 
      if "%%Y"=="!namenum!" set "name=%%Z" & set /a "count+=1" 
      if "%%Y"=="!hostnum!" set "host=%%Z" & set /a "count+=2" 
      if "%%Y"=="!yearnum!" set "year=%%Z" & set /a "count+=4" 
      if "%%Y"=="!monthnum!" set "month=%%Z" & set /a "count+=8" 
      if "%%Y"=="!daynum!" set "day=%%Z" & set /a "count+=16" 
      if "%%Y"=="!hournum!" set "hour=%%Z" & set /a "count+=32" 
      if "%%Y"=="!minutenum!" set "minute=%%Z" & set /a "count+=64" 
      if "%%Y"=="!secondnum!" set "second=%%Z" & set /a "count+=128" 
      if /i "%%X"=="@name" set "namenum=%%Y" 
      if /i "%%X"=="@host" set "hostnum=%%Y" 
      if /i "%%X"=="@year" set "yearnum=%%Y" 
      if /i "%%X"=="@month" set "monthnum=%%Y" 
      if /i "%%X"=="@day" set "daynum=%%Y" 
      if /i "%%X"=="@hour" set "hournum=%%Y" 
      if /i "%%X"=="@minute" set "minutenum=%%Y" 
      if /i "%%X"=="@second" set "secondnum=%%Y" 
      rem When a full set is reached print it out. 
      if "!count!" equ "255" (
       echo %%~nxF !name! !host! !year! !month! !day! !hour! !minute! !second!>>%OutputFile% 
       call :Reset 
      ) 
      if "!count!" gtr "255" (
       echo %%~nxF: Incomplete Set Encountered. Stopping parsing of this file, continuing to the next. 
       rem You can also use other validations to identify incomplete sets. 
      ) 
     ) 
    ) 
) 

goto :eof 

:: Reset all of the variables for the next set. 
:Reset 
set "name=" 
set "namenum=" 
set "host=" 
set "hostnum=" 
set "year=" 
set "yearnum=" 
set "day=" 
set "daynum=" 
set "month=" 
set "monthnum=" 
set "hour=" 
set "hournum=" 
set "minute=" 
set "minutenum=" 
set "second=" 
set "secondnum=" 
set "count=0" 
goto :eof 

:End 
endlocal 

摘要

这是一个什么样的剧本在做什么,它是如何工作的总结。

  1. Main for循环将遍历当前工作目录内的所有文件。 dir /a:-d /b /s
  2. 在这个循环中,我们首先设置跟踪文件变量所需的变量。 :Reset
  3. next for循环将打开文件并读取文件的每一行。%%L
  4. 第三个for循环将解析相关变量信息的行。从行(1,2,*)中检索第一个,第二个和所有剩余的标记为变量(%% X,%% Y,%% Z)
  5. 现在执行逻辑以跟踪变量集。如果已设置变量号并且该行与变量号匹配,则保存变量值。否则,如果变量名称匹配,请检索变量编号以便在以下行中进行比较。
  6. 同时增加设置的计数数量,并且当发现完整集合时,用文件名将它写入输出文件。
  7. 重置变量以供下一组使用。
  8. 如果计数不等于255,则文件格式不正确或具有不完整的一组变量。

这个脚本正是似乎是提出的问题的最佳解决方案,其中所提供的信息。我必须对正在处理的文件做出假设,因此可能需要进行一些修改。如果您想向我们展示要解析的文件的完整示例,它将有助于确定要执行的操作。

+0

这是一个很好的帮助,谢谢。还不完全: 1)想要从不同的目录中读取文件。 ('dir。\ data/a:-d/b')中将/ f“delims =”%% F改为''代码''如果存在“%%〜fF”( 但这不起作用 2)回声重定向不起作用。只响应活动文件,而不响应任何变量。回应到屏幕的作品,而不是重定向到一个文件。当前行是:'code'echo %%〜nF!name! !主办! !年! !月! !天! !小时! !分钟! !秒%!>>%RESULTSFOLDER%\%EXPORTFILE% 3)只抓取第一组变量 – Locker 2013-02-28 23:49:31

+0

您可否详细说明1号不能如何工作。脚本是否崩溃或无法找到文件夹?另外'%RESULTSFOLDER%\%EXPORTFILE%'的值是多少?请注意,为了创建输出文件,必须存在'%RESULTSFOLDER%'。 – 2013-03-01 02:19:13

+0

大卫,感谢您的帮助,对不起,我一直无法工作。我回来了,很快就会找到答案,但不确定“1号”的含义。 TIA – Locker 2013-03-21 20:01:36

相关问题