Move to New folder context menu

Discussion in 'Scripting' started by Thomas Dubreuil, Apr 15, 2019.

  1. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    #1 Thomas Dubreuil, Apr 15, 2019
    Last edited: Apr 21, 2019
    I recently explored NirCmd options and it gave me some ideas to simplify repetitive task.
    or things that should have been implemented since long time...
    Like moving a file/folder or selection to a new folder, why doing this in 3 steps? create folder, copy, paste.

    Part1 : Using SendTo menu.
    It is done with a simple enough script and a shortcut placed in "send to" menu.
    Type shell:sendto , or %AppData%\Microsoft\Windows\SendTo
    and place script shortcut there (minimized)

    Code:
    @echo off
    
    cd /d "%~dp1"
    set "NF=New folder"
    set "Dest=%~dp1%NF%"
    
    if not exist "%Dest%" goto :MoveTask
    :: incremental folder naming
        setlocal enableDelayedExpansion
        set "baseName=New folder ("
        set "n=1"
        for /f "delims=" %%F in (
          '2^>nul dir /b /ad "%baseName%*)"^|findstr /xri /c:"%baseName%[0-9]*)"'
        ) do (
          set "name=%%F"
          set "name=!name:*%baseName%=!"
          set "name=!name:)=!"
          echo !name!
          if !name! gtr !n! set "n=!name!"
        )
        set /a n+=1
        set "NF=%baseName%%n%"
        set "Dest=%~dp1%NF%)"
    
    :MoveTask
        mkdir "%Dest%" >NUL 2>&1
        for %%A in (%*) do (
            move %%A  "%Dest%"
            ) >NUL 2>&1
    exit /b
    Bonus:
    2 variations, first script will name the "new folder" like item selected, second will show a prompt to type the name yourself...

    Code:
    @echo off
    
    set "Dest=%~dpn1"
        cd /d "%~dp1"
        if exist "%Dest%" goto :eof
        mkdir "%Dest%" >NUL 2>&1
        for %%A in (%*) do (
            move %%A  "%Dest%"
            ) >NUL 2>&1
    exit /b
    Create shortcut with "normal window" for this one (to show the prompt)
    Code:
    @echo off
    
    mode 100,3
    cd /d "%~dp1"
    echo Type folder name and press "Enter":
    set /p "NF="
        mkdir "%NF%"
        for %%A in (%*) do (
            move %%A  "%NF%"
            ) >NUL 2>&1
    exit /b
    Part 2 : Directly from context menu
    Now, what if I want to make a "simpler" button in context menu, not using the sendto menu to move my selection.
    I realized it's much harder, because from context menu selected files are processed one at a time, and so you can not pipe selection inside the script with "for %%" like the other scripts,
    since it launches script for each file one by one, creating more than 1 folder or other weird behavior.

    That was tricky but I successfully got around this using date and time comparison.
    In short, same creation time (in second) as current time= do not create new folder and move file to last folder instead.
    Had to learn well how to use WMIC command cause good old DIR doesn't give you folder creation time in seconds, and if you have to do the task repetitively, then...

    Disclaimer: Use NirCmd or NSudo to hide cmd shell (see bonus 2)

    Code:
    @echo off
    
    cd /d "%~dp1"
    set "NF=New folder"
    set "Dest=%~dp1%NF%"
    
    if not exist "%Dest%" mkdir "%Dest%" & goto :MoveTask
    
        for /f "usebackq tokens=1 delims=." %%# in (`"WMIC path Win32_Directory WHERE name='%Dest:\=\\%' get creationdate"`) do (
            for /f %%@ in ("%%#") do set "FolderDate=%%@"
        )
    
        for /f "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set LDT=%%j
        set LDT=%LDT:~0,4%%LDT:~4,2%%LDT:~6,2%%LDT:~8,2%:%LDT:~10,2%:%LDT:~12,2%
        set LDT=%LDT::=%
    
    if "%FolderDate%" == "%LDT%" goto :MoveTask
    
        setlocal enableDelayedExpansion
        set "baseName=New folder ("
        set "n=1"
        for /f "delims=" %%F in (
          '2^>nul dir /b /ad "%baseName%*)"^|findstr /xri /c:"%baseName%[0-9]*)"'
        ) do (
          set "name=%%F"
          set "name=!name:*%baseName%=!"
          set "name=!name:)=!"
          echo !name!
          if !name! gtr !n! set "n=!name!"
        )
        set /a n+=1
        set "NF=%baseName%%n%"
        set "Dest=%~dp1%NF%)"
    
        for /f "usebackq tokens=1 delims=." %%# in (`"WMIC path Win32_Directory WHERE name='%Dest:\=\\%' get creationdate"`) do (
            for /f %%@ in ("%%#") do set "FolderDate=%%@"
        )
    
    if "%FolderDate%" == "%LDT%" goto :MoveTask
    if not exist "%Dest%" mkdir "%Dest%" & goto :MoveTask
    
    :MoveTask
        for %%A in (%*) do (
            move %%A  "%Dest%"
        ) >NUL 2>&1
    exit /b
    Bonus 2 : Add all this (SendTo new file and Move to new file) to your context menu...
    Code:
    @echo off
    
    echo Enter NirCmd path here:
        set /p "NirCmdFolder="
    echo Enter scripts path here:
        set /p "ScriptPath="
    echo ADD Send To New Folder Capabilities to explorer Context Menu
        PowerShell -NoProfile -ExecutionPolicy Bypass "$s=(New-Object -COM WScript.Shell).CreateShortcut('%AppData%\Microsoft\Windows\SendTo\New folder (create new folder).lnk');$s.TargetPath='%ScriptPath%\SendtoNewFolder.bat';$s.WorkingDirectory='%AppData%\Microsoft\Windows\SendTo';$s.WindowStyle=7;$s.Description='Move selection to a new folder';$s.IconLocation='%SystemRoot%\System32\SHELL32.dll,4';$s.Save()" >NUL 2>&1
        PowerShell -NoProfile -ExecutionPolicy Bypass "$s=(New-Object -COM WScript.Shell).CreateShortcut('%AppData%\Microsoft\Windows\SendTo\New folder (named as selection).lnk');$s.TargetPath='%ScriptPath%\SendtoFolderName.bat';$s.WorkingDirectory='%AppData%\Microsoft\Windows\SendTo';$s.WindowStyle=7;$s.Description='Move selection to a new folder, named as first item selected.';$s.IconLocation='%SystemRoot%\System32\SHELL32.dll,4';$s.Save()" >NUL 2>&1
        PowerShell -NoProfile -ExecutionPolicy Bypass "$s=(New-Object -COM WScript.Shell).CreateShortcut('%AppData%\Microsoft\Windows\SendTo\New folder (type name).lnk');$s.TargetPath='%ScriptPath%\SendtoFolder.bat';$s.WorkingDirectory='%AppData%\Microsoft\Windows\SendTo';$s.Description='Prompt for a folder name and move selection to that folder';$s.IconLocation='%SystemRoot%\System32\SHELL32.dll,4';$s.Save()" >NUL 2>&1
        reg add "HKCR\CLSID\{7BA4C740-9E81-11CF-99D3-00AA004AE837}" /v "flags" /t REG_DWORD /d "2" /f >NUL 2>&1
    echo ADD Move to New Folder Button to Explorer Context Menu with script
    :: Requires provided script and nircmd to hide window: since files are processed separately from "regular" context menu, it will open a cmd window for each selected file.
        reg delete "HKCR\*\shell\03_MoveTo" /f >NUL 2>&1
            reg add "HKCR\*\shell\03_MoveTo" /ve /t REG_SZ /d "Move to new folder" /f >NUL 2>&1
            reg add "HKCR\*\shell\03_MoveTo" /v "Icon" /t REG_SZ /d "imageres.dll,176" /f >NUL 2>&1
            reg add "HKCR\*\shell\03_MoveTo" /v "SeparatorAfter" /t REG_DWORD /d "1" /f >NUL 2>&1
            reg add "HKCR\*\shell\03_MoveTo" /v "AppliesTo" /t REG_SZ /d "NOT (System.ItemPathDisplay:=\"C:\" OR System.ItemPathDisplay:=\"C:\Users\" OR System.ItemPathDisplay:=\"%USERPROFILE%\" OR System.ItemPathDisplay:=\"C:\ProgramData\" OR System.ItemPathDisplay:=\"C:\Program Files\" OR System.ItemPathDisplay:=\"C:\Program Files (x86)\" OR System.ItemPathDisplay:=\"C:\Windows\" OR System.ItemPathDisplay:=\"C:\Windows\System32\")" /f >NUL 2>&1
            reg add "HKCR\*\shell\03_MoveTo\command" /ve /t REG_SZ /d "\"%NirCmdFolder%\nircmd.exe\" exec hide \"%ScriptPath%\MovetoNewFolder.bat\" \"%%V\"" /f >NUL 2>&1
    exit /b
    11-12  - Move Selection to New Folder (SendTo Menu and Move Button).PNG

    Soon adding this to my (growing) tweak project
    That's it for today!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. wintergrey

    wintergrey MDL Novice

    Apr 18, 2019
    1
    1
    0
    Great solution!
    I know this is something I used to be constantly frustrated by when handling files (along with flattening the contents of a folder down by one towards the root).
    Personally, and no disrespect at all meant to your work, I've been using files-2-folder (not the inferior filestofolder) to quench my same desire for frustration sensation minimalisation.
    It's a shell extension (for the context menu) which auto names the folder from the filename (minus ext) or, when multiple files selected, pops up a simple gui with quick useful naming options.
    It can be found on the net at dcmembers from coder skwire.

    Thanks!
     
  3. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    #3 Thomas Dubreuil, Apr 19, 2019
    Last edited: Apr 20, 2019
    (OP)
    yes! have been using it as well...I think I tried every single solution :)
    I didn't want a popup but direct task (those can be repetitive) and the smallest footprint.

    That's why I made different scripts and options in sendto menu : folder named as file, new folder, choose folder name...
    and a context menu button to copy selection to new folder in one click.

    11-12  - Move Selection to New Folder (SendTo Menu and Move Button).PNG

    Same story to be able to copy clipboard content to a file, I ended up making that screesnip script, because paste to file and alike all open a popup first.
    This kind of things should be implemented in the OS... I believe you can move selection to new folder on mac os X
    And for clipboard, while I can CTRL+V clipboard content in notepad or paint it is not natively possible on desktop or folder, which would have been more useful for me.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    Nice. Simplifies creating, renaming and moving to new folder. Thanks
     
  5. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,390
    1,322
    120
    Could you tell me if the above given script
    "Move to New folder context menu"
    means the same as the Windows default context menu features
    "Move To Folder" and "Copy To Folder".
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,358
    2,267
    60
    #6 freddie-o, Apr 21, 2019
    Last edited: Apr 21, 2019
    No it's different. This script creates a new folder, renames the folder same as your file name, then moves your file to the newly created folder.

    I modified it a little. Named the batch script. "Create_Folder_and_Move.bat"

    Then added it to the right click context menu

    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\*\shell\Create Folder and Move]
    "Icon"="%SystemRoot%\\System32\\shell32.dll,4"
    
    [HKEY_CLASSES_ROOT\*\shell\Create Folder and Move\command]
    @="\"D:\\MY APPS\\SCRIPTS\\Create_Folder_and_Move\\Create_Folder_and_Move.bat\" \"%1\""
     
  7. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,390
    1,322
    120
    @freddie-o Thanks, everything is clear. That's what I wanted to know.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...