简体   繁体   English

如何重命名子文件夹或zip文件中的文本文件

[英]How to rename text files within subfolders or zip files

I have multiple zip folders with unique names but the files within the folders all have the same name. 我有多个具有唯一名称的zip文件夹,但是文件夹中的文件都具有相同的名称。 I need to rename the text files to match the folder names. 我需要重命名文本文件以匹配文件夹名称。 I also need those renamed text files to be moved to the working directory. 我还需要将那些重命名的文本文件移至工作目录。

For example, I have the following: 例如,我有以下内容:

name1.zip decompresses to name1\\AncestryDNA.txt but need to be name1.txt. name1.zip解压缩为name1 \\ AncestryDNA.txt,但需要为name1.txt。 name2.zip decompresses to name2\\AncestryDNA.txt but need to be name2.txt. name2.zip解压缩为name2 \\ AncestryDNA.txt,但需要为name2.txt。

I used 7-Zip 19.00 to unzip the files. 我使用7-Zip 19.00解压缩文件。 I decompressed the files because I also need to remove lines starting with "#" from each decompressed text file for use in another program. 我解压缩了文件,因为我还需要从每个解压缩的文本文件中删除以“#”开头的行,以供其他程序使用。

I start with a windows batch file the runs the following on my Data directory that contains my zip files: 我从Windows批处理文件开始,在包含我的zip文件的数据目录中运行以下命令:

echo Unzipping the files... echo解压缩文件...

forfiles /p Data /m *.zip /c "cmd /c 7z x *.zip -o* -y"

echo Moving zip files to storage... 将zip文件移动到存储中...

forfiles /p Data /m *.zip /c "cmd /c move @file ..\StorageZipFiles"

echo Trimming all except header from AncestryDNA text files... echo修剪除AncestryDNA文本文件中的标头以外的所有内容...

forfiles /p Data /s /m *.txt /c "cmd /c sed -i '/#/d' @file"

These work but I can't get the rename function to work. 这些工作,但我无法使重命名功能正常工作。

I tried to rename the files before decompressing... 我尝试在解压缩之前重命名文件...

forfiles /p Data /s /m *.zip /c "cmd /c 7z rn @file @fname\AncestryDNA.txt @fname\@fname.txt" 

and... 和...

forfiles /p Data /s /m *.zip /c "cmd /c 7z a rn @file @fname\AncestryDNA.txt @fname\@fname.txt"

and after decompressing... 然后解压缩...

forfiles /p Data /s /m *.txt /c "cmd /c ren @file @fname.txt" 

but I have not been successful with carrying the name of the folder over to the text file within the folder. 但是我无法成功将文件夹名称保留到文件夹内的文本文件中。

When trying to rename the files after decompressing, the command prompt does not produce an error message. 尝试在解压缩后重命名文件时,命令提示符不会产生错误消息。 It keeps the old file name of AncestryDNA.txt nested within the folders name1, name2, etc. 它将AncestryDNA.txt的旧文件名嵌套在文件夹name1,name2等中。

Another way of renaming the files after decompression is this powershell script. 解压缩后重命名文件的另一种方法是此powershell脚本。

Get-ChildItem  -recurse -path Data *.txt | rename-item -path {$_.fullname} -newname  {$($_ | split-path | split-path -leaf ) + ".txt"} -whatif

This has two interleaved powershell pipelines, one gets all the right directories and the second pipeline extracts the name of the folder used to rename the file. 它有两个交错的Powershell管道,一个管道获取所有正确的目录,第二个管道提取用于重命名文件的文件夹的名称。 Note that I put a -whatif at the end of this for safety. 请注意,出于安全考虑,我在此末尾添加了-whatif。 If the output looks good then remove the -whatif to actually change the filenames. 如果输出看起来不错,则删除-whatif以实际更改文件名。 It is still best to test this on test data before using on real data. 仍然最好先在测试数据上对此进行测试,然后再使用实际数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM