Drag and drop files onto a batch script

Below you will find two scripts that can be used to process multiple files (or even folders) by dragging and dropping them onto a batch script.

 @echo off
 :: Use this code stub as the basis for drag drop batch files

 :: You can drag and drop multiple files (or folders)
 :: If files are dropped it will return the long pathname of each file.

 :: If folders are dropped it will return the long pathname of
 :: each folder (but not the files in those folders)

 PUSHD %~dp0   
 IF NOT EXIST short2long.cmd ECHO Error&pause&GOTO :s_end

 IF ["%1"] EQU [""] GOTO :eof
 CALL short2long %1
 :: At this point ECHO %_short2Long%
 :: will display the long pathname of the current file
 ECHO %_short2Long%

 :: Add other commands here

 GOTO :s_next


This second script Short2Long.cmd is called by the first and converts each short filename into a long one. (Place both scripts in the same folder)

:: short2Long.cmd
@echo off
:: Converts short filename to long

IF NOT exist %1 ECHO No such file or folder&goto :eof

SET _filename=%~nx1
SET _ext=%~x1
SET _remainder=%~dp1

IF defined _ext echo ::File with extension&set _fileYN=Y&goto :s_known_type

IF NOT defined _filename echo ::Path with trailing \ &goto :s_known_type

DIR /a:d %1 >nul 2>&1
IF [%errorlevel%] EQU [2] Echo ::File without extension&set _fileYN=Y&goto :s_known_type

Echo ::Path without trailing \
:: so we need to fix the ~dp1 variable and add a trailing \
SET _remainder=%_remainder%%_filename%\


SET _filename
SET _ext
SET _remainder

   FOR /f "tokens=1* delims=\" %%G IN ("%_remainder%") DO set _node=%%G&set _remainder=%%H
   :: Store the drive letter
   IF "%_node:~1,1%"==":" SET _drive=%_node%&goto :s_getnode

   ::Find the long filename equivalent for this node
   FOR /f "tokens=4* delims= " %%G in (
   'dir /x /a:d "%_drive%%_Long_path%\%_node%\.."
   ^| find "/"'
   ) DO call :s_process_node %%G "%%H"
IF defined _remainder GOTO :s_getnode

:: At this point %_Long_path% is the long path
:: and %_filename% is the short filename

:: Add the drive letter
SET _Long_path=%_drive%%_Long_path%

:: Finally add the long filename to the path
IF not defined _fileYN GOTO :s_finish

FOR /f "tokens=* delims= " %%G IN ('dir /b "%_Long_path%\%_filename%"') DO (
    SET _Long_path=%_Long_path%\%%G)
GOTO :s_finish

   :: Now %1 is short filename and %2 is long filename(if there is one)
   :: Next line makes sure we have an exact match
   IF /i ["%_node%"] NEQ ["%1"] goto :eof

   SET _shortnode=%1
   SET _Longnode=%2

   :: If node is actually 8.3 - dir/x won't include a long filename so
   IF [%_Longnode%] EQU [""] SET _Longnode=%_shortnode%&goto :s_store_node
      :: Remove quotes
      SET _Longnode=###%_Longnode%###
      SET _Longnode=%_Longnode:"###=%
      SET _Longnode=%_Longnode:###"=%
      SET _Longnode=%_Longnode:###=%
   :: Store this node
   SET _Long_path=%_Long_path%\%_Longnode%
GOTO :eof

   ECHO short path is %1
   ECHO long  path is %_Long_path%
   ENDLOCAL&set _short2Long=%_Long_path%

This works for files with or without a file extension: FILE or FILE.TXT
However drag and drop of a folder won't work if the folder has an extension: MYFOLDER.BAD

It is possible that filenames with some puctuation characters may cause the script to fail.


Long filename issues

© Copyright SS64.com 1999-2015
Some rights reserved