Check if directory is being opened in Windows 10 explorer - batch-file

I would like to detect whether or not a folder is "in use" (I am defining that as if it or any sub folder is opened in Windows File Explorer). Is there a way to get the paths of all folders File Explorers opened? I did some reading and some people say that explorer.exe is the process as the task bar/start menu and you have to change an option to separate it? I am fine with doing this as this program is only going to be on one PC.
I know batch isn't the best way to go about doing this, but is this possible?

I don't think you can do that. Even if you enable Launch folder windows in a separate process then the new processes aren't called with the folder path in the command line
It may be possible if you attach a debugger to explorer.exe and read the path somewhere inside the process, but it's not a good way either, and will be very slow

Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each win in AllWindows
WScript.Echo win.LocationUrl
Next
To use in batch
For /f "delims=" %A in ('cscript //nologo "C:\Users\User\Desktop\Bat+Vbs\ListOpenShellWindows.vbs"') Do #Echo %A

Related

Open, wait, save, close, and copy a file using Windows batch file commands

With my .bat I would like to:
open the xlsx file,
waiting 2 min,
close the file with save options
copy this file to another folder.
For now I can copy and paste the file, but I don't know how to open it, with a cmd function, and save it.
Thank you for your help.
My code is :
#echo off
cmd "O:\XXXX\*.*"
xcopy/y "O:\XXXX\*.*" "O:\XXX\"
pause
Marie (TooLong;ToRead) in disjointed comments
I suggested, A simpler alternative method to do what you need on this
occasion is to use a simple command line tool see Orlandos Sendkeys
Utility (the example is almost what you want to do)
download sendkeys from cpap.com.br/orlando
see how the demo runs
open excel with a blank sheet and at a CMD> run this demo string
SendKeys.exe 1.5 10 "Microsoft Excel" "Hello!~{PAUSE 2}After 2s.~{PAUSE 2}%(FS)~"
adapt to your own version of excel keys since the %(FS) is ALT File Save in English
you replied
#KJ Thank you, KJ, unfortunately I can't download Orlando with my PC.
So we continue to doing it in a more dirty fashion, but you still need a means to save the file by invoking an autosave which would most easily be done using an extended excel macro in your source .xlsm, anyway
after all these changes your non working file should now be replaced in your question as
#echo off
start "Excel Running" /MIN EXCEL.EXE "\\XXX\Fichier.xlsm"
REM add a delay of **2 minutes !** whilst sheet recalculates before saving a copy
timeout 120
REM copying a file that has NOT been saved using keys at this point will NOT
REM be what you really need to solve your problem unless you use a macro ?
REM see Later
xcopy/y "\\XXX\Dossier_avant*.*" "\\XXX\Dossier_apres\"
REM add a 3 second delay to check above worked but is not really needed
timeout 3
REM temporary for debugging. Later just REM it out
TASKLIST /M |Find /i "exce"
REM this line should be working with either a SUCCESS: or ERROR:
TASKKILL /T /F /IM excel.exe
REM keep this line for seeing errors above, once happy, it can become REM PAUSE
PAUSE
I think that IF you are constrained (by IT policy) to the command line it is best you write your own autosaving macro, however, MY problem is I dont know if you need it for more than one input.xlsm.
So save this as OpenRunSaveExit.vbs in your working folder where your .bat is. There is a reason I did NOT use spaces or & in the name for a later step.
Set WshShell = WScript.CreateObject("WScript.Shell")
' You may need to include the path to excel.exe if it is a portable version like mine
WshShell.Run "EXCEL.EXE "+"\\XXX\Fichier.xlsm", 9
' 120000 milli-seconds = 2 minutes
WScript.Sleep 120000
' These are the English key combinations for ALT+File+Save . SO alter or remove if not needed
WshShell.SendKeys "%FS"
' These are the English key combinations for ALT+File+eXit . SO alter if needed for french excel
WshShell.SendKeys "%FX"
' Lets us wait 2 seconds for clean closure
WScript.Sleep 2000
As Peter has pointed out in his answer you need to /WAIT before xcopy and depending on how your vbs file handling is set-up you may not need Wscript in the start line
NOW replace your .bat with this
#echo off
start /WAIT Wscript OpenRunSaveExit.vbs
xcopy/y "\\XXX\Dossier_avant*.*" "\\XXX\Dossier_apres\"
pause
And check it runs without the need for taskkill.
Finally why use a 2-4 line .bat since a desktop shortcut would potentially be easier to use. So make a shortcut for the .vbs file (right click the .vbs, and in English its Create Shortcut) and wherever it is built move it to your desktop.
Then change the properties like this (where & has a special meaning so the .vbs filename must NOT have spaces or &.)
%comspec% /c "start /wait wscript.exe OpenRunSaveExit.vbs & xcopy/y "\\XXX\Dossier_avant*.*" "\\XXX\Dossier_apres\" & pause"
P.S. I forgot to add Peters start / wait in this image until later
You can't interact with Microsoft Excel (or most of other programs) through Batch unless they provide such an interface. There is however an option to do it with VBS i.e. via an interface Microsoft Excel supports for interacting with that software.
For just opening the program check start command e.g.:
start /B excel.exe <filename>
then you can wait for the user to both edit and save the file for two minutes or also utilize pause if you don't want to introduce a race condition between saving and copying with xcopy.
Alternatively use start like this:
start /WAIT /B excel.exe <filename>
so the opened file blocks the operation and once it closes (no edit or saving by the user guaranteed) then it'll unblock and xcopy would take place without any time-dependent feature.

Need Batch file help searching for specific string being created in another rolling open session?

Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman

Opening Multiple File Explorer Windows in Different Locations on the Desktop using cmd

So, I'm trying to use a batch file to open various instances of Windows File explorer in various locations on my desktop instead of them all starting right on top of each other, which is annoying. I've had no luck Googling this, so what can I incorporate into my batch script that will force each window to open in a specific location and at a certain size on my desktop? I'm not afraid to use plugins or 3rd party solutions.
This is an example of the script I have so far, which just opens them all on top of each other:
#echo off
start explorer "M:\2) Mathematical Innovations"
start explorer "This PC\3) Pictures"
start explorer "D:\5) Projects\Ae"
What should I do?
Setting window X, Y, W, and H with specific coordinates and dimensions requires programmatic access to SetWindowPos, which would require you to compile and link a C# program or similar.
As an alternative, you could launch all your Explorer windows, then call the WSH Shell.Application COM object's TileHorizontally() or TileVertically() method to let Windows figure out how to tile them. You can do it with a PowerShell one-liner within your batch script like this:
#echo off
setlocal
start explorer "M:\2) Mathematical Innovations"
start explorer "This PC\3) Pictures"
start explorer "D:\5) Projects\Ae"
rem // pause for 3 seconds to allow the windows to appear
timeout /t 3 >NUL
powershell "(new-object -COM 'Shell.Application').TileVertically()"
Great script, only comment is the batch file window was getting included so first just changed the shortcut to run minimized. But then found the below that is even better.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing

WshShell.run and major problems in Windows 7

This is a VBScript that I would like to improve. I would like four things :
1) Add a line that would rename the extension cleanup.dll to cleanup.exe, so as it can be called by the WshShell.run and executed (hidden).
2) The way it is written just below, the script opens two screens : the screen of the cleanup.exe and a blank screen, which should be hidden for the user and it is not what is happening ! How to hide the second screen ? I want to run it invisibly (the user cannot close or manipulate the second screen. It will be closed via code that is inside the cleanup.exe).**NOTE : The code below works perfectly in Windows XP, but not on Windows 7. How to make it work in all Windows platforms ?
VBSCRIPT "Second.vbs"
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
WshShell.Run "c:\cleanup.exe", 0, TRUE
Set WshShell = Nothing
BATCH "Master.bat"
#echo off
wscript Second.vbs
exit /b
3) Is there a good and reliable software to convert from VBS to EXE ?
4) The other problem I am having is that the command line below does not yield results.Must I use the second pard of the code below instead ? Why ??
Suppose that my Batch file is in located in drive f:\
If I double click on it, my screen should be then populated with information extracted from the TXT file, which actually resides in drive c:\
#echo off
set DRV=C:\August\MyProgram
cd\
cd %DRV%
type test.txt & pause>nul
#echo off
set DRV=C:\August\MyProgram
cd\
c:
cd %DRV%
type test.txt & pause>nul
Thank you in advance for the explanations and solutions
Why run with batch, vbscript is more powerfull and offers more controll.
about the visible console window
WshShell.Run "c:\cleanup.exe", 0, TRUE should hide the console while running and waits before continuing
Make sure you start your script with wscript.exe, not cscript.exe and don't use any wscript.echo
Renaming a file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
about the batch cd, practice this in a console window
cd never changes to a drive, only to another map, drive: changes the active drive
d: => d:\>
c: => c:\> (so now if you are on c:\)
cd d:\test =>c:\ (changes your active map on d: to d:\test but since your c: drive is still the active drive you see nothing happening)
d: => d:\test (change drive to d:, you do see that the active map on drive d: is d:\test (at least with the default prompt)

batch file to copy files to another location?

Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.

Resources