LesFerch / WinSetView

Globally Set Explorer Folder Views
MIT License
1.08k stars 46 forks source link

Save WinSetView.ps1 parameters to a file #11

Closed jglandorf closed 3 years ago

jglandorf commented 3 years ago

(Feature request.) Create a file containing the parameters to WinSetView.ps1. This would allow WinSetView.ps1 to be run on another device, without user intervention, from a batch file. E.g.:

  rem Use https://github.com/LesFerch/WinSetView to generate parameters for their PowerShell script 
  rem WinSetViewParams.txt and WinSetView.ps1 reside in the folder "%USERPROFILE%\scripts\".
  set /p params=<"%USERPROFILE%\scripts\WinSetViewParams.txt"
  Powershell.exe -ExecutionPolicy Bypass "%USERPROFILE%\scripts\WinSetView.ps1" %params%

I've tested the following modification to WinSetView.hta; it creates a parameter file %APPDATA%\WinSetView\WinSetViewParams.txt:

Sub RunScript
  CmdLine = "Powershell.exe -ExecutionPolicy Bypass " & Chr(34) & MyPath & "WinSetView.ps1" & Chr(34) & " " & Params
  Dim CmdFile
  set CmdFile = oFSO.CreateTextFile(AppData & "\WinSetViewParams.txt")
  CmdFile.Write Params 
  CmdFile.Close()
  oShell.Run CmdLine,Visible,False
  Window.Close
End Sub
LesFerch commented 3 years ago

Great idea and thanks for the code! I'll add that into the next update.

jglandorf commented 3 years ago

Thanks for writing this awesome project! --Joe

On Wed, Apr 21, 2021, 1:38 PM LesFerch @.***> wrote:

Great idea and thanks for the code! I'll add that into the next update.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/LesFerch/WinSetView/issues/11#issuecomment-824238059, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACAHOFA3FJISYAIZKLL6353TJ4EQHANCNFSM43KQG6KA .

GitHubRulesOK commented 3 years ago

@LesFerch Kudos to @jglandorf I added to hta (changed CmdFile to ParamFile for clarity)

Dim Count, Pick, Language, oOption, LangCount, LangLast, TaggedHTML, LangIndex, SubMode, ParamFile

added here the last extra line

'Set up paths
If Not oFSO.FolderExists(AppData) Then oFSO.CreateFolder(AppData)
CSVFile = AppData & "\WinSetView.csv"
ParamFile = AppData & "\WinSetViewParams.txt"

also modified this block to reflect the "prior art" but dont know how correct that cut and paste is

Sub RunScript
  CmdLine = "Powershell.exe -ExecutionPolicy Bypass -File " & Chr(34) & MyPath & "WinSetView.ps1" & Chr(34) & " " & Params
  oStream.Open
  oStream.WriteText Params
  oStream.SaveToFile ParamFile,2
  oStream.Close
  oShell.Run CmdLine,Visible,False
  Window.Close
End Sub

Anyway AFAIK my portable usage works ok

Unattended.cmd

@echo off & set appdata=%~dp0
set appdata=%appdata:~0,-1%
Powershell.exe -ExecutionPolicy Bypass -File "%~dp0WinSetView.ps1" "%~dp0WinSetView\WinSetViewParams.txt"
explorer "%appdata%"
LesFerch commented 3 years ago

That "-File" parameter breaks the command line. The Params don't get passed. I went with suggestion from @SumatraPeter and replaced MyPath with ".\". Fixes the problem of running from a folder with a space in the same and still passes the params.

LesFerch commented 3 years ago

This feature has now been rolled into the code in the Main branch. Please test and let me know if it's OK.

GitHubRulesOK commented 3 years ago

Nicely implemented However an unknowing user could pull the trigger prematurely by running command before hta also at end of run we are faced with NO folders not even the starting work dir so can I suggest a mod to top and tail

WinSetView.cmd


@Echo Off

if not exist ".\AppData\WinSetViewParams.txt" echo Usage:- ^ Run WinSetView.hta to create or update WinSetViewParams.txt & pause & exit

Set /p Params=<".\AppData\WinSetViewParams.txt" Powershell.exe -ExecutionPolicy Bypass .\WinSetView.ps1 %Params% explorer %~dp0

LesFerch commented 3 years ago

Excellent! I removed explorer %~dp0 and instead added Explorer $PSScriptRoot to WinSetView.ps1. That way, the behavior is consistent regardless of how it's launched.

GitHubRulesOK commented 3 years ago

Great but the aim here is to use a previous set of user saved params easily

I find if they are in a subfolder that's not easy to drag and drop

1) It is easier if the Params.txt is saved in the main folder (it is just that one file, rest can stay in the app data) 2) With the suggestion to keep last run by that name (allows for double click - same as before) 3) HOWEVER can also rename a copy to say LesPreferedParams.txt (then can drag and drop as per following)

@Echo Off

if exist ".\WinSetViewParams.txt" goto OK
echo:
echo Usage:- Run WinSetView.hta to create or update WinSetViewParams.txt
echo         Or drop a previously saved/renamed MyParams.txt on this cmd
echo:
pause & exit
:OK
Set /p Params=<".\WinSetViewParams.txt"
if exist ".\%~n1.txt" Set /p Params=<".\%~n1.txt"

Powershell.exe -ExecutionPolicy Bypass .\WinSetView.ps1 %Params%

OR the alternative is to place the above .cmd in the appdata folder but needs ..\WinSetView.ps1

LesFerch commented 3 years ago

Thanks. I like putting the above CMD in the AppData folder. Main branch updated accordingly.

GitHubRulesOK commented 3 years ago

Seems good enough to me So guessing this is closed and version can be released as 1.3.1 / 1.4 ?

LesFerch commented 3 years ago

I'm considering doing it as a 1.31 or 1.32 release. But, I'm working on custom settings for search results, which will be a 1.40 release. I may hold off until that's ready, since that should be anywhere from 1-5 days away.

GitHubRulesOK commented 3 years ago

anywhere from 1-5 days

yes looking at output from https://github.com/microsoft/Windows-classic-samples/blob/master/Samples/Win7Samples/winui/WindowsSearch/WSFromScript/QueryEverything.vbs

that generates thousands of lines to filter

GitHubRulesOK commented 3 years ago

I encountered the same issue running cscript across network as I did here

You may need to note that whilst hta can be run from \ \server\folder the reason I said use mapped drive z: is that hta will not see the support language files nor will ps1 save the settings in such a \ \URL location

LesFerch commented 3 years ago

Yes. I just tried it running from a drive I have attached to my router with a share of \192.168.1.1\Videos. No mapped drive. I see the same issue. The reg backup saved, so Reg.exe was okay with it, but Languages not read and settings not saved. How to detect? Just look for a drive letter in the path?

GitHubRulesOK commented 3 years ago

somewhere (not very helpful) I thought I saw that PS can auto allocate \server\name by translating to first free drive from z: down but cant remember where (could try powerscript -help) first hit on google is not a help but may be a start point https://stackoverflow.com/questions/12488030/getting-a-free-drive-letter

GitHubRulesOK commented 3 years ago

WRONG THREAD As it was raised in "portable use" but related here one reason CMD drops back to a windows dir

'\\ADVENT\Share\WinSet View'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows>
LesFerch commented 3 years ago

It looks like WinSetView.ps1 is fine with the UNC path. WinSetView.hta needed a little fix. It now uses ".\" which is working for me on UNC paths and mapped drives. Now the CMD script is another matter.

Maybe replace the CMD with a VBS script. Working on it...

LesFerch commented 3 years ago

WinSetView.vbs replaces WinSetView.cmd for GUI-less execution. It works on UNC paths. That is, you can RUN the script from a UNC path. The Explorer view for a UNC path will NOT get any new view set with WinSetView. It has to be drive letter mapped to get the same views as local drives.

GitHubRulesOK commented 3 years ago

Great cant test just now as running an errand I had replaced the cmd with a number of %~dps and copy to %temp% as before but the user would need to ignore the UNC error messageand needs @cls & echo off as first line so not very elegant

GitHubRulesOK commented 3 years ago

Testing against uncommon usage such as UNC path with a space suggests all is well just beware some user may yet complain it does not work from their Klingon named cloud gogglebox account on a mac server in the outer galaxy

LesFerch commented 3 years ago

WinSetView v2 uses an INI file to pass all parameters.