powershell - Powershell + 7zip 批量提取和重命名 - 收到“没有要处理的文件”消息
问题描述
编辑:
最初,我的问题是为什么第一段代码不起作用。如果我在循环外的单个文件上单独运行解压缩操作,则解压缩操作正在工作。但是一旦我用循环将它包裹起来,它就不起作用了,也没有红色错误。
谢谢@nemze,他/她的回答启发我改变我的代码:
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
ls -Path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -Path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = '"$zipFolderChild"+"\"+"Data.zip"'
$command = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword $zipFile"
iex $command
#file rename command that I have not written yet
}
至 :
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
ls -Path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -Path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = "$zipFolderChild"+"\"+"Data.zip"
$command = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword ""$zipFile"""
iex $command
#file rename command that I have not written yet
}
通过将$zipFile
定义移到ForEach
循环之外,这是可行的!
我认为我的第二个障碍现在转移到在循环内重命名我的文件。
我想要达到的目标:
- 将 20181001 文件夹中的 Data.xls 重命名为 20181001.xls
- Data.xls 从 20181008 文件夹重命名为 20181008.xls
修改后的代码仍然读取$zipFolderChild
为完整路径,我怎样才能只提取文件夹名称?
编辑3:
试图将重命名语句放入循环中,但不确定如何使-NewName
参数起作用,$zipFolderChild.Name.xls
clear 不起作用。也试过:
$folder= $zipFolderChild.Name
#file rename command that I have not written yet
$rename = "-path", "$zipOutPath\Data.xls" ,"-NewName", "$folder.xls"
& Rename-Item @rename
在循环内,也不起作用。
最后工作:
$7ZipPath = "C:\7z\7za"
$zipFolderRoot = "Z:\long folder path\Test Folder\Unzip Test"
$zipOutPath = "Z:\long folder path\Test Folder\Unzip Test\Unzip"
$zipFilePassword = "TEST123"
ls -path $zipFolderRoot -directory -Exclude Unzip
Foreach ($zipFolderChild in (ls -path $zipFolderRoot -directory -Exclude Unzip))
{
$zipFile = "$zipFolderChild\Data.zip"
$cmd = "& $7ZipPath x -o'$zipOutPath' -y -p$zipFilePassword ""$zipFile"""
iex $cmd
$folder= $zipFolderChild.Name
$xlsFile = "$zipOutPath\Data.xls"
$NewName = "$zipOutPath\$folder.xls"
& Rename-Item -Path "$zipOutPath\Data.xls" -NewName $NewName
}
解决方案
这会更好吗?
& $7ZipPath x -o$zipOutPath -y -p$zipFilePassword $zipFile
$zipFile 不会得到带有单引号的变量:
$zipFile = "$zipFolderChild" + "\" + "Data.zip"
推荐阅读
- node.js - Properly handling Google OAuth with separated frontend and backend
- javascript - 为什么 Javascript 不能与 Django HTML 一起使用?
- python - docker中的容器路径是什么?
- c# - 如何获取所有超过 N 分钟的文件?
- python - “图像尺寸太大”错误在 python 中保存颤动图并剪裁
- python - 返回日期之间的行仅间歇性地工作
- php - Slackware Web 服务器不加载 PHP MySQL 扩展,即使它们存在于正确的目录中
- python - 如何将PDF文件提取到没有表头/标题的Pandas数据框,因为数据框将其视为列
- android - 如何从房间持久性库中仅获取字符串?
- python - 解析Json提取键值python