-1

I am using the following as a backup for all open folder windows when Windows crashes. It works well when I click on it in Windows File Explorer. It creates the batch file foldersession.bat that can be used for opening the folder windows again.

@echo off
title create backup of currently open folder windows
setlocal enabledelayedexpansion

powershell  @^(^(New-Object -com shell.application^).Windows^(^)^).Document.Folder.Self.Path >> prevfolderpaths.txt

FOR /F "tokens=*" %%f IN (prevfolderpaths.txt) DO (

    set "var=%%f"
    set "firstletters=!var:~0,2!"

    IF "!firstletters!" == "::" ( ECHO start shell:%%~f >> foldersession.bat) ELSE ( ECHO start "" "%%~f" >> foldersession.bat)
)

del "prevfolderpaths.txt"

For example, the created foldersession.bat looks like this:

start "" "C:\Users\sscic\Downloads"  
start "" "C:\Windows\symbolic links\New folder" 
start "" "C:\Users\sscic\Downloads" 

The main batch file works well when I open it by clicking it. The problem is that I tried to run it with Windows Task Scheduler to make the open folder windows backup every few minutes. But it does not work. It creates no foldersession.bat.

I also tried launching it via explorer.exe with using C:\Users\sscic\explorer.exe "C:\Windows\symbolic links\New folder\foldersave.bat to no avail. It is baffling me completely.

No foldersession.bat is created when automating its execution as task.

1
  • it was the start in folder the issue in taks scheduler thanks for the reply Commented Oct 30, 2024 at 2:18

1 Answer 1

0

The code you have submitted is what I call lazy. It uses output files with relative paths, yet it does not define the location from which they are relative. When you run something from Task Scheduler, within the configuration settings for the task, you can input the location of the 'Start in' directory, which will become the current working directory for your script on opening.

Usually when you don't define the current directory, or configure the Start in location, the default will become C:\Windows\System32, and my best guess is that is your problem.

Also, as this is a programming site, not a software configuration one, your code could be rewrote to be more efficient, require no intermediate output file, and be done in just a single line.

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "@((New-Object -COMObject Shell.Application).Windows()).Document.Folder.Self.Path | ForEach-Object {If ($_.StartsWith('::')) {Write-Output \"@Start Shell:$_\"} Else {Write-Output \"@Start `\"`\" `\"$_`\"\"}} | Out-File '.\FolderSession.cmd' -Encoding OEM"

In the code above I changed your .bat file extension to .cmd, because that is the proper extension for batch files created this century.

Sign up to request clarification or add additional context in comments.

1 Comment

it was the start in folder the issue in taks scheduler thanks for the reply – I also updated the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.