[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 求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: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"

TOP

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

TOP

回复 3# idwma


    感谢大侠支招, 还是不理解导致问题的原因, 有相关的资料参考吗?

TOP

回复 4# 5i365


呃就是些基础常识呀
1楼原因就是你把cd切换当前目录这步省了呀,又没把路径补上
2楼help dir有说明

TOP

回复 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. }
复制代码

TOP

回复 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. }
复制代码

TOP

回复 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()
}

TOP

回复 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. }
复制代码
1

评分人数

    • 5i365: 乐于助人, 技术牛X技术 + 1

TOP

回复 9# idwma


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

TOP

返回列表