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-1152506

bat_cmd's questions

Martin Hope
bat_cmd
Asked: 2023-02-27 11:13:07 +0800 CST

能否修改这些 bat/ps1 文件以在 MTP 设备上打开文件夹(我当前的 bat/ps1 脚本已经可以复制 mp4 文件)

  • 5

我正在使用以下.ps1脚本和.bat文件从我手机上的文件夹复制 MP4 文件,该文件夹是没有驱动器号的 MTP 设备(因此需要使用 Powershell)。

我想修改 Powershell 脚本和 bat 文件,使其不复制 MP4 文件,而只是打开 MP4 文件所在的文件夹。

这是 Powershell 脚本,名为CFD.ps1:

param([string]$deviceName,[string]$deviceFolder,[string]$targetFolder,[string]$filter='(.jpg)|(.mp4)$')
 
function Get-ShellProxy
{
    if( -not $global:ShellProxy)
    {
        $global:ShellProxy = new-object -com Shell.Application
    }
    $global:ShellProxy
}
 
function Get-Device
{
    param($deviceName)
    $shell = Get-ShellProxy
    # 17 (0x11) = ssfDRIVES from the ShellSpecialFolderConstants (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx)
    # => "My Computer" — the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel.
    # This folder can also contain mapped network drives.
    $shellItem = $shell.NameSpace(17).self
    $device = $shellItem.GetFolder.items() | where { $_.name -eq $deviceName }
    return $device
}
 
function Get-SubFolder
{
    param($parent,[string]$path)
    $pathParts = @( $path.Split([system.io.path]::DirectorySeparatorChar) )
    $current = $parent
    foreach ($pathPart in $pathParts)
    {
        if ($pathPart)
        {
            $current = $current.GetFolder.items() | where { $_.Name -eq $pathPart }
        }
    }
    return $current
}
 
$deviceFolderPath = $deviceFolder
$destinationFolderPath = $targetFolder
# Optionally add additional sub-folders to the destination path, such as one based on date
 
$device = Get-Device -deviceName $deviceName
$folder = Get-SubFolder -parent $device -path $deviceFolderPath
 
$items = @( $folder.GetFolder.items() | where { $_.Name -match $filter } )
if ($items)
{
    $totalItems = $items.count
    if ($totalItems -gt 0)
    {
        # If destination path doesn't exist, create it only if we have some items to move
        if (-not (test-path $destinationFolderPath) )
        {
            $created = new-item -itemtype directory -path $destinationFolderPath
        }
 
        Write-Verbose "Processing Path : $deviceName\$deviceFolderPath"
        Write-Verbose "Moving to : $destinationFolderPath"
 
        $shell = Get-ShellProxy
        $destinationFolder = $shell.Namespace($destinationFolderPath).self
        $count = 0;
        foreach ($item in $items)
        {
            $fileName = $item.Name
 
            ++$count
            $percent = [int](($count * 100) / $totalItems)
            Write-Progress -Activity "Processing Files in $deviceName\$deviceFolderPath" `
                -status "Processing File ${count} / ${totalItems} (${percent}%)" `
                -CurrentOperation $fileName `
                -PercentComplete $percent
 
            # Check the target file doesn't exist:
            $targetFilePath = join-path -path $destinationFolderPath -childPath $fileName
            if (test-path -path $targetFilePath)
            {
                write-error "Destination file exists - file not moved:`n`t$targetFilePath"
            }
            else
            {
                $destinationFolder.GetFolder.CopyHere($item)
                if (test-path -path $targetFilePath)
                {
                    # Optionally do something with the file, such as modify the name (e.g. removed device-added prefix, etc.)
                }
                else
                {
                    write-error "Failed to move file to destination:`n`t$targetFilePath"
                }
            }
        }
    }
}

这是.bat文件...

cls
@echo off
color 0f
title Copy MP4 Files

:: Use Powershell to copy MP4 files. Requires Powershell script "CFD.ps1" next to this batch file.

:: --------------------------------------------------------------------------------------------------------------
:: Put your device name here inside the double quotes:
set DEVICE_NAME="Galaxy A3 (2017)"

:: Put your device folder path (the part after the device name in "This PC") here inside the double quotes:
set DEVICE_FOLDER_PATH="Card\DCIM\Camera"

:: -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
:: Copy MP4 files from Device
cls
@echo off
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dp0CFD.ps1' -deviceName '%DEVICE_NAME%' -deviceFolder '%DEVICE_FOLDER_PATH%' -targetFolder '%CD%' -filter '(.mp4)$'"

:: ------------------------------------------------------------------------------
exit

如果运行上述批处理文件并假定 2 个变量DEVICE_NAME并DEVICE_FOLDER_PATH具有正确的设备名称和路径,以及插入和打开的设备,它将 MP4 文件复制到运行 bat 文件的文件夹中。MP4 文件最初位于此文件夹中:

This PC\Galaxy A3 (2017)\Card\DCIM\Camera

既然这样,你会认为更改上面的 bat 和 ps1 文件并不难,所以它们只需打开 mp4 文件所在的文件夹即可。

我已经连续三天询问 Chat GPT,以更改我的 2 个脚本,以便它只在 Windows 资源管理器中打开文件夹,并且在可能尝试了 50 多次之后它没有给我一个有效的答案!

由于上面的方法可以复制 MP4 文件,我不明白为什么修改它会出现问题,所以它只是打开文件夹而不是从中复制 MP4 文件,但到目前为止我还没有成功。

如果有人可以阐明如何执行此操作,那么您将解决我几个小时以来一直试图解决的问题!

windows
  • 1 个回答
  • 51 Views
Martin Hope
bat_cmd
Asked: 2023-02-12 07:01:26 +0800 CST

在批处理文件中使用时将一个 Powershell 命令拆分为多行?

  • 7

此命令用于将txt 文件中的OldText1,OldText2和 的实例替换为,并且当命令位于批处理文件中的一行时,如下所示:OldText3NewText1NewText2NewText3

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem 'Old and New text.txt' | ForEach-Object {(Get-Content $_) -replace 'OldText1', 'NewText1' -replace 'OldText2', 'NewText2' -replace 'OldText3', 'NewText3' | Set-Content $_.FullName}"

如果我尝试使用该^符号来拆分命令(通常在批处理文件中有效),它就不起作用。例如,这不起作用,txt 文件中没有任何内容被替换:

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem 'Old and New text.txt' | ForEach-Object {(Get-Content $_) ^
-replace 'OldText1', 'NewText1' ^
-replace 'OldText2', 'NewText2' ^
-replace 'OldText3', 'NewText3' ^
| Set-Content $_.FullName}"

使用反引号也不起作用(但不会因为这不是 PS1 脚本):

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem 'Old and New text.txt' | ForEach-Object {(Get-Content $_) `
-replace 'OldText1', 'NewText1' `
-replace 'OldText2', 'NewText2' `
-replace 'OldText3', 'NewText3' `
| Set-Content $_.FullName}"

我整个早上都在问那个聊天机器人,但它无法提供任何有用的东西。有一半时间它会再次将命令放在一行上,而我只是绕着它转圈。

做我想做的事是不可能的吗?

我想将命令拆分成多行的原因很简单,因为我希望能够读取正在替换的内容,而不必水平查看(最终会是什么)一个庞大的命令。我打算添加比这更多的替换命令,但它甚至对三个替换命令都不起作用......但是!

提前感谢任何可能知道为什么这不起作用的人。

windows
  • 1 个回答
  • 52 Views
Martin Hope
bat_cmd
Asked: 2022-06-06 02:20:58 +0800 CST

当前面有“cd /d”(Windows 10,批处理文件)时,for 循环不起作用

  • 5

如果我在 MP4 文件文件夹内的批处理文件中运行它,它可以将最后修改的 MP4 文件设置为变量,因此可以使用默认媒体播放器播放:

:: Set last modified MP4 file in current folder, as a variable...
for /f "eol=: delims=" %%F in ('dir /B /O:D *.mp4') do @set "newest=%%F"

:: Play the file (opens in default media player)...
"%newest%"

如果我尝试将以下内容放在 MP4 文件文件夹之外的批处理文件中,cd /d则被 for 循环忽略,它不起作用:

:: Change to directory containing MP4 files...
cd /d "C:\My Videos"

:: Set last modified MP4 file in "C:\My Videos", as a variable (not working)...
for /f "eol=: delims=" %%F in ('dir /B /O:D *.mp4') do @set "newest=%%F"

:: Copy the MP4 to current folder...
xcopy "%newest%" "%CD%" /i /r /v /k /f /c /h /y >nul 2>&1

:: Change back to current directory
cd /d "%CD%"

如您所见,每个示例中的 for 循环命令完全相同。它适用于第一个,但不适用于第二个。

我尝试在每个命令之间放置pause以查看是否出现任何错误,但没有。

我试着从周围去掉双引号%CD%- 没有区别。

我试着把Setlocal EnableDelayedExpansion命令放在前面,什么也没做。

以前,我尝试通过添加/S到 DIR 命令直接在 FOR 循环中指定文件夹,但由于某种原因复制了错误的 MP4 文件,其中修改的日期是无意义的随机日期,它不是最后修改的 MP4。该命令是这样的:

for /f "eol=: delims=" %%F in ('dir /B /S /O:D "C:\My Videos\*.mp4"') do @set "newest=%%F"

这确实将 MP4 设置为变量......但不是正确的!

在 MP4 文件的文件夹中使用批处理文件,我知道它总是这样工作。

提前感谢任何可以解释为什么这不起作用的人。

windows command-line
  • 1 个回答
  • 183 Views
Martin Hope
bat_cmd
Asked: 2022-03-25 22:10:00 +0800 CST

在 Windows 10 中使用命令行打开资源管理器窗口时复制 GUI 行为?

  • 5

如果我双击文件夹的快捷方式并打开该文件夹,然后再次双击相同的快捷方式,Windows 不会打开该文件夹的第二个实例,如果它已经打开,它会激活(或最大化)该文件夹,即我试图在命令行上复制的行为。

我可以在 bat 文件中运行它: explorer "MyFolder"

这会打开 bat 文件旁边的“MyFolder”文件夹,但如果我在“MyFolder”已经打开的情况下再次运行 bat 文件,它会打开该文件夹的另一个实例。

explorer.exe 几乎没有任何开关,所以我想知道是否还有其他一些未记录的解决方法或秘密开关,所以如果 bat 文件多次运行也没关系,只有一个“MyFolder”实例会打开?

也许有一个小的命令行程序可以处理这个问题,但我什么都不知道,因此在这里问。

提前喝彩。

编辑:经过一番摆弄,这可行,但如果文件夹已经最大化,它会最小化它。不过,它确实只能打开一个实例...... start "" "MyFolder"

我想这可能是可用的,但如果文件夹已经打开并最大化,则将其最小化是不方便的。

我可以创建文件夹的快捷方式并在命令行上运行它,并将属性设置为“运行最大化”,但我不希望它填满屏幕,“运行最大化”会这样做。

windows command-line
  • 1 个回答
  • 66 Views
Martin Hope
bat_cmd
Asked: 2022-03-11 15:25:41 +0800 CST

Powershell命令(在批处理文件中)从文件名中删除最后3个字符(扩展名之前)?

  • 5

我有 mp3 文件(具有不同长度的名称)并且想要删除批处理文件旁边的每个 mp3 文件的“.mp3”之前的最后三个字符。

例如foobar043.mp3需要成为foobar.mp3

这听起来很简单,但我尝试了很多方法(在批处理文件中使用 PowerShell)并尝试仅使用批处理命令来完成它 - 使用 SuperUser 上已有的答案。

我的最后一个命令最接近,但它也删除了文件扩展名。这被缩短为命令本身:

gci *.mp3 | rename-item -newname { $_.name.substring(0,$_.name.length-7) }

现在,如果只有某种方法可以在末尾保留“.mp3”(或者将现在的无扩展名文件重命名为 .mp3,而不重命名文件夹!)它会正常工作。

人们通常要求列出已经尝试过的内容,但这个问题最终会延长十倍!这么说吧,如果你在谷歌中输入“powershell remove last characters from file name”,我已经尝试了到目前为止我发现的所有东西。

只是一些尝试接受(和投票)答案的例子:

  • https://stackoverflow.com/questions/33255834/powershell-rename-filename-by-removing-the-last-few-characters
  • https://stackoverflow.com/questions/57202742/how-to-remove-last-x-number-of-character-from-file-name
  • https://stackoverflow.com/questions/29636714/remove-end-of-filename-using-powershell
  • https://stackoverflow.com/questions/10396769/powershell-remove-last-3-characters-from-file-name

(以及关于香料和reddit的答案,如果这里不允许,我不会链接到)。

提前感谢任何知道如何在保留文件扩展名的同时执行此操作的人。

这是一个艰难的过程,因为通常我可以将文件名(不带扩展名)设置为变量,但因为 PowerShell 将重命名文件,所以该变量实际上变得无用。

windows command-line
  • 2 个回答
  • 2096 Views
Martin Hope
bat_cmd
Asked: 2021-12-08 09:11:46 +0800 CST

Excel嵌套“IF”返回“FALSE”,但我不知道为什么

  • 5

我想在 Excel 中做一些非常简单的事情:

If Y13 is greater than 0% of X13 and less than 33% of X13, display "Strong".    
If Y13 is greater than 67% of X13 and less than 100% of X13, display "Weak".    
If Y13 is greater than 33% of X13 and less than 67% of X13, display "Medium".

我一定尝试了 20 种不同的方法,试图*0.67欺骗它以其他方式计算百分比,但无济于事。

这不会产生任何错误,但它错误地出现FALSE在单元格中,当 Y13是X13 (56/616) 的 9% 时,它应该属于“强”类别:

=IF(AND(Y13>0%<33%)X13,"Strong",IF(AND(Y13>67%<100%)X13,"Weak",IF(AND(Y13>33%<67%)X13,"Medium")))

由于某种原因,它没有说“强”,而是抛出了默认的 Excel FALSE。

我也尝试了这些,但没有任何工作:

=IF(AND(Y13>0%<33%X13),"Strong",IF(AND(Y13>67%<100%X13),"Weak",IF(AND(Y13>33%<67%X13),"Medium")))

=IF(AND(Y13>0%,X13,<33%,X13),"Strong",IF(AND(Y13>67%,X13,<100%,X13),"Weak",IF(AND(Y13>33%,X13,<67%,X13),"Medium")))

=IF(AND(Y13>0%X13<33%X13),"Strong",IF(AND(Y13>67%X13<100%X13),"Weak",IF(AND(Y13>33%X13<67%X13),"Medium")))

这是我认为应该工作但也没有:

=IF(AND(Y13>0%<33%),X13,"Strong",IF(AND(Y13>67%<100%),X13,"Weak",IF(AND(Y13>33%<67%),X13,"Medium")))

最后一个会产生错误“参数过多”,但删除(一些)逗号并没有解决它。

提前感谢任何可以看到问题所在的人。

microsoft-excel worksheet-function
  • 1 个回答
  • 25 Views
Martin Hope
bat_cmd
Asked: 2021-12-05 01:21:54 +0800 CST

缩短Excel“IF”公式?

  • 8

这个公式确实有效,但它是巨大的:

=IF(X3=B2,K2,IF(X3=B3,K3,IF(X3=B4,K4,IF(X3=B5,K5,IF(X3=B6,K6,IF(X3=B7,K7,IF(X3=B8,K8,IF(X3=B9,K9,IF(X3=B10,K10,IF(X3=B11,K11,IF(X3=B12,K12,IF(X3=B13,K13,IF(X3=B14,K14,IF(X3=B15,K15,IF(X3=B16,K16,IF(X3=B17,K17,IF(X3=B18,K18,IF(X3=B19,K19,IF(X3=B20,K20,IF(X3=B21,K21))))))))))))))))))))

这是它正在做的事情:

If X3 is the same as B2, show the contents of cell K2.
If X3 is the same as B3, show the contents of cell K3.
If X3 is the same as B4, show the contents of cell K4.
...etc etc etc all the way to...
If X3 is the same as B21, show the contents of cell K21.

由于 B2:B21 只是一列单元格,而 K2:K21 也只是一列单元格,有什么办法可以缩短上面的公式,所以它不是很大吗?

我不知道如何将其变成 2 个 B 细胞和 K 细胞范围。

尝试这样的事情是行不通的: =IF(X3=B2:B21,K2:K21)

因为告诉 Excel 使用:就是告诉它把从 B2 到 B21 和 K2 到 K21 的所有东西加在一起。我想知道是否有其他分隔符(不是 a :)告诉 Excel 单独处理每个单元格而不是添加它们?

这不起作用: =IF(X3=B2-B21,K2-K21)

这导致:#VALUE!

问题是,无论 B 细胞是哪个数字,都必须与 K 细胞中的相应数字(水平)保持匹配。

提前感谢任何可能知道答案的人,如果 Excel 中存在该功能,我相信这真的很简单。

microsoft-excel worksheet-function
  • 3 个回答
  • 562 Views
Martin Hope
bat_cmd
Asked: 2021-08-01 03:01:07 +0800 CST

批处理文件中的Powershell命令:从文本文件中删除所有行,除了带有“foo”和“bar”的行?

  • 2

我有一个包含这 3 行的文本文件:

This line contains foo.
This line contains bar.
This line has neither.

我可以在“.bat”文件(上面的“.txt”文件旁边)中运行以下命令,它会删除文本文件中的所有行,但包含“foo”的行除外:

Powershell.exe -Command "gci *.txt | foreach { (cat $_.FullName | ? { $_ -like '*foo*' }) | set-content $_.FullName}"

运行具有预期效果,在文本文件中只留下这一行:

This line contains foo.

现在的问题是:如果我想保留行中包含“foo”和“bar”的行怎么办?

到目前为止,我可能已经尝试了 40 或 50 种不同的组合,放一个竖线字符:*foo*|*bar*,去掉星号foo|bar,只在开头和结尾放星号:*foo|bar*,使用逗号,不使用逗号,使用分号,不使用分号,使用和不使用 以及许多其他方式复制-like命令。$_

没有任何效果 - 到目前为止,每次尝试时,我最终得到的文本文件看起来都一样,原来的三行包含 foo(第 1 行)、bar(第 2 行)和两者都不包含(第 3 行)。

为了确保我没有编辑命令并出错,我会定期删除我的编辑并再次运行该命令,以取出行上没有“foo”的行并且它可以工作,或者我只是再次粘贴命令,再次测试它,然后开始尝试编辑它以包含“bar”,但无济于事。

我最近已经问过这个问题,但我想我的标题措辞不清楚。有人试图回答它,但他们提供的每个答案都没有奏效,至少当我将命令放入批处理文件并在文本文件旁边运行时,也许它对他们有用,使用不同的方法我是谁,我不知道。

请注意,我在批处理文件中使用了 powershell 命令!我没有在控制台中输入或使用 ps1 脚本。 我不需要这样做,因为我上面提到的第一个命令确实可以删除所有带有“foo”的行。

显然,既然这确实有效,那么该命令可以用作添加的基础,因此它可以在一个命令中处理“foo”和“bar”。

除非这当然是 100% 不可能的——在大约 4 天之后尝试了 40 或 50 种不同的命令变体——这就是我开始怀疑的!

请,如果有人回答这个问题,请像我上面那样发布一个批处理文件命令。请不要告诉我改用 ps1 脚本或给出解释在控制台窗口中键入什么内容的答案。

我在上面发布的命令只需要某种修改,因此“bar”可以添加到“foo”,因此它可以处理多个文本字符串。

这看起来如此困难的原因似乎是因为如果您运行一个命令来取出除带有“foo”的行之外的所有行,那么您已经取出了带有“bar”的行。我怀疑这就是为什么还没有人想出一个可行的解决方案,也许根本就没有。

提前感谢任何有工作答案的人。通常,这些问题在这里得到了非常快的回答,或者我只是不断尝试不同的方式并让它发挥作用——但这次不是,出于某种原因,这真的很难。

windows command-line
  • 2 个回答
  • 599 Views
Martin Hope
bat_cmd
Asked: 2021-06-07 01:37:05 +0800 CST

Powershell - 在通配符之后替换文本,什么都没有?

  • 6

我在 Windows 批处理文件中使用 Powershell 命令来搜索这样的文本字符串:(foo=1 可能1是任何东西)

我知道.*?在 Powershell 中用作通配符。下面的代码片段不是完整的命令,我只放了相关部分。

我试图foo=1用 Powershell 通配符.*?替换整行,1如下所示:

-replace 'foo=.*?', ''

问题是,我没有留下空白行,而是留下了尾随=1

我不知道为什么没有按照上面代码框中的命令替换通配符。

提前感谢任何能对此有所启发的答案。

windows command-line
  • 2 个回答
  • 5914 Views
Martin Hope
bat_cmd
Asked: 2021-05-04 22:26:07 +0800 CST

在批处理文件中链接 Powershell 命令?(视窗)

  • 5

我在批处理文件中运行 Powershell 命令来替换文本。

这可行,但每次替换某些东西时它都会运行一个 Powershell 实例:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | Set-Content $_.FullName}"

:: Replace foo2 with bar2
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2' | Set-Content $_.FullName}"

:: Replace foo3 with bar3
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

& ^有没有办法通过在第二条命令的末尾添加类似的东西来链接三个命令?

我想将每个-replace命令放在单独的行上,因为我有 100 多个命令要链接在一起。

我试图这样做,但它不起作用:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | & ^

:: Replace foo2 with bar2
 | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2' | & ^

:: Replace foo3 with bar3
 | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

我也试过上面没有&字符的方法。

我尝试ForEach-Object {(Get-Content $_)从中间和最后一行删除,不起作用。

它是:: comments造成问题的原因吗?

我想既然这是用双引号括起来的 Powershell 命令,我会尝试这种方式,每行末尾的反勾号和分号应该告诉 Powershell 继续执行命令并将下一行视为相同的命令:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1';`

# Replace foo2 with bar2
 | ForEach-Object {(Get-Content $_) -replace 'foo2', 'bar2';`

# Replace foo3 with bar3
 | ForEach-Object {(Get-Content $_) -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

(不起作用)

我尝试取出一些 Powershell 命令:

:: Replace foo1 with bar1
start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1';`

# Replace foo2 with bar2
'foo2', 'bar2';`

# Replace foo3 with bar3
'foo3', 'bar3' | Set-Content $_.FullName}"

(不起作用)。

我已经尝试完全删除评论,不起作用。

我已经尝试添加回-replace每行的开头,但不起作用。

试图把它放在一条线上是行不通的:

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1'; -replace 'foo2', 'bar2'; -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

放置管道而不是分号也不起作用:

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace 'foo1', 'bar1' | -replace 'foo2', 'bar2' | -replace 'foo3', 'bar3' | Set-Content $_.FullName}"

可能有几十种不同的组合试图让它发挥作用,但我不知道正确的方法是什么,而且我没有想法。

;当与其他答案中描述的类似命令链接在一起时,这些命令不起作用。

干杯。

windows command-line
  • 2 个回答
  • 758 Views
Martin Hope
bat_cmd
Asked: 2021-05-02 05:13:37 +0800 CST

仅保留 txt 文件中包含特定单词/字符的行 (Powershell)(Windows 批处理文件)

  • 5

我只想保留包含该单词的行example并删除所有其他行。

我认为这会起作用,但它与我正在尝试做的相反:

PowerShell -Command "gci *.txt | foreach { (cat $_.FullName | ? { $_ -notlike '*example*' }) | set-content $_.FullName}"

我想以一个命令结束:

This line contains example.
This line does not.

进入这个:

This line contains example.

这似乎应该很简单,但我找不到任何答案。我发现的解决方案要么不起作用,要么相反,或者它们包含一堆我不需要做的其他事情。

干杯。

windows command-line
  • 1 个回答
  • 48 Views
Martin Hope
bat_cmd
Asked: 2021-04-21 20:08:13 +0800 CST

对 goto 标签(批处理文件)Windows 感到困惑

  • 5

如果我以这种方式使用这些命令,它会做它应该做的事情:

如果foo 以前设置为一件事或另一件事,它会跳到foobar_menu

如果foo以前没有设置为(相同的)一件事或另一件事,它会跳到another_menu

rem Check if foo was previously set as bar1 or bar2...
if "%foo%"=="bar1" goto :foobar_menu
if "%foo%"=="bar2" goto :foobar_menu

rem Check if foo was NOT previously set as bar1 or bar2...
if not "%foo%"=="bar1" goto :another_menu
if not "%foo%"=="bar2" goto :another_menu

这就是为什么它让我感到困惑 - 如果我如下所示交换它们,那么它不能正常工作,它会进入错误的标签,但为什么呢?

rem Check if foo was NOT previously set as bar1 or bar2...
if not "%foo%"=="bar1" goto :another_menu
if not "%foo%"=="bar2" goto :another_menu

rem Check if foo was previously set as bar1 or bar2...
if "%foo%"=="bar1" goto :foobar_menu
if "%foo%"=="bar2" goto :foobar_menu

为什么这两件事不做同样的事情?

这是因为我没有将 goto 部分放在括号中吗?

我确实让它工作(第一种方式),但这与我的菜单进入的顺序不同,我只是希望它按照上面第二个(非工作)示例中的布局方式运行。

干杯。

编辑:

这些是实际的命令。choose_audio_sample_rate以这种方式放置时,尽管是嵌套的,但即使在变量下设置了 opus 或 libopus ,它仍然会进入标签。

if not "%audio_codec%"=="libopus" if not "%audio_codec%"=="opus" goto :choose_audio_sample_rate
if "%audio_codec%"=="libopus" if "%audio_codec%"=="opus" goto :choose_audio_sample_rate_opus

实际上,交换的版本也不起作用,choose_audio_sample_rate即使在变量下设置了 opus 或 libopus 也是如此。

if "%audio_codec%"=="libopus" if "%audio_codec%"=="opus" goto :choose_audio_sample_rate_opus
if not "%audio_codec%"=="libopus" if not "%audio_codec%"=="opus" goto :choose_audio_sample_rate

我在这里变成了一个疯子,哈哈,好吧,如果我只使用它,它就会起作用(并直接传递到非作品菜单):

if "%audio_codec%"=="libopus" (goto :choose_audio_sample_rate_opus)
if "%audio_codec%"=="opus" (goto :choose_audio_sample_rate_opus)

如果它是 libopus,它会转到正确的标签。如果不是 libopus,则处理下一行,如果是 opus,则转到正确的标签。

如果超出此范围,则 libopus 或 opus 都不是,它会继续进入一般菜单。

windows batch
  • 3 个回答
  • 141 Views
Martin Hope
bat_cmd
Asked: 2021-04-15 02:18:51 +0800 CST

在批处理文件中使用powershell删除所有尾随空格?

  • 7

这可以从运行的批处理文件旁边的所有 txt 文件中的所有行的末尾删除一个空格:

start /wait /min Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem '*.txt' | ForEach-Object {(Get-Content $_) -replace ' $', '' | Set-Content $_.FullName}"

有没有办法修改上述内容,以便从行尾删除任何数量的空白(包括制表符)?

我只是想防止在行尾有两个或多个空格和/或制表符。

我整个早上都在寻找并尝试上述不同的变化,但无济于事。

干杯。

powershell command-line
  • 1 个回答
  • 556 Views
Martin Hope
bat_cmd
Asked: 2021-04-13 08:19:21 +0800 CST

PowerShell中双引号旁边的转义反斜杠失败

  • 6

我在批处理文件中运行 PowerShell 来替换字符,但我试图\"用".

\自己可以通过做来逃脱\\

"自己可以通过做来逃脱""""

这些工作,但如果双引号旁边有一个反斜杠,这不起作用:\\""""

是否可以这样做,仍然在批处理文件中使用 PowerShell?

powershell command-line
  • 1 个回答
  • 2293 Views
Martin Hope
bat_cmd
Asked: 2020-09-05 02:54:17 +0800 CST

从 Windows 的批处理文件中删除特定文件夹(如果它为空)?

  • 7

我想删除一个名为“NEW”的文件夹,但前提是该文件夹为空。

这是我尝试过的:

if exist "NEW\*" goto finished
if not exist "NEW\*" goto remove_new

:remove_new
rd /s /q "NEW\"
goto finished

:finished
exit

以上似乎是考虑到 NEW 文件夹存在所以它没有删除它?

windows batch
  • 3 个回答
  • 1463 Views
Martin Hope
bat_cmd
Asked: 2020-03-31 19:50:15 +0800 CST

具有提升权限的批处理文件不起作用?

  • 6

看到此解决方案后: 如何以提升的权限运行批处理文件命令?

我把它放在一个批处理文件中:

powershell -command "Start-Process cmd -ArgumentList '/c %CD% && elevated.bat' -Verb runas"

在该文件旁边,我使用以下命令放置“elevated.bat”:

regedit /S reg_entries.reg

之后,它会弹出一个用于 Windows 命令处理器 aka cmd.exe 的 UAC 提示:

cmd 的 UAC 对话框:

我肯定以管理员身份登录(通过检查控制面板 > 用户帐户确认)

我现在卡住了!

我认为上面第一个命令的目的是停止这个 UAC 提示?

windows-7 command-line
  • 1 个回答
  • 191 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