Author Topic: Program code to simulate removing and inserting USB stick  (Read 5138 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Program code to simulate removing and inserting USB stick
« Reply #15 on: January 15, 2021, 02:34:08 pm »
@SpriggsySpriggs

Thanks for your reply.

My (second-hand) laptop has, which various 3-rd party software reports as Windows "Enterprise", has Windows 10 x64 Pro build 20H2 and updated up to about a month ago. Sometimes there are some various small differences the way Windows seems to operate (maybe this thread is yet another one) and for instance once I tried to dual install Linux on to it and it really totally crashed my system - worst most complicated reinstall of Windows and files I ever experienced.

With your program I simply entered "J"  (no slashes or colon or ") as you might notice from the tiny screenshot above. I had to use the /D switch as part of the "recovery process"  as also shown in the screenshot above - essentially in the last screenshot, this is the minimum steps to restore my usb stick back to normal (i tried only as per your last sentence above - but it does not restore for me). I ran both QB64 AND CMD as ADMINISTRATOR for the tests.

A couple of reasons why I want to do it:-

A fair percentage of my usb sticks (I have many different models) run very hot (too hot to touch) but still appear to function properly - to date none have failed or died. Because of my program automated projects these usb sticks may be still connected for days continuously, and I am not always around (or awake), so if the program cycles round to say 3am in the morning to cross-check the usb stick data and only needs 5 minutes connection - I was hoping that shutting down ("ejecting") the usb sticks would allow the HOT ones to cool down (and stay cool) until when and if they are accessed again automatically.

I have "quarantined" a number of usb sticks that were "compromised" when my laptop was infected with a "virus" a year ago. Again automating the process - I am investigating "minimal connect time" of these suspect usb sticks of extracting all the data (and possibly purposely fragmenting the data) - so should a virus be present (it seemed to take windows automatic  defender anti-virus about 4 weeks (if my memory serves correct) to eliminate ALL the virus renamed-exe files (that is another story))  So, if automatically a suspect usb stick is only "connected" for say a minute rather than a day - it minimises the opportunity for the suspect usb stick virus (if present) to do damage - it can take a fair while for Windows anti virus to scan the many files on the usb sticks.

Hope this gives you some insight of why the "ejecting" etc (I like the idea of automated programs rather than me manually).

Thanks for all your efforts - at least the first program to eject only will be quite useful - I may have to try out SHELLing the mountvol for the remounting the usb sticks.

I forgot to mention that ALL my drives, except A:\, are FOLDER PROTECTED - so for the first time a new program runs (eg yours) I have to "Allow" the application access to J:\ - but thereafter I don't have to worry. All my results reporting to you  was when your program(s) were running from the second time onwards.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Program code to simulate removing and inserting USB stick
« Reply #16 on: January 15, 2021, 03:13:49 pm »
@Richard Ok that makes more sense to me now. Hopefully some piece of that code will end up being useful to you. I'll keep this in mind for later down the road if I happen upon some code that better emulates the ejecting and mounting. It was actually a little difficult just to find the code that I gave you.
Shuwatch!

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Program code to simulate removing and inserting USB stick
« Reply #17 on: January 15, 2021, 08:42:01 pm »
@SpriggsySpriggs

Just going on a bit of a "tangent" (but it MAY still be related to all that has been said)...


I purchased a brand new usb stick 64 GByte some time ago - the only purpose of this "special" usb stick is solely for manually copying over files from a 2009 MAC computer to PC. The MAC I only use occasionally and I am too cheap to spend any more money on it (like buy software to enable MAC to write to NTFS   OR  buy software so that PC can read/write a MAC formatted external drive). I formatted the "special" usb stick as exFAT (the lowest common denominator for interchanging data).

The "special" usb stick worked perfectly a number of times with essentially completely overwriting ALL the data - then ALL OF A SUDDEN it became "write protected" (there is no physical switch etc on the "special" usb stick for write protect).

Apparently this problem is known to occasionally occur with some usb brands (including mine "Verbatim") and all the suggestions I tried from Google (from MAC sites AND from PC sites) do not cancel the write protect status. I have forgotten exactly but there was a fairly straightforward CMD solution (run as ADMINISTRATOR) which supposedly allowed one to change the status, applied same - cmd reports now OK (not write protected) - but the device is still write protected. I also tried the usual stuff like to reformat the "special" usb stick (I don't care about the files on it - I already made a backup). Except for the write protected - the "special" usb stick seems to be working perfectly.

Just for fun I tried mountvol with some switches to rectify the situation, but with no luck.

As I say it does not matter what happens to the data - I would just like to use the "special" usb stick without restrictions.

Maybe you have access to "more sophisticated methods" of cancelling the "write protected" (hoping)?

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Program code to simulate removing and inserting USB stick
« Reply #18 on: January 15, 2021, 09:14:36 pm »
If you plug it into a fresh computer and can't write to it, I'd suggest returning the product as faulty.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Program code to simulate removing and inserting USB stick
« Reply #19 on: January 15, 2021, 09:28:58 pm »
For fun, look into the DOS command attrib. easydos.com is a good site for reference. You can use the command line, or write a SHELL in QB64. You might be able to re-write the write protect by doing so.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Program code to simulate removing and inserting USB stick
« Reply #20 on: January 15, 2021, 11:11:10 pm »
@Pete

Just for fun I tried ATTRIBUTE in CMD as ADMINISTRATOR. Attribute does not work with directory like the site says. I tried it for a particular file

originally the file's attribute was  "A" BUT when did ATTRIB -R ... (FOR Read+Write) got message

Unable to change attribute .... (for the path/filename)

@luke  will try with a "fresh" computer ASAP - because the usb stick was purchased I think over a year ago, I gather any warranties are now void. (I did previously use that same usb stick many times at almost full capacity - just that all of a sudden it became "write protected" - maybe the MAC did something very funny to the usb stick.

Just in case anyone reading this knows - is there any software (free I hope) that can read AND write individual bytes on a usb stick, especially in all the FAT boot etc records? I do own a file recovery software package but apparently it is only designed to recover to another drive (never intended to repair the faulty drive )(Note I do not need to recover the files as I can still read the usb stick normally)