2

I'm a noob to this site, found lots of answers to other coding challenges in the past.

I'm also a noob to programming in Python. I have some code I wrote in DOS few years ago. Let me explain why:-

when you are using Windows 10 it keeps a log of "files used recently" this can be a big problem when I did a Windows 10 rollout at a big charity, and our HR team needed to borrow a laptop for a meeting, and when I got it back, logging on with the generic log on which was shared, a file was deleted but in frequently used files was something along the lines of "internal investigation into Mr _____.docx" which sounded very confidential.

Anyways, this script I wrote, doesn't disable Windows 10 recently used files, it makes a copy of these files and throws them in a 7zip file. When its finished, it deletes the frequently used list of files, and re-opens with 7zip and puts the files back where they were, so no one knows something sensitive has been used in the meantime.

I did this using part of this script in DOS calling 7zip from command line.

rem *** get rid of the current shortcut list of recently used files ***

del /F /Q %APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations\*

rem *** put back the previously copied set of shortcuts ***

x:\apps\7-zip\7z.exe x x:\internals\scripts\win10recent.7z -o%APPDATA%\Microsoft\Windows\Recent -y

I'm now trying to rewrite this in Python. I'm entirely new to Python, (I'm not an actual programmer by trade) but learned a lot from Googling around to make some portions of the code work so far functional.

Just opening 7zip from within Python I have to do this:-

subprocess.call(['x:\\apps\\7-zip\\7z.exe'])

Here's the bit I am stuck. To call certain generic paths with the % symbols around it, example for %appdata%. I've searched around but I am not sure how I can call this path within Python?

any ideas?

melpomene
  • 84,125
  • 8
  • 85
  • 148
JonathanH
  • 21
  • 1
  • Those things are called environment variables. (Also, that's not DOS.) – melpomene Jul 08 '18 at 21:39
  • 1
    @melpomene probably [`os.path.expandvars()`](https://docs.python.org/3.6/library/os.path.html?highlight=expandvars#os.path.expandvars) is a better fit than just accessing environment variables. And I am sure there is some subtlety but how is this not a DOS (`.bat`) script? (I know recents is a windows things but the script itself?). – AChampion Jul 08 '18 at 21:49
  • Not really related to the asked coding problem, but it might be easier to use new system user accounts or guest accounts in these scenarios... – Capricorn Jul 08 '18 at 21:53
  • I couldn't find a perfect dup, but I added two that show how to use `expandvars` to solve similar problems. If someone knows of a better one that could replace all three of these, that would be great. Or, to the OP: Do those dups together tell you everything you need to solve this, or is it still unclear? – abarnert Jul 08 '18 at 21:54
  • I added another one that is also close but based on UNIX env variables. – AChampion Jul 08 '18 at 21:55
  • @AChampion, it's a batch script, not a DOS script, and since XP all versions of Windows use the CMD shell to run batch scripts. CMD extends the DOS implementation of batch in numerous ways, plus the available command-line utilities are different and not always the same even if they have the same legacy name as MS-DOS utilities. However, CMD does try to be compatible with DOS batch, as does Windows in general (e.g. enabling the creation of 8.3 short names by default and using them for `%TEMP%`, and using hidden environment variables to emulate the per-drive working directory in DOS). – Eryk Sun Jul 09 '18 at 06:27
  • Using `%AppData%` is fine for a simple one-off script, but a more-developed application or library should use [known folders](https://learn.microsoft.com/en-us/windows/desktop/shell/knownfolderid). – Eryk Sun Jul 09 '18 at 06:30
  • The reason why I am learning Python, is when I convert the batch files to exe get constantly flagged by antivirus software becoming a support nightmare, I figured a modern language to do them would be better. I've not yet converted these files to exe yet to test though. The finished files will be running in a portable fashion of USB drive for a charity project. I did search to see if this was answered before, but found nothing obvious. This is why I mentioned I'm a noob please be gentile with me. really appreciate your suggestions!! – JonathanH Jul 09 '18 at 20:22

0 Answers0