简体   繁体   中英

remove last n characters of a file using batch

I am having around 500 files in a folder. 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

Now I want them to revert to orignal state. Few files are around 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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