Saved Bookmarks
| 1. |
Solve : Set as Type? |
|
Answer» I was wondering if it's possible to SET something in a batch, as the TYPE of a file. I know that trying to do this, only gets the first line of the file: @echo off The above code will produce a compound (stem.tail) type array where you could iterate numerically through the tails to reproduce the file. However the file contents are not in a single variable. Quote for /f "delims=" %%i in ('type C:\path to file\file.txt') do (set contents=%%i) The above code will generate a variable with a value of the last line in file.txt. You can modify the snippet to use CONCATENATION: Code: [Select]@echo off for /f "delims=" %%i in ('type C:\path to file\file.txt') do ( call set contents=%%contents%%%%i ) echo %contents% which will put the contents of the file into a single variable. Unfortunately, all the carriage returns and line feeds will have been stripped out. Reconstructing the original file will be quite the challenge. Why do you need to do this and have you considered using VBScript or even Powershell which have instructions for just this very purpose? for /f "token=1*" %%a in (file.txt) do ((call set /a int=%%int%%+1&call set int.%%int%%=%%a%%b)) How can i add this all to one line similar to the above?Quote from: Sidewinder on January 05, 2009, 05:05:53 AM Why do you need to do this and have you considered using VBScript or even Powershell which have instructions for just this very purpose?I'm trying to compress the file. There is a way to compress, then decompress the file using the set function, but I need the full contents. I'm not sure there's a way to recreate it in VB, and my school probably doesn't have powershell...(Yes, I am a student) The school also doesn't allow downloading unauthorized software, so you can rule that out...Quote The school also doesn't allow downloading unauthorized software, so you can rule that out... Understandable. Powershell also requires the .Net Framework. Quote I'm trying to compress the file. There is a way to compress, then decompress the file using the set function We may have different definitions of compress, but I'm fairly certain the set command would not be helpful in this situation. VBScript however, can be helpful here. Windows recognizes compressed files by a header record. Once Windows finds the header, any files(s) copied into this namespace are compressed using system routines. Code: [Select]Set WshShell = WScript.CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") strTargetFile = WshShell.ExpandEnvironmentStrings("%temp%") & "\test1.zip" 'compressed output file Set zip = fso.CreateTextFile(strTargetFile) zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & STRING(18, 0) 'this is the zip header record zip.Close WScript.Sleep 500 Set objShell = CreateObject("Shell.Application") Set objFile = objShell.NameSpace(strTargetFile) Set src = objShell.NameSpace("c:\scripts") 'input directory; files will be compressed objFile.CopyHere src WScript.Sleep 10000 Save script with a vbs extension and run from the command line as cscript scriptname.vbs If this snippet is helpful, great! If not perhaps you could explain how you would use set to compress a file. Quote from: Sidewinder on January 05, 2009, 06:48:13 PM If this snippet is helpful, great! If not perhaps you could explain how you would use set to compress a file.Gladly. The only reasonable way to compress a text-based file (.txt or .doc are what I'm planning) would be to change whole words, "the" for example, into one-letter phrase...say "t". I'm also using this code for a censor in another batch project. Here's the code. Code: [Select]set contents=%contents:the=t%Pretty simple...I do realise that it won't be perfect, but that's what debugging is for. Now if only I could SET the TYPE of a file...Quote from: Helpmeh on January 06, 2009, 04:59:18 PM would be to change whole words, "the" for example, into one-letter phrase...say "t". I'm also using this code for a censor in another batch project. think about it. you change "the" into t. now you expand it. but... suddenly words that were originally tiny and tooth and tatters are theiny, theootheh and theathetheers. true I guess...I could do a letter+number combination... Like, the into t1 and tiny into t2 and so on and so forth...So there's no CONFUSION, but can ANYONE solve this problem?Quote So there's no confusion, but can anyone solve this problem? Which one? The compression or the single variable CONTAINING the contents of a file? For the single variable switch to VBScript which is already installed on your XP machine. Many compression algorithms use statistical redundancy to reduce file size. Text and documents have much white space which can be reduced by declaring the the numerical iterations followed by the character in question. For example: 40 consecutive spaces in a document might be reduced to [40] . As was mentioned, compressing the file is not main hurdle. Reconstructing the file from it's compressed state is the real challenge. I was just sort of picturing it...find a way to compress it in dos, but save it as an unused extension...Then compile the decompresser to an exe and make the new file always open with the exe decompresser... Am I just dreaming? or is this possible in some way?Some of the things you mentioned are possible. But not all of them are possible. So no, it's not possible.Ok. Thanks anyway! |
|