Jump to content

hidding a text file


Ace08
 Share

Recommended Posts

hi guys i what i want to do is to have variable that counts how many times the script was run. seeing this is kinda imposible(for me) i've decided to create a textfile, read from the file and every time the script is run add 1 to the content.... the problem is users when encountering such bizare files tend to do treat them as viruses or somewhat useless files then deletes them and when that happens my counter will be reset..... so the question is how do i set the file in hidden and unhide when reading it then hide it again.... in vbs it can be done with file.attribute = 2(Hidden), can i do the same with autoit?

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

Hello Ace08,

Just thought I would answer your original question just in case. You can change file attributes, such as Hidden, with FileSetAttrib(). Also, there is no need to to unhide the file in order to read/write to it. Also to keep ppl from deleting the file, I generaly choose a common folder that most ppl don't know about such as the Application Data Directory or the Windows Directory. The Application Data Directory is also commonly a Hidden Folder, so those that do know about it, generally have the understanding that the files there in are beneficial and in some case critical to an applications process. Since your appending one small stat, I would recommend using IniRead/IniWrite.

Example when creating the file with hidden attributes:

Local $myFile=@AppDataDir & "\MyApplication\MyFile.ini"

$file = FileOpen($myFile,1)
FileClose($file)

FileSetAttrib($myFile,"+H");Sets file to Hidden

Example appending the file:

Local $myFile=@AppDataDir & "\MyApplication\MyFile.ini"
$lastCount=IniRead($myFile,"My Application","Run Count",-1)
$newCount = $lastCount +1
IniWrite($myFile,"My Application","Run Count",$newCount)

If you want to make the file look like it is a critical component to your application, you can change the file extension, in which IniRead/Write will still be able to read it, I have tested with .dat, .config, .tmp, .dll with no problems.

Realm ;)

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. 

Link to comment
Share on other sites

Thanks HavikTech,Wakillon and Realm for the tips...

@wakillon i've tried using regkeys and its working but i have a few questions about this, like how will i know the key existed? when i restart the PC will it be deleted? what if the power for your PC was cut off and the PC was reading the key what will happen? or how will i know that the key is still there? ..... sorry if im asking to much its just that i hav'nt used regkey before while the process i would be doing is kinda critical.

@Realm thanks for this piece of code im gonna keep it.... might be useful for my script thanks ;)

@HavikTech might as well try wakillons idea :)

Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

Thanks HavikTech,Wakillon and Realm for the tips...

@wakillon i've tried using regkeys and its working but i have a few questions about this, like how will i know the key existed? when i restart the PC will it be deleted? what if the power for your PC was cut off and the PC was reading the key what will happen? or how will i know that the key is still there? ..... sorry if im asking to much its just that i hav'nt used regkey before while the process i would be doing is kinda critical.

No reg key are not deleted after reboot, try this...

; to each start
$_RegKeySettings = "HKEY_CURRENT_USER\Software\HiddingTextFile\Settings"

$_RegRead = RegRead ( $_RegKeySettings, "StartNumber" )
ConsoleWrite ( "$_RegRead StartNumber value : " & $_RegRead & @Crlf )
If $_RegRead = '' Then ; if there is no value
    RegWrite ( $_RegKeySettings, "StartNumber", "REG_SZ", 1 )
Else
    RegWrite ( $_RegKeySettings, "StartNumber", "REG_SZ", $_RegRead + 1 )
    
EndIf


; and for delete reg key : RegDelete ( $_RegKeySettings, "StartNumber" )

Understand you better ?

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

thanks again wakillon, actualy what i did was something like this

RegWrite("HKEY_CURRENT_USER\C:\Script\Test", "TestKey", "REG_SZ", "Hello this is a test")
$var = RegRead("HKEY_CURRENT_USER\C:\Script\Test", "TestKey")
MsgBox(4096, "", $var)

i was just thinking since i hav'nt dealt with reg keys thanks for the clarification ;)

Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

oh man..... sorry for bringing this post back but i am having troubles with this one, apparently my boss dosn't like registry keys(what am i gonna do with it, my job is to follow him) so ive decided to do what i originaly posted hidding a text file thus came Realm's code, modified a few because the file only need one content(numbers only)that is the number of times the script was run.

the code i have so far is this:

Local $myFile = "C:\Scripts\Autoit\RunSeq.dat"
$Rfile = FileOpen($myFile)
$Seq = FileReadLine($Rfile)
FileClose($Rfile)
$SeqNumber = Number($seq + 1)
msgbox(0," ", $SeqNumber)

FileOpen($myFile,2)
FileWrite($myFile,$SeqNumber)
FileSetAttrib($myFile,"+H")

if i comment out FileSetAttrib($myFile,"+H")then the script works fine output increses by one every time i run the script but after adding filesetattrib it doesn't work anymore...

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

the file will contain a number(number of times the script was run) i want it hidden so that users won't be able to see them because they might delete the file, if that happens the script will have to generate a new text file then will start the counting all over again.... also i would like to use this to tag the output of the file say input is test.txt then the output should give me 01test.txt.

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

I have created some functions for you.

In _LaunchCount_GetFile() you need to replace NAME_OF_YOUR_APPLICATION_OR_GUID. I prefer using GUIDs but that may be just me. You can generate one here if you want. A GUID is unique and will ensure that no other application with the same name as yours will try to use your files.

We will be using the Application Data folder of Windows. In Windows 7 this path would look like C:\Users\USERNAME\AppData\Roaming\OUR_FOLDER_WILL_BE_CREATED_HERE. The AppData folder is there for this exact purpose.

MsgBox(64, "LaunchCount by Calistoga", _
                "_LaunchCount_GetFile(): " & _LaunchCount_GetFile() & @CRLF & _
                "_LaunchCount_GetCount(): " & _LaunchCount_GetCount() & @CRLF & _
                "_LaunchCount_IncrementCount(): " & _LaunchCount_IncrementCount());

Exit;

Func _LaunchCount_GetFile()

    Local $s_configDir = @AppDataDir & "\NAME_OF_YOUR_APPLICATION_OR_GUID";

    Local $s_configFile = $s_configDir & "\config.dat";

    If Not (FileExists($s_configDir)) Then

        If Not (DirCreate($s_configDir)) Then Return SetError(1, 0, "");

    EndIf

    If Not (FileExists($s_configFile)) Then

        If Not (FileWrite($s_configFile, "")) Then Return SetError(2, 0, "");

    EndIf

    Return SetError(0, 0, $s_configFile);

EndFunc ;==>_LaunchCount_GetFile

Func _LaunchCount_GetCount()

    Local $s_config = _LaunchCount_GetFile();

    If ($s_config = "") Then Return SetError(1, 0, 0);

    Local $s_content = FileRead($s_config);

    If (@error = -1 Or @error = 1) Then Return SetError(2, 0, 0);

    If ($s_content = "") Then $s_content = 0;

    Return SetError(0, 0, $s_content);

EndFunc ;==>_LaunchCount_GetCount

Func _LaunchCount_IncrementCount()

    Local $i_count = _LaunchCount_GetCount();

    If (@error <> 0) Then Return SetError(1, @error, False);

    Local $s_config = _LaunchCount_GetFile();

    If (@error <> 0) Then Return SetError(2, @error, False);

    $h_config = FileOpen($s_config, 2);

    If ($h_config = -1) Then Return SetError(3, 0, False);

    If Not (FileWrite($h_config, $i_count + 1)) Then Return SetError(4, 0, False);

    FileClose($h_config);

    Return SetError(0, 0, True);

EndFunc ;==>_LaunchCount_IncrementCount

If you want the functions to be more flexible you could use this version of _LaunchCount_GetFile() where NAME_OF_YOUR_APPLICATION_OR_GUID has been replaced with a function argument instead.

Func _LaunchCount_GetFile($s_GUID)

    Local $s_configDir = @AppDataDir & "\" & $s_GUID;

    Local $s_configFile = $s_configDir & "\config.dat";

    If Not (FileExists($s_configDir)) Then

        If Not (DirCreate($s_configDir)) Then Return SetError(1, 0, "");

    EndIf

    If Not (FileExists($s_configFile)) Then

        If Not (FileWrite($s_configFile, "")) Then Return SetError(2, 0, "");

    EndIf

    Return SetError(0, 0, $s_configFile);

EndFunc ;==>_LaunchCount_GetFile

We could improve this function further by checking whether $s_GUID contains illegal NTFS characters ( / ? < > \ : * | ” ), but I'm sure you would figure that out.

You don't have to use _LaunchCount_GetFile() as that is handled automatically by the two other functions. The first time you call _LaunchCount_GetCount() it will return 0. You increment the count by one using _LaunchCount_IncrementCount().

I did some quick tests and it should work ;)

Edited by Calistoga
Link to comment
Share on other sites

Thanks a lot Calistoga although this is an advance coding(at my current level) i'll try to understand how this work :)

also i've managed to fix my script by adding FileSetAttrib($myFile,"-H+N")at the beggining of the script and FileSetAttrib($myFile,"+H-N") at the end of the script(although i realy am not sure what +- means) ;)

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

+H means that the attribute 'Hidden' will be applied to your file, - means that the attribute will be removed (visible again). The same logic goes for N.

Note though, that if you use my functions then you do not need to apply these attributes as the file resides in a hidden directory specially designed for this kind of files. A regular user will never find it and start messing with it.

Basically what the functions does, is:

- Create the correct folder structure.

- Create the file (config.dat which is a regular text file).

- Return error values if something goes wrong.

- Return and update the launch count value inside config.dat.

I should have commented where i used SetError(), but when you see Return SetError(etc) used it means that the function will halt and a value will be stored inn the @error macro. This is to help you with troubleshooting what went wrong during the execution of the function. If you see SetError(3, 0, False), the first number will be stored in @error, the second in @extended and the last one will be returned directly from the function.

When I'm at it, this function will reset the count to 0. @error will equal 1 if the function fails, the return value will then be False. @extended will equal 0 in all cases.

Func _LaunchCount_ResetCount()

    Local $s_config = _LaunchCount_GetFile();

    If Not (FileDelete($s_config)) Then Return SetError(1, 0, False);

    Return SetError(0, 0, True);

EndFunc ;==>_LaunchCount_ResetCount

I understand if you are confused now, so just ask if you got any questions ;)

Edited by Calistoga
Link to comment
Share on other sites

hi there well you see not all users are the same, some can be a little anoying, some may also dont know what that file is for and thinking its useless then deletes them. but anyways i was able to solve this one.... i just need to study calistoga's code above to make it a bit more better..

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...