Board logo

标题: [问题求助] 求PowerShell解惑 dir 命令 加cd和不加cd的结果为什么不同 [打印本页]

作者: 5i365    时间: 2022-2-15 13:41     标题: 求PowerShell解惑 dir 命令 加cd和不加cd的结果为什么不同

PS文件和W文件夹在同一目录, W文件夹的结构在最后:

目前问题: code1没有任何输出, code2有输出且正确, 为什么会出现这种情况?
示例文件夹: https://send.cm/d/8ojE
  1. #code1
  2. dir "W" -dir |
  3. where{
  4. (dir $_ -File -dep 2).name -contains "YES"
  5. }
  6. #code2
  7. cd "W"
  8. dir -dir |
  9. where{
  10. (dir $_ -File -dep 2).name -contains "YES"
  11. }
复制代码
---------------------------------------------------------------
C:\Users\Administrator\Desktop\T>tree W /f
卷 Win 的文件夹 PATH 列表
卷序列号为 448B-18AC
C:\USERS\ADMINISTRATOR\DESKTOP\T\W
├─1
│  └─A
│          YES

├─2
│  └─B
│          YES

└─3
    └─C
            NO
作者: 5i365    时间: 2022-2-16 09:17

本帖最后由 5i365 于 2022-2-16 09:19 编辑

又遇到相同的问题了,带cd的能成功, 不带cd的没有输出:
在task5文件夹下找YES文件, 然后将其移动到所在文件夹的父文件夹下的OK文件夹中

cd task5
$s = gci -File -rec "YES"
move $s.Directory "$($s.Directory.parent)\OK"
---------------------------------------------------------
$s = gci task5 -File -rec "YES"
move $s.Directory "$($s.Directory.parent)\OK"
作者: idwma    时间: 2022-2-16 14:16

  1. dir "W" -dir |
  2. where{
  3. (dir $_.fullname -File -dep 2).name -contains "YES"
  4. }
复制代码
  1. gci task5 -File -rec  -include "YES"
复制代码

作者: 5i365    时间: 2022-2-16 16:37

回复 3# idwma


    感谢大侠支招, 还是不理解导致问题的原因, 有相关的资料参考吗?
作者: idwma    时间: 2022-2-16 16:56

回复 4# 5i365


呃就是些基础常识呀
1楼原因就是你把cd切换当前目录这步省了呀,又没把路径补上
2楼help dir有说明
作者: 5i365    时间: 2022-2-16 17:00

回复 5# idwma


    可是下面这个, 图片为什么没有保存到CD转到的目录?
  1. #@&cls&powershell "type '%~0'|out-string|iex"&pause&exit
  2. [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')
  3. cd "C:\Users\Administrator\Desktop\新建文件夹"
  4. dir *.bmp |
  5. foreach{
  6. $image = [Drawing.Image]::FromFile($_.FullName)
  7. #$image.Save($($_.BaseName + '.jpg'), [Drawing.Imaging.ImageFormat]::Jpeg)
  8. $s=$_.BaseName
  9. $image.Save("$s.jpg", [Drawing.Imaging.ImageFormat]::Jpeg)
  10. $image.Dispose()
  11. }
复制代码

作者: idwma    时间: 2022-2-16 17:14

回复 6# 5i365


    放的位置不对
  1. #@&cls&cd "C:\Users\Administrator\Desktop\新建文件夹"&powershell "type '%~0'|out-string|iex"&pause&exit
  2. [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')
  3. dir *.bmp |
  4. foreach{
  5. $image = [Drawing.Image]::FromFile($_.FullName)
  6. #$image.Save($($_.BaseName + '.jpg'), [Drawing.Imaging.ImageFormat]::Jpeg)
  7. $s=$_.BaseName
  8. $image.Save("$s.jpg", [Drawing.Imaging.ImageFormat]::Jpeg)
  9. $image.Dispose()
  10. }
复制代码

作者: 5i365    时间: 2022-2-16 18:07

回复 7# idwma


    我不保存成bat, 而是在 powershell ise 中直接执行ps代码, 也不行, 没有在   新建文件夹   中生成jpg文件
我想使用cd 命令, 并想让jpg文件, 保存在cd到的文件夹中,

[void][Reflection.Assembly]:oadWithPartialName('System.Drawing')
cd "C:\Users\Administrator\Desktop\新建文件夹"
dir *.bmp |
foreach{
        $image = [Drawing.Image]::FromFile($_.FullName)
        #$image.Save($($_.BaseName + '.jpg'), [Drawing.Imaging.ImageFormat]::Jpeg)
        $s = $_.BaseName
        $image.Save("$s.jpg", [Drawing.Imaging.ImageFormat]::Jpeg)
        $image.Dispose()
}
作者: idwma    时间: 2022-2-16 18:39

回复 8# 5i365

官方文档里什么都有呀
  1. https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_automatic_variables#pwd
复制代码
  1. [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')
  2. $a="C:\Users\Administrator\Desktop\新建文件夹"
  3. [System.Environment]::CurrentDirectory=$a
  4. cd $a
  5. dir *.bmp |
  6. foreach{
  7. $image = [Drawing.Image]::FromFile($_.FullName)
  8. #$image.Save($($_.BaseName + '.jpg'), [Drawing.Imaging.ImageFormat]::Jpeg)
  9. $s=$_.BaseName
  10. $image.Save("$s.jpg", [Drawing.Imaging.ImageFormat]::Jpeg)
  11. $image.Dispose()
  12. }
复制代码

作者: 5i365    时间: 2022-2-16 19:05

回复 9# idwma


大侠牛X, 这回懂了, 原来有个这个变量:
[System.Environment]::CurrentDirectory=$a




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2