首页 > 解决方案 > 循环遍历每一行 - 尝试过 PS 和 CMD

问题描述

美好的一天,我的问题是我的脚本只在每一轮解析我的列表的第一行,有问题的程序没有批处理支持,所以它需要从每一行开始。如果有人能解释我做错了什么,那就太好了

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1

FOR /F "tokens=* USEBACKQ" %%F IN (`cd /d J:\temp\2 ^& dir /p /b /s`) DO (
SET var=%%F
)
ECHO %var%

IF not exist "J:\temp\1\%var:~3%" (mkdir "J:\temp\1\%var:~3%")
RMDIR /s /q "J:\temp\1\%var:~3%\"

FOR /F "tokens=* USEBACKQ" %%A in ( %var% ) DO (
SET count+=1
J:\temp\windows\comp.exe -o"J:\temp\1\%var:~3%" -cn -d10 -intense -brute -s125 -t-j "%var%" )

这也行不通

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
set /a count = 1

FOR /F "tokens=* USEBACKQ" %%F IN (`cd /d J:\temp\2 ^& dir /p /b /s`) DO (
SET var=%%F
)
ECHO %var%

IF not exist "J:\temp\1\%var:~3%" (mkdir "J:\temp\1\%var:~3%")
rmdir /s /q "J:\temp\1\%var:~3%\"

for /F "tokens=* USEBACKQ" %%A in ( %var% ) do (
SET /a count+=1
echo !count!
J:\temp\windows\precomp.exe -o"J:\temp\1\%var:~3%" -cn -d10 -intense -brute -s125 -t-j "%var%" )

尝试过Powershell,小得多但没有运气。powershell 认为路径会很长,但没有

$file = (Get-ChildItem "J:\temp\2" -Recurse -Force -ErrorAction SilentlyContinue ).fullname
$fout = (Get-ChildItem "J:\temp\2" -Recurse -Force -ErrorAction SilentlyContinue ).fullname.Substring(10)

Robocopy "J:\temp\2" "J:\temp\1\" /S /E /ZB /DCOPY:T /SEC /COPYALL /XC /XO /XF *.*

Get-Content (Get-ChildItem "J:\temp\2" -Recurse -Force -ErrorAction SilentlyContinue ).fullname | ForEach-Object {
J:\temp\windows\precomp.exe -o"J:\temp\1\$fout" -cn -d10 -intense -brute -s125 -t-j "$file"
}

另外Powerrshell:如果我使用Get-Content我得到错误访问路径被拒绝,或者使用Get-ChildItem突然显示错误的路径,如果手动匹配它会给我错误“ReadLines”和“1”参数(s )" 所以它不再逐行解析它

$file = @(Get-ChildItem "J:\temp\2\" -Recurse -Force -ErrorAction SilentlyContinue ).fullname
$fout = (Get-ChildItem "J:\temp\2\" -Recurse -Force -ErrorAction SilentlyContinue ).fullname.Substring(10)

Robocopy "J:\temp\2" "J:\temp\1\" /S /E /ZB /DCOPY:T /SEC /COPYALL /XC /XO /XF *.*

$arrayFromFile = @(Get-Content $file)
$arrayFromFout = @(Get-Content $fout)

foreach($line in [System.IO.File]::ReadLines("$arrayFromFile"))
{
       J:\temp\windows\precomp.exe -o"J:\temp\1\$_arrayFromFout" -cn -d10 -intense -brute -s125 -t-j "$_arrayFromFile"
}

标签: bashpowershellloopsparsing

解决方案


最后不明白你在 foreach 循环中做了什么。您从 J:\temp\2 中获取所有数据,但什么也不做。要使用此数据,您必须在 foreach 中使用 $_ 变量。

 Get-Content (Get-ChildItem "J:\temp\2" -Recurse -Force -ErrorAction SilentlyContinue ).fullname | ForEach-Object {
    J:\temp\windows\precomp.exe -o"J:\temp\1\$fout" -cn -d10 -intense -brute -s125 -t-j "$file"
    }

推荐阅读