AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-969781

Io-oI's questions

Martin Hope
Io-oI
Asked: 2025-02-23 12:44:39 +0800 CST

如何在 PowerShell 中操作和清理 -WhatIf 的输出?

  • 7

我正在尝试捕获并清理powershell-WhatIf中文件重命名操作的参数输出。目标是删除输出的某些部分,但即使经过多次尝试,我还是无法让它工作。以下是我尝试过的方法:

Set-Location "C:\Path\To\Directory"

$Replace1  = 'Item: C:\Path\To\Directory\'
$Replace2  = 'What if: Performing the operation `"Rename File`" on target'
$Replace3  = 'Destination: C:\Path\To\Directory\'
$VarIvExp  = "GCI -Path . -Filter *.pdf | Ren -New { `$_.Name -replace ' ', '_' -replace '-', '_' } -Whatif ;"

# Various attempts to capture and replace parts of the output:
Invoke-Expression $VarIvExp            | % { ($_ 3>&1).Replace($Replace1,'').Replace($Replace2,'').Replace($Replace3,'') }
Invoke-Expression $VarIvExp.ToString() | % { ($_ 3>&1).Replace($Replace1,'').Replace($Replace2,'').Replace($Replace3,'') }

Invoke-Expression $VarIvExp 3>&1       | % {  $_.ToString().Replace($Replace1,'').Replace($Replace2,'').Replace($Replace3,'')       }
Invoke-Expression $VarIvExp.ToString() | % { ($_.ToString() 3>&1).Replace($Replace1,'').Replace($Replace2,'').Replace($Replace3,'') }

Invoke-Expression $VarIvExp.ToString() | % {  $_            | out-string -Replace $Replace1,'' -Replace $Replace2,'' -Replace $Replace3,'' }
Invoke-Expression $VarIvExp            | % {  $_.ToString() |            -Replace $Replace1,'' -Replace $Replace2,'' -Replace $Replace3,'' }

Invoke-Expression $VarIvExp 3>&1       | % {  $_            | out-string -Replace $Replace1,'' -Replace $Replace2,'' -Replace $Replace3,'' }
Invoke-Expression $VarIvExp.ToString() | % {  $_            |            -Replace $Replace1,'' -Replace $Replace2,'' -Replace $Replace3,'' }
Invoke-Expression $VarIvExp            | % {  $_.ToString() |            -Replace $Replace1,'' -Replace $Replace2,'' -Replace $Replace3,'' }

  • 输出:
What if: Performing the operation "Rename File" on target "Item: C:\Path\To\Directory\SomeText  I nº 001-SMSCBMRJ-20250222.pdf" Destination: C:\Path\To\Directory\SomeText__I_nº_001_SMSCBMRJ_20250222.pdf
What if: Performing the operation "Rename File" on target "Item: C:\Path\To\Directory\SomeText  I nº 002-SMSCBMRJ-20250222.pdf" Destination: C:\Path\To\Directory\SomeText__I_nº_002_SMSCBMRJ_20250222.pdf
What if: Performing the operation "Rename File" on target "Item: C:\Path\To\Directory\SomeText II nº 001-SMSCBMRJ-20250222.pdf" Destination: C:\Path\To\Directory\SomeText_II_nº_001_SMSCBMRJ_20250222.pdf
What if: Performing the operation "Rename File" on target "Item: C:\Path\To\Directory\SomeText II nº 002-SMSCBMRJ-20250222.pdf" Destination: C:\Path\To\Directory\SomeText_II_nº_002_SMSCBMRJ_20250222.pdf

注意:我还尝试过直接在 上进行各种操作$Results,但没有成功。以下是使用 进行的尝试$Results...$Results.Replace('various attempts','')

Set-Location "C:\Path\To\Directory"
$Replace1  = 'Item: C:\Path\To\Directory\'
$Replace2  = 'What if: Performing the operation `"Rename File`" on target'
$Replace3  = 'Destination: C:\Path\To\Directory\'

$VarIvExp  = "GCI -Path . -Filter *.pdf | Ren -New { `$_.Name -replace ' ', '_' -replace '-', '_' } -Whatif ;"

$Results = Invoke-Expression $VarIvExp

$Results = Invoke-Expression $VarIvExp.Replace( -Replace $Replace1,'' -Replace $Replace2,'' -Replace $Replace3,'')

$Results.ToString().Replace($Replace1,'').Replace($Replace2,'').Replace($Replace3,'')

  • 期望/预期输出:
SomeText  I nº 001-SMSCBMRJ-20250222.pdf SomeText__I_nº_001_SMSCBMRJ_20250222.pdf
SomeText  I nº 002-SMSCBMRJ-20250222.pdf SomeText__I_nº_002_SMSCBMRJ_20250222.pdf
SomeText II nº 001-SMSCBMRJ-20250222.pdf SomeText_II_nº_001_SMSCBMRJ_20250222.pdf
SomeText II nº 002-SMSCBMRJ-20250222.pdf SomeText_II_nº_002_SMSCBMRJ_20250222.pdf
powershell
  • 1 个回答
  • 65 Views
Martin Hope
Io-oI
Asked: 2024-10-22 17:51:45 +0800 CST

如何在 PowerShell 中写入‘Stream:$DATA’并批量删除‘:$DATA’?

  • 6

尝试从cmd:$DATA中的文件中删除 Stream ,但没有任何效果,除了powershell之外,是否不再可能使用纯cmd / batch来删除?

重新创建/重现场景并显示失败的尝试......

  • 首先将字符串写入:Stream文件:
>"C:\Path\To\LogError.Log::Stream.Report" (echo/[CopyStatus]&echo/Error=3)

  • :Stream第二步从文件中读取字符串:
more < "C:\Path\To\LogError.Log:Stream.Report" 
:: output:
[CopyStatus]
Error=3

  • 第三个列表/检查:Stream文件中的内容:
dir /r "C:\Path\To\LogError.Log:Stream.Report" | find "$DATA"
:: Output:
25 LogError.Log:Stream.Report:$DATA

  • 第四次尝试通过多次尝试从文件中删除:Stream.Report( )::$DATA
cd.>"C:\Path\To\LogError.Log:Stream.Report"
cd.>"\\?\C:\Path\To\LogError.Log:StreamReport'"

echo.>"C:\Path\To\LogError.Log:Stream.Report" 
echo.>"\\?\C:\Path\To\LogError.Log:Stream.Report"

more < nul > "C:\Path\To\LogError.Log:Stream.Report" 
more < nul > "\\?\C:\Path\To\LogError.Log:Stream.Report" 

fsutil reparsepoint delete "C:\Path\To\LogError.Log:Stream.Report"
fsutil reparsepoint delete "C:\Path\To\LogError.Log:Stream.Report"

wmic datafile where "name='C:\\Path\\To\\LogError.Log'" delete "Stream.Report"
wmic datafile where "name='C:\\Path\\To\\LogError.Log:Stream.Report'" delete "Stream.Report"

  • 第 5 列表/检查(再次):Stream文件中的内容以验证它们是否已被删除:
dir /r "C:\Path\To\LogError.Log:Stream.Report" | find "$DATA"
:: output 
25 LogError.Log:Stream.Report:$DATA

more < "C:\Path\To\LogError.Log:Stream.Report" 
:: Output:
[CopyStatus]
Error=3
  • 没有任何变化......$DATA仍然留在文件中。

我正在尝试做什么,但我被卡住了。
我成功删除了内容:Stream:,但没有$DATA从文件中排除流;它仍然存在:

>dir /r LogError.Log
 Directory of C:\Path\To

10/22/2024  05:24 AM            17,083 LogError.Log
                                    25 LogError.Log:Stream.Report:$DATA
               1 File(s)         17,083 bytes
               0 Dir(s)  128,989,868,032 bytes free

没有任何困难,它直接与powershell一起工作

Remove-Item -Path "C:\Path\To\LogError.Log:Stream.Report" 
Get-Content -Path "C:\Path\To\LogError.Log:Stream.Report" 

有没有办法:$DATA使用纯powershell和批处理来删除?我正在寻找一种允许我在两种环境中处理此问题的解决方案,但我发现每个工具都面临限制。如何在两个平台上实现相同的功能?

cmd.exe /c '(echo/[CopyStatus]&echo/Error=3)>"C:\Path\To\LogError.Log:Stream.Report"'

:: Not work:
[System.IO.File]::WriteAllText("C:\Path\To\LogError.Log:Stream.Report", "[CopyStatus]")
[System.IO.File]::WriteAllText("C:\Path\To\LogError.Log:Stream.Report", "[Error=3]"   )

"[CopyStatus]" | Out-File -FilePath "C:\Path\To\LogError.Log:Stream.Report" -Encoding utf8
   "[Error=3]" | Out-File -FilePath "C:\Path\To\LogError.Log:Stream.Report" -Encoding utf8

但是在cmd中,我无法删除,在powershell中,我无法写入。感觉我被困在中间:PowerShell 可以执行两个操作(读取和删除),而 cmd 也可以执行两个操作(写入和读取)。

为什么我不能在powershell中执行所有三个操作,也不能在cmd中执行所有三个操作? 有办法实现吗?


  • 概括起来,问题的关键点如下:
问题: 到:
1.删除: File.Log:$DATA 命令/命令
2.     写入: File.Log: Stream:$DATA powershell
powershell
  • 2 个回答
  • 169 Views
Martin Hope
Io-oI
Asked: 2021-09-12 15:50:38 +0800 CST

如何将变量发送到后台进程,这是一个循环:蝙蝠本身的标签(如果定义了变量,则停止循环)

  • 5

我有一个带有一些标签的蝙蝠,所以我开始我的 bat 检查是否传递了任何参数,如果没有,执行将通过一个参数对 bat 本身进行新调用,一旦完成,它将继续执行下一个命令。

当 bat 通过一个带参数的调用开始时,它的执行将在 :label 内循环(永久)

这是一个可能需要很长时间才能运行的蝙蝠,因此调用会导致“无限”进度条出现在窗口标题中,并在后台循环执行。

在此处输入图像描述

因为在屏幕上或标题栏中看到静态行(“Finding Nemo, Files, etc...”)很乏味,而且最近一个需要很长时间的任务/脚本示例正在寻找更新的通过扫描所有驱动程序并比较结果以获得一个(更新的)可执行文件(版本/日期),在此处回答。

以上面链接的代码为一个耗时的任务示例,并在标题中添加一个进度条,我有以下代码:

@echo off

if "%~1."=="." (call start ".:." /b cmd.exe /v:on /c "%~f0" ? )else goto %:^|
setlocal enabledelayedexpansion && set "_f=" && set "_ff=" && set /a "_cnt=0"

for /f usebackq^tokens^=* %%f in (`%__AppDir__%mountvol.exe^|find.exe /i ":\"
    `)do 2>nul pushd %%~f && (set/a "_cnt+=1" && call %:^) _ff_!_cnt! & popd)

for /f useback^tokens^=1*delims^=^=^ %%i in (`^<nul ^<con: set _ff_ ^|%__AppDir__%sort.exe /r
    `)do <con: <nul call set "_ffprobe=%%~dpnxj" & call set "_ffprob_Path=%%~dpj" & goto %:^V

%:^)
for /f usebackq^tokens^=2^,4* %%i in =;(`2^>nul %__AppDir__%where.exe /r \ ffprobe.exe /t ^|^
    %__AppDir__%sort.exe /r^|^|exit/b`)do set "_f=%%~k" && for /f useback^delims^=. %%G in (`
        ^<con %__AppDir__%wbem\wmic.exe DataFile where name^="!_f:\=\\!" get LastModified ^|^
            %__AppDir__%findstr.exe /rb [0-9]`)do <con: <nul call set "%~1=%%~k" && exit /b 0

%:^|
>nul chcp 65001 & mode 89,5 & set "_title=Looking for your latest ffprob.exe, please wait^!!"

<con: ;color 0a && set^ "_176=░░░░░░░░░░" && set^ "_255=▓▓▓▓▓▓▓▓▓▓" && set^ "_022=▬▬▬▬▬▬▬▬▬▬"
call setlocal enabledelayedexpansion & set "_ping=%__AppDir__%pathping.exe 127.1 -q 1 -p 050"

%:^/
set "_title=I'm doing something that takes a while, please wait^!!"
for %%G in =;(1,2,3,4,5,6,0)do if "!_ffprob_Path!\." == "\." =;( if ".\%%~G\." == ".\1\." =;(
     for /l %%L in (0 01 10)do %_ping% |title !_title! [!_255:~0,%%~L!!_022:~%%L!] !time:~,8! 
    )else if "%%G\" == "2\" =;(if "!_ffprob_Path!\." == "\." =;(
     for /l %%L in (10 -1 0)do %_ping% |title !_title! [!_255:~0,%%~L!!_176:~%%L!] !time:~,8! 
   ))else if "%%G\" == "3\" =;(if "!_ffprob_Path!\." == "\." =;(
     for /l %%L in (0 01 10)do %_ping% |title !_title! [!_022:~0,%%~L!!_176:~%%L!] !time:~,8! 
   ))else if "%%G\" == "4\" =;(if "!_ffprob_Path!\." == "\." =;(
     for /l %%L in (10 -1 0)do %_ping% |title !_title! [!_022:~0,%%~L!!_255:~%%L!] !time:~,8! 
   ))else if "%%G\" == "5\" =;(if "!_ffprob_Path!\." == "\." =;(
     for /l %%L in (0 01 10)do %_ping% |title !_title! [!_176:~0,%%~L!!_255:~%%L!] !time:~,8! 
   ))else if "%%G\" == "6\" =;(if "!_ffprob_Path!\." == "\." =;(
     for /l %%L in (10 -1 1)do %_ping% |title !_title! [!_176:~0,%%~L!!_022:~%%L!] !time:~,8! 
   ))else if "%%G\" == "0\" =;(if "!_ffprob_Path!\." == "\." =;(
    goto %:^/
   )));= 

%:^V
rem./ your code continues at this point using: / & echo+!_ffprob_Path! & echo+!_ffprobe!

结果是“无限”进度条开始并且不会停止,即使在设置变量之后,它应该在进度条标签中表示循环结束,但它不起作用......

在此处输入图像描述

使用启用/禁用 setlocal 进行了几次尝试,颠倒了标签调用的顺序,以及我什至不记得的事情,但是有几次尝试结束循环但没有成功......

如何结束在同一蝙蝠后台运行的标签的循环?

我已经在这一点上很久了,我什么也没看到。

任何想法?


常规布局中的相同代码...

@echo off

if "%~1." == "." (
     call start ".:." /b cmd.exe /v:on /c "%~f0" ? 
    ) else (
     goto %:^p
    )
    
setlocal enabledelayedexpansion

set "_f="
set "_ff="
set /a "_cnt=0"

for /f "usebackq tokens=*" %%f in (`%__AppDir__%mountvol.exe ^| %__AppDir__%find.exe /i ":\"`) do 2>nul (
     pushd %%~f && (
         set /a "_cnt+=1"
         call %:^) _ff_!_cnt!
         popd
        )
    )
    
for /f "usebackq tokens=1* delims==" %%i in (`set _ff_ ^| %__AppDir__%sort.exe /r`) do (
     call set "_ffprobe=%%~dpnxj"
     call set "_ffprob_Path=%%~dpj"
     goto %:^V
    )
 
%:^)
for /f "usebackq tokens=2,4*" %%i in  (`2^>nul %__AppDir__%where.exe /r \ ffprobe.exe /t ^| %__AppDir__%sort.exe /r ^|^| exit /b`) do (
     set "_f=%%~k"
     for /f "usebackq delims=." %%G in (`^<con: call %__AppDir__%wbem\wmic.exe DataFile where name^="!_f:\=\\!" get LastModified ^| %__AppDir__%findstr.exe /rb [0-9]`)do (
         call set "%~1=%%~k"
         exit /b 0
        )
    )

%:^p
>nul chcp 65001
%__AppDir__%mode.com con: cols=89 lines=5

set "_title=Looking for your latest ffprob.exe, please wait^!!"

color 0a
set "_176=░░░░░░░░░░"
set "_255=▓▓▓▓▓▓▓▓▓▓"
set "_022=▬▬▬▬▬▬▬▬▬▬"

setlocal enabledelayedexpansion
set "_ping=%__AppDir__%pathping.exe 127.1 -q 1 -p 050"

%:^/
for %%G in =;(1,2,3,4,5,6,0)do (
     if "!_ffprob_Path!\." == "\." ( 
        if ".\%%~G\." == ".\1\." (
             for /l %%L in (0 01 10) do %_ping% |  title !_title! [!_255:~0,%%~L!!_022:~%%L!] !time:~,8! 
        )else if "%%G\" == "2\" (
             if "!_ffprob_Path!\." == "\." (
                 for /l %%L in (10 -1 0) do %_ping% | title !_title! [!_255:~0,%%~L!!_176:~%%L!] !time:~,8! 
                )
        )else if "%%G\" == "3\" (
             if "!_ffprob_Path!\." == "\." (
                 for /l %%L in (0 01 10) do %_ping% | title !_title! [!_022:~0,%%~L!!_176:~%%L!] !time:~,8! 
                )
        )else if "%%G\" == "4\" (
             if "!_ffprob_Path!\." == "\." (
                 for /l %%L in (10 -1 0) do %_ping% | title !_title! [!_022:~0,%%~L!!_255:~%%L!] !time:~,8!
                )
        )else if "%%G\" == "5\"  (
             if "!_ffprob_Path!\." == "\." (
                 for /l %%L in (0 01 10) do %_ping% | title !_title! [!_176:~0,%%~L!!_255:~%%L!] !time:~,8! 
            )
        )else if "%%G\" == "6\"  (
             if "!_ffprob_Path!\." == "\." (
                 for /l %%L in (10 -1 1) do %_ping% | title !_title! [!_176:~0,%%~L!!_022:~%%L!] !time:~,8! 
            )
        )else if "%%G\" == "0\"  (
            if "!_ffprob_Path!\." == "\." (
                 goto %:^/
                )
            )
        )
    ) 
   
%:^V
rem./ your code continues at this point using: / 
echo+!_ffprob_Path!
echo+!_ffprobe!
powershell batch
  • 1 个回答
  • 103 Views
Martin Hope
Io-oI
Asked: 2021-01-27 06:40:14 +0800 CST

那会是一个错误吗?执行时立即关闭当前cmd窗口:move nul 2>&0

  • 9

最终,我move nul 2>&0在我的脚本中使用“突然”关闭当前的 cmd 窗口,从而立即中止/关闭正在运行的 bat。

我不能说它就像剧本中的自杀一样,但我通常这样称呼它。

事实上,这会导致在当前 cmd 窗口中扩展/达到效果的自动终止。

这不仅仅是关闭/结束正在运行的球棒。

基本上用于一些批处理,其中变量的值"%cmdcmdline%"通过单击调用来执行响应,而不是简单地关闭蝙蝠,也关闭通过单击蝙蝠激活/加载的窗口。

我想知道的是:

  1. 什么解释了这种行为?
  2. 它是一个错误吗?
  3. 将来它的行为可能不一样吗?

一些代码示例:

@echo off 

title <nul 
title to kill or not to kill?

mode 50,05

echo\%cmdcmdline%|find "%~0" >nul && (
     echo\
     echo\ so sorry bro, I'll kill myself
     echo\ and this CMD.exe window too!
     
     timeout 5 /nobreak 
     move nul 2>&0 
   )

echo/ will run this code bro!
echo/
echo/ keep itself alive: %date% %time:~0,5%
echo/
pause
windows command-line
  • 1 个回答
  • 1411 Views
Martin Hope
Io-oI
Asked: 2020-04-23 01:06:52 +0800 CST

在 Windows Linux 子系统中更新/升级 Debian:解决“deb.debian.org”的临时故障

  • 4

在此处输入图像描述

Linux 子系统不解析名称,因此无法安装或更新任何东西,为什么会发生这种情况以及如何修复?

在此处输入图像描述

ecker@Lame_Slug:~$ cat /etc/apt/sources.list
deb http://deb.debian.org/debian buster main
deb http://deb.debian.org/debian buster-updates main
deb http://security.debian.org/debian-security/ buster/updates main
deb http://ftp.debian.org/debian buster-backports main

Windows 10 专业版 x64 1909 英文版

观察: Windows 是全新/全新安装,在 Debian 中没有进行任何编辑...

windows-10 debian
  • 1 个回答
  • 1350 Views
Martin Hope
Io-oI
Asked: 2019-12-04 17:52:36 +0800 CST

cmd Windows 运算符 || 和 && 在 macOS 终端中?有一些安全的选项可以做同样的事情吗?

  • 3

在 Windows cmd 中,我可以使用"||"并"&&"按照强制执行的结果进行操作。

some_command && (does_because_returned 0) || (does_because_it_did_not_return_0)

  • 举例:
date -u 1203222019 && (echo corrected date/time) || (echo uncorrected date/time)

在 macOS 中可以使用哪些等效的运算符来执行相同的操作

或者对于安全行为,什么可以做同样的事情?用于脚本的终端?

macos command-line
  • 1 个回答
  • 952 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve