How to remove dead files from Open Recent list in Sublime Text 3? - sublimetext3

In Sublime Text 3 if I go to:
MENU: File>Open Recent
There are files in that list which are no longer available.
How can I clean that list of dead files?
I don't want to clear the full list, just the dead ones.
Note: I've followed How to increase number of recent files in Sublime Text 3? to get 30 recent files showing in this menu.

Following this post over on the Sublime Forum it can be done by editing the Session.sublime_session file.
For example: I'd edited etc/hosts so I could block myself from watching youtube, but with it being in the recent files list in Sublime it's easy to unblock myself thus I wanted to remove /etc/hosts from my recent file list in Sublime (ST3).
My copy of Sessions.sublime_session is in ~/.config/sublime-text-3/Local/ from which I deleted all lines containing etc/hosts.
The only caveat is that you can't edit that file using Sublime, Sublime should not be running, I used a different text editor.
Final caveat this is the solution for linux, and ST3, for windows and mac I'm guessing there's something similar.

I found out how to do this using this python script here:
https://github.com/STealthy-and-haSTy/SublimeScraps/tree/master/session_cleaner
I don't want to post the python code as I don't have permission to do so but that has been up on GitHub for a couple of years so there should be no worries of it disappearing anytime soon.
I've written a batch file that will start the python file, see the code below. Just name it RunSessionCleaner.cmd and place this in the same folder as the sublime_session_clean.py file.
#echo off & title %~nx0 & color 5F & chcp 65001 >NUL
rem Based on this thread here: https://stackoverflow.com/a/13351373
goto :MY_ROUTINE
:CMD_SIZE
chcp 850 >NUL & set "uiWidth=%1" & set "uiHeight=%2"
mode %uiWidth%,%uiHeight%
if %4==TRUE (set /a "uiHeightBuffer=uiHeight+%3")
if %4==TRUE (powershell.exe -ExecutionPolicy Bypass -Command ^
"&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeightBuffer%;$W.buffersize=$B}")
if %4==FALSE (powershell.exe -ExecutionPolicy Bypass -Command ^
"&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeight%;$W.buffersize=$B}")
chcp 65001 >NUL & goto :EOF
:MY_ROUTINE
call :CMD_SIZE 60 7 222 TRUE
title Cleaning folders, files and projects...
echo Cleaning folders, files and projects...
:: Full path to data folder W:\Apps (Portable)\Sublime Text\Data
:: Full path to python.exe W:\Apps (Portable)\Sublime Text\Data\Packages\User\SessionCleaner\python\python.exe
:: Full path to sublime_session_clean.py W:\Apps (Portable)\Sublime Text\Data\Packages\User\SessionCleaner\sublime_session_clean.py
:: Or like I have done use a relative directory to the Data folder
:: The SsssionClearner.py file is located in: W:\Apps (Portable)\Sublime Text\Data\Packages\User\SessionCleaner
::
:: Change this line below to suit your needs.
".\python\python.exe" "sublime_session_clean.py" --data-dir "..\..\..\..\Data" --workspaces --files --folders
echo/
goto :EXIT_ROUTINE
:EXIT_ROUTINE
rem call :CMD_SIZE 100 10 222 FALSE
title Exiting routine...
echo Exiting routine...
set /p "=" <NUL &
ping localhost -n 3 >NUL & exit
exit
Change line 24 of the code above to suit your paths. You can just use relative paths here like I have done so you can do that too.
This batch file can be clicked on or set up in Task Scheduler to start up when the PC does to clean out all the non-existent files or workspaces.
Please note, Sublime Text needs to be closed in order for this to work.

Related

Start Excel file from Windows batch script in safemode, use default file association

Question Summary:
Can I start Excel file Installer.xlsm from Windows batch script in safemode, without providing EXCEL.EXE installation path?
Details
I have a windows batch script which downloads the latest versions of a family of Excel Add-ins from a remote server, places them in a directory (C:\appname\AddIns) and calls the Excel file Installer.xlsm.
Upon loading, Installer.xlsm executes a VBA macro, which uninstalls older versions of the add-ins and installs their newer version.
Currently I start Installer.xlsm using the command:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm"
What's great about it is that it uses Windows' file association to open Excel, and I don't have to provide the EXCEL.EXE installation path (multiple users with different machine images and MS Office versions).
Now I'd like to load Installer.xlsm in safemode, to make sure that no add-ins are loaded and no other code is run while Installer.xlsm tries to work with the add-ins.
I know I can use "<PathToExcel>excel" /safemode "<PathToXls>Installer.xlsm" as described in this answer, but this method doesn't use Windows' file association and requires that I provide a path.
We have users with various machine images, using different versions of MS Office, so I do not want to get into hardcoding all possible Excel installation locations.
Can I do something of the following form:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm /safemode"
I tried different possible combinations without success. How would you do it?
First I suggest to read the Microsoft documentation page Application Registration. It explains how the installer of an application or an application suite like Microsoft Office should register the installed application(s) so that the executable(s) of the application(s) can be found by other applications.
Recommended is creating under registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
a subkey with name of the executable file like excel.exe with default string value being name of the executable with full path and optionally adding one more string value with name Path containing just the path to the executable. The Path string can but most not exist and it can but must not end with a backslash.
The command START uses also this key to find an application as explained in answer on Where is “START” searching for executables?
The installers of the various versions of Microsoft Office register excel.exe key under this key too.
So the easiest method on Windows Vista and later Windows versions to get installation location of Microsoft Excel is:
#echo off
for /F "skip=1 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
But on Windows XP the output of reg.exe is different and requires for that reason this batch code:
#echo off
for /F "skip=3 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
The different outputs are explained in answer on Read words separated by space & string value also contains space in a batch script in batch code written to get string value of a default string of a registry key containing spaces.
And it is good coding practice to add extra code which handles an error case like registry key does not exist at all because Microsoft Excel is not installed at all.
But is it possible with batch code to do what command START respectively the Windows shell function ShellExecuteEx does on using in a command prompt window the command line?
start "Launching installer file" "C:\Path to file\Installer.xlsm"
Yes, it is possible as the commented batch code below demonstrates.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem First query default string value of HKEY_CLASSES_ROOT\.xlsm from registry.
call :GetDefaultRegValue "HKCR\.xlsm"
rem Is there no key HKEY_CLASSES_ROOT\.xlsm or was the default string empty?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem Get the shell command line used for opening a *.xlsm file.
call :GetDefaultRegValue "HKCR\%RegValue%\shell\open\command"
rem Could the command line not read successfully from Windows registry?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem The command line contains as first string usually enclosed in double
rem quotes EXCEL.EXE with full path enclosed in double quotes. And there
rem can be even more arguments on the command line which are not needed
rem here. The command line below is used to get just first string of
rem the command line which should be EXCEL.EXE with full path.
for %%I in (%RegValue%) do set "RegValue=%%~I" & goto CheckExcelExistence
rem It is not good when both registry queries above fail. This means
rem either Microsoft Excel is not installed at all or a version of
rem Excel is installed which does not support *.xlsm files like Excel
rem of MS Office 2003, MS Office 2000 or MS Office 97.
rem However, perhaps just *.xlsm is not correct registered and therefore
rem get full path to excel.exe from application registration key.
:GetFromAppPaths
call :GetDefaultRegValue "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe"
if defined RegValue goto CheckExcelExistence
echo Failed to determine installation location of Microsoft Excel.
echo/
endlocal
pause
goto :EOF
:CheckExcelExistence
SET RegValue
rem Remove surrounding double quotes if the Excel executable file name
rem read from Windows registry is still enclosed in double quotes.
set "RegValue=%RegValue:"=%"
if exist "%RegValue%" goto :RunInstall
echo Registered "%RegValue%" does not exist.
echo/
endlocal
pause
goto :EOF
:RunInstall
SET RegValue
ECHO start "Launching installer file" /wait "%RegValue%" "%~dp0Installer.xlsm" /safemode
endlocal
goto :EOF
rem This subroutine queries from Windows registry the default string value of
rem the key passed to the subroutine as first and only parameter and assigns
rem this value to environment variable RegValue. Environment variable RegValue
rem is deleted and therefore is not defined after subroutine exits on failure
rem to get the registry value or when the default value is an empty string.
rem This subroutine works for Windows XP and all later versions of Windows.
:GetDefaultRegValue
set "TypeToken=2"
:Reg3Run
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if not "%%~B" == "" (
set "RegValue=%%B"
goto :EOF
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto Reg3Run
)
)
set "RegValue="
goto :EOF
This batch code is just a demonstration. It does not start Excel when really found. Instead it just outputs the command line which would start Excel because of ECHO left of start ... in block below label RunInstall.
Further this batch code contains 4 lines with just SET RegValue. Those 4 lines output just the string value queried successfully from Windows registry and stored in environment variable RegValue. Those 4 commands help to understand what happens on execution of the batch file. Those four command lines should be deleted finally from batch file and also the single ECHO written in upper case.
Note: It is quite easy to test what happens if an expected registry key does not exist or its default value is an empty string. Just insert a single character like # before last double quote on a line starting with call :GetDefaultRegValue and the modified registry key is not found anymore.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
setlocal /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg.exe command line with using a separate command process started in background.

How to open a *.xlsx file containing a space in file name found by command DIR in Excel?

I have a batch file that opens an Excel file with a space as well as date in its file name.
For example: Book 1-27Aug2016
Currently, I am having trouble disabling the delimiters so that command start doesn't try to open two files: Book.xlsx and 1-27Aug2016.xlsx
Here is my code:
for /f "delims=*" %%# in ('dir /tw /o-d /b "Book 1-*"') do (start excel %%#& exit)
I referenced the web page in SS64 Windows CMD Shell forum below for disabling/modifying delimiters, but I still have yet to experience success.
For /f documentation
Lastly, once I remove the space from the file name, the batch runs without any issues.
What do I need to modify in the single line batch code to open also an Excel file with a space in file name in Excel?
Open a command prompt window, run in this window for /? and read very carefully all output help pages.
Command FOR with option /F splits up a string by default on spaces/tabs. It can be either used "tokens=*" or "delims=" to avoid this string splitting. The usage of "delims=*" works also for file names because the name of a file without or with path can't contain an asterisk. But "delims=*" is usually not used to prevent splitting up a string into tokens because a string read from a text file, output of an application or an environment variable could contain 1 or more asterisks as well.
Next run in a command prompt window cmd /? and read at least last output help page on which is explained on which characters in name of a file/folder without or with path the file/folder name string must be enclosed in double quotes. In general it is advisable to enclose file/folder names without/with path always in double quotes.
The command DIR returns with the used options just the names of the files without path and always without surrounding double quotes as it can be seen on running in a command prompt window in directory with Book 1-* files
dir "Book 1-*" /A-D /B /O-D /TW
after first running dir /? to get displayed the help for command DIR.
So used should be:
for /F "delims=" %%# in ('dir "Book 1-*" /A-D /B /O-D /TW 2^>nul') do ( start "" excel.exe "%%#" & exit /B )
2^>nul is 2>nul whereby the redirection operator > is escaped with ^ to apply this redirection on running command DIR instead of being interpreted as redirection of command FOR at an invalid position in command line. The command DIR outputs the error message File not found to handle STDERR if it can't find any file in current directory matching the pattern Book 1-*. This error message is suppressed by redirecting it to device NUL.
It is advisable to specify the application to start with file extension if well known even if the path is not known. Read answer on Where is “START” searching for executables? for an explanation.
In the batch command line above there are after command START also two double quotes before the name of the executable to start. Usage of "" is highly recommended as command START interprets often first double quoted string in arguments list as optional title string. By specifying explicitly an in this case empty title string helps to avoid unexpected application execution. For details on command START run in command prompt window start /?.
And last it is better to use command EXIT with parameter /B to exit just processing of current batch file and not exit entire command process. If this batch file is called with command CALL from another batch file or started from within a command prompt window and command EXIT is used without parameter /B, the processing would not continue on parent batch file respectively command prompt window would be closed by EXIT, too. There is no difference between usage of exit and exit /B if this batch file is executed by a double click on the batch file. For details on command EXIT run in a command prompt window exit /?.
By the way: The command START uses file association registration data if it can find an executable or script with given name to open the specified file with the registered application for the file extension.
So it would be also possible to use:
for /F "delims=" %%# in ('dir "Book 1-*" /A-D /B /O-D /TW 2^>nul') do ( start "" "%%#" & exit /B )
Now command START opens the *.xslx file with whatever application is associated with this file extension as default application for opening it.
Add double quotes around the last variable.
for /f "delims=*" %%# in ('dir /tw /o-d /b "Book 1-*"') do (start excel "%%#"& exit)

Move 300 images from a folder (contains 800 images) to another based on file name list

I need to move 300 images from a folder (contains 800 images) to another folder. The file name list of these 300 images are available in the excel format. Is it possible to move them via programming instead of search the file and move it one by one? Our IT told me he can't separate these files. Do you have any solution? Many thanks in advance!!!
Here's one way of doing this - I am assuming you are on Windows. First, save text file called ListOfImages.txt that contains the names of the images you wish to move - put one image on each line and include the extension. Then, save the following into a file called movefiles.cmd:
#echo off
set Source=C:\Users\YourName\Desktop\moving\MovingFrom
set Target=C:\Users\YourName\Desktop\moving\MovingTo
set FileList=C:\Users\YourName\Desktop\moving\ListOfImages.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do move "%Source%\%%a" "%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
You will want to change the variables for Source, Target, and FileList to match where you have those folders and the ListOfImages.txt on your machine. After you have saved this file (make sure it has the .cmd extension, you should be able to double-click it and it will run the commands in your Command Prompt.
For example, say my MovingFrom folder contains the following:
And I only want to move Image1.png and Image2.png -- then my ListOfImages.txt file would like this:
After running moveFiles.cmd (provided I have changed the necessary variables to point to the right folders/places on my machine), my MovingTo folder should contain the following:
Notice that Image2.png was not moved because it was not listed in the ListOfImages.txt text file.

I would like a script to search within text files a certain pattern of strings and move those files to another directory

I need to know how to perform an advanced search on a text file using tools like notepad++, freecommander and windows if possible. A bat script file will be great.
The problem is that i need to search through about 1000 txt files in a directory. I need to know form those 1000 txt files which have a string in the form of for example "SYR_SHA/245/4". I just want it search for the pattern for example *****_******/*****/****** where * are characters that can change in number.
There must be an _ between the first and second set of characters as seen above in the example.
the script should go through the entire txt file and search for the above pattern. The script should then Move all the results in to a seperate directory
Many Thanks
Create this batch file and copy it into your folder where you have all the 1000 .txt files
Change the yourdestinationdirectory directory and run the batch file
#Echo Off
FindStr /M /R "[a-zA-Z]*_[a-zA-Z]*\/[0-9]*\/[0-9]*" *.Txt > findstr.out
For /F "tokens=*" %%a In (FindStr.out) Do call :move_Rtns %%a
del FindStr.out
Exit /B
:move_rtns
copy %1 yourdestinationdirectory\*
del /Q %1
Exit /B
Please change the Regex according to your requirement, for eg. If you are expecting number and letter together you can replace [a-zA-z] with [a-zA-Z0-9]
Good luck

How to add files/folders to multiple zip files at once with WinRAR?

Is there any way to add files/folders to multiple zip files at once. For example:
Updates:
folder1
folder2
file1
file2
Archives:
archive1.zip
archive2.zip
archive3.zip
I need to add updates to all archives without opening all of them and pasting updates in there.
Any way to do this?
Or is there another archive program that can do this?
This can be done using a batch file and for example WinRAR.
#echo off
if not exist archive*.zip (
echo There are no archive*.zip files to update in
echo.
echo %CD%
echo.
pause
goto :EOF
)
set "ErrorCount=0"
for %%I in (archive*.zip) do (
"%ProgramFiles%\WinRAR\WinRar.exe" u -afzip -cfg- -ep1 -ibck -inul -r -y "%%I" folder1 folder2 file1 file2
if errorlevel 1 (
echo Error on updating %%I
set /A ErrorCount+=1
)
)
if not "%ErrorCount%" == "0" (
echo.
pause
)
set "ErrorCount="
For each archive*.zip file WinRAR is called to update the ZIP file with the two folders and the two files.
The batch processing finishes without printing any message and without pausing if all ZIP files found could be updated successfully. Otherwise the batch file outputs which ZIP file could not be updated for example because of read-only attribute set, and pauses the batch processing before finishing so that the user can read the error message(s).
For details on the WinRAR command u and the used switches open in WinRAR from menu Help the Help topics, open on tab Contents the item Command line mode and read at least the help pages:
Command line syntax
Commands - Alphabetic commands list
Switches - Alphabetic switches list
For understanding the other used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
pause /?
set /?
You can do it by using 7zip command line version
7z a archive1.zip dir\ -v10k -v15k -v2m #listfile.txt
this will do archive all files and folder is in that folder named 'dir'
and first volume will be 10kb, second will be 15kb and others will be 2mb each except last
and you can use a file that has list of all files named.

Resources