No announcement yet.

BATCH: get folder path when calling the script from context menu

  • Filter
  • Time
  • Show
Clear All
new posts

  • BATCH: get folder path when calling the script from context menu

    I would like to create an option within the context menu of Windows, that runs a batch script which runs the following file just within the directory that I am browsing from, say windows explorer.

    @echo off
    cd %current directory%
    del *.class > delete.log

    It would be nice if I could log delete.log to another directory, for example, C:\Program Files\custom sript log files\.

    Any idea how to script it?

  • #2
    Re: script

    Did you configured a run command for the batch file in the key "HKEY_CLASSES_ROOT\Directory\shell\<...>\command" or have you copied a shortcut to a "sendto folder" ?

    The variable you can use in the batch for expanding the folder path is %1 (or "%~1").

    try this batch,
    :: for adding this batch to the context menu for folders,
    :: edit the default value in HKCR\Directory\shell\my menu entry\command
    :: cmd.exe /c "title delete *.class files from "%1" &color 9E &"c:\folder name\batch file.cmd""
    @pushD "%~1" ||goto :EOF
    @Set "LogFolder=C:\Program Files\custom sript log files"
    @Set "LogFile=delete.log"
    @if not exist "%LogFolder%" mkdir "%LogFolder%"
    @Del /F /Q /A *.class >> "%LogFolder%\%LogFile%" 2>&1
    When the batch is called via 'SendTo', then just create a simple shortcut to the batch in the SendTo folder - you can enter the Title and Color statements in the batch itself if you like.
    (The idea behind entering the Title on the command line that would be configured in the registry was used as an option for a way to describe what that batch, which is pointed from there, will perform.)

    The Del (or Erase) command does not show you the files that are deleted, just the files that could not be deleted. Therfore for writing a log of the files that are deleted you have to use the /s switch. However the /s makes the script also delete the files from all subfolders - to avoid that you can use a for-do statement like in the sample below,

    @setlocal enableextensions
    @echo\& >>"%LogFolder%\%LogFile%" (@echo.&echo %date% %time%^> %~1)
    @For /f "tokens=*" %%* in ('dir /b /-d *.class') do @erase /S /F /A /Q "%%~*" >>"%LogFolder%\%LogFile%" 2>&1
    Replace the line with the @Del statement in the first script with these three lines.

    EDIT: note 2,
    NonAdministrators do not have permissions to write to the "program files" directory

    Last edited by Rems; 14th July 2011, 19:11.

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts