简体   繁体   English

使用批处理删除文件的最后n个字符

[英]remove last n characters of a file using batch

I am having around 500 files in a folder. 我的一个文件夹中有大约500个文件。 I am trying to concatenate a 4KB data(stored as a file) to all the files using batch " for /r %i in (*) do type 4KB file.txt >> %i 我正在尝试使用批处理“ for /r %i in (*) do type 4KB file.txt >> %i将4KB数据(存储为文件)连接到所有文件, for /r %i in (*) do type 4KB file.txt >> %i

Now I want them to revert to orignal state. 现在我希望他们恢复原始状态。 Few files are around 14GB . 几乎没有文件在14GB左右。 While trying to read , it takes a lot of time to open. 尝试阅读时,打开它需要很多时间。

Please let me know how can I revert them back to original state. 请让我知道如何将它们恢复为原始状态。

First off, the difficulty of this task depends on the actual string you appended to the file. 首先,此任务的难度取决于您附加到文件的实际字符串。

Get-ChildItem [your path here] | ForEach-Object {
$TempVar = Get-Content $_ | Select-string -Exclude "Something that uniquely matches the string you want to remove"
$TempVar | Set-Content $_ }

Alternatively: 或者:

Get-ChildItem [your path here] | ForEach-Object {
$Length = ($_.ToCharArray) - 4096 #As you have 4KB, exactly?
$Newfile = ($_.ToCharArray() | Select -first $Length) -join ""
$NewFile | Set-content $_ 

}

Both attempts can be considered harmful as this may affect encoding, file structure etc - I just don't have enough time to check on that. 两次尝试都被认为是有害的,因为这可能会影响编码,文件结构等-我只是没有足够的时间进行检查。 If the files are of any importance, better back them up. 如果文件很重要,最好备份它们。

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

相关问题 如何使用批处理脚本从文件中删除第一个和最后一个字符? - How to remove the first and last character from a file using batch script? 批处理文件可从文本文件中读取“ N”个字符 - Batch File to read 'N' characters from a text file 如何使用命令提示符或批处理文件从字符串中删除某些字符? - How to remove certain characters from a string using command prompt or batch file? 如何使用批处理从文本文件中删除回车符和换行符? - How to remove carriage return and line feed characters from a text file using batch? 使用批处理脚本解析文本文件并从每行中删除前 2 个字符 - Parse a text file using batch script and remove the first 2 characters from each line 是否可以使用批处理文件从Windows中的文件名中删除特定字符? - Is there any way to remove specific characters from a filename in Windows, using a batch file? 我想使用批处理脚本/cmd 从目录中的每个 txt 文件中删除最后一个字符 - I want to remove last character from each txt file in a directory using batch script/cmd 批量DOS复制文件的最后一行,限制为65536个字符 - Batch DOS copying last lines of a file limited by 65 536 characters 用于从文件名中删除特殊字符的批处理文件脚本(Windows) - Batch file script to remove special characters from filenames (Windows) 使用Java读取文件的最后n个字节 - Read last n bytes of file using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM