Run command with drag and drop onto batch file - batch-file

I want to drag and drop a file onto a batch file in order to run the below command on it. How do I go about running the command on the dropped file?
PotreeConverter.exe <dropped file> -o C:/output -p index

The path of the file, when you drop it on the BATfile, will be returned as a normal %1 argument.
so :
#echo off
PotreeConverter.exe "%~1" -o C:/output -p index
You can use %* if you drop more then 1 file
Example :
#echo off
for %%a in (%*) do echo [%%a] was dropped on me
pause

Following this easy guide.
Create a batch file test.bat with the contents
#echo off
echo The full path of the file is: %1
pause
Drag any file onto it, you will see that %1 is replaced with the full path for that file in quotes.
Now you know how to execute some command that takes a path to a file as an argument:
#echo off
some_command_that_takes_a_path_to_a_file %1

Related

Drag and Drop multiple files onto batch

Please keep in mind that I'm a newb.
I need to drag and drop 244 files with a .tex extension into a batch that then creates a .png that I can edit. Simply selecting them all and dropping them in isn't doing the trick, so someone wrote me a code that I have no idea how to properly use:
for %%f in (*.tex) do c:\python27\python.exe tools/textool.py -x -v -ra %%f
The .tex files are all in the same directory of the batch, which is in C:\users\myname\downloads\folder1\folder2\folder3. Hope you can help.
Try this batch code:
#echo off
if "%~1" == "" goto :EOF
for %%I in ("%~1\*.tex") do C:\python27\python.exe "%~dp0tools\textool.py" -x -v -ra "%%~fI"
The batch file does nothing if called without a parameter.
But in case of batch file is called with a parameter, it expects without verification (could be added) that the (first) parameter specifies a folder path in which all *.tex files should be processed by the Python script.
An alternate solution:
#echo off
if "%~1" == "" goto :EOF
pushd "%~1"
if errorlevel 1 goto :EOF
for %%I in ("*.tex") do C:\python27\python.exe "%~dp0tools\textool.py" -x -v -ra "%%~nxI"
popd
The directory specified as parameter becomes the current working directory for this batch file and the Python script gets just file name with file extension passed via command line parameter.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains:
%~1 ... first parameter without surrounding double quotes
%~dp0 ... drive and path of argument 0 which is path of folder containing the batch file ending with a backslash.
echo /?
for /?
goto /?
if /?
popd /?
pushd /?
Note: It would be much faster to modify the Python script to search itself for all *.tex files in folder path specified on command line as parameter for conversion of each found file matching the pattern to a *.png file.

Specifying relative path to :r command in sqlcmd mode

A) Suppose you have four .sql files (script1.sql,script2.sql,script3.sql,script4.sql ) in say in a folder c:\scripts.
B) Create a main script file (Main.sql) with the following, please note I have given relative path for scripts.
:r script1.sql
:r script2.sql
:r script3.sql
:r script4.sql
Save the Main.sql in c:\scripts itself.
C) Create a batch file named "ExecuteScripts.bat" with the following:-
SQLCMD -E -d<YourDatabaseName> -ic:\Scripts\Main.sql
PAUSE
When I run the batch file, its unable to script1.sql file. When i give full path C:\scripts\script1.sql, it works fine but I don't want to hardcode the path here.
Is it possible to achieve this using sqlcmd?
#echo off
pushd "c:\scripts"
SQLCMD -E -d<YourDatabaseName> -iMain.sql
popd
PAUSE
Or if all the sql scripts are in the same folder as your batch script, then:
#echo off
pushd "%~dp0"
SQLCMD -E -d<YourDatabaseName> -iMain.sql
popd
PAUSE
The last version allows your scripts to run properly, no matter where they reside.
What about using complete paths, but creating file Main.sql dynamically?
Batch file which expects the 4 script files in folder of the batch file and creates there also Main.sql.
#echo off
set "BatchFolder=%~dp0"
( echo :r %BatchFolder%script1.sql
echo :r %BatchFolder%script2.sql
echo :r %BatchFolder%script3.sql
echo :r %BatchFolder%script4.sql
)>"%BatchFolder%Main.sql"
sqlcmd.exe -E -d "YourDatabaseName" -i "%BatchFolder%Main.sql"
set "BatchFolder="
pause
Open a command prompt window and run there call /?. The help of this command is output in the window explaining %~dp0 which means drive and path of argument 0 ending with a backslash without surrounding quotes. Argument 0 on running of a batch file is the name of the batch file.
Running in command prompt window set /? results in getting help of command set displayed, listing on last help page some special environment variables defined dynamic on running a batch file. The first one listed is %CD% which means current directory.
So instead of working with folder of batch file, it is also possible to work with current folder on batch execution.
#echo off
set "CurrentFolder=%CD%\"
( echo :r %CurrentFolder%script1.sql
echo :r %CurrentFolder%script2.sql
echo :r %CurrentFolder%script3.sql
echo :r %CurrentFolder%script4.sql
)>"%CurrentFolder%Main.sql"
sqlcmd.exe -E -d "YourDatabaseName" -i "%CurrentFolder%Main.sql"
set "CurrentFolder="
pause
The folder path referenced by %CD% does not end with a backslash like the folder path returned by %~dp0 which is the reason for backslash after %CD% on second line.

Command Batch Script In Context Menu not working

I have a old batch script from nt/xp that runs from Context Menu. What it does is when I select a folder and run cmd it will create a temp folder in the active folder i right clicked in. Then will run a program to convert all the tiff's in the original folder and output the new images in the temp folder. New that I using windows 7, I'm having problems getting the CMD.exe to open in the working folder. when I use the script and right click it goes to /windows/system32 and not the folder I click on.
here are the reg file and batch to show what I want to do:
REG file:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Folder\shell\BW Comp/OV]
#="B&W Compress/OV"
[HKEY_CLASSES_ROOT\Folder\shell\BW Comp/OV\Command]
#="C:\\Program Files\\ISRU\\bin\\bwcov.cmd"
BATCH file:
mkdir temp
FOR %%j in (*.tif) do mr_file -T -S 128 -C j -Q 3 -K g %%~nj.tif temp\%%~nj.tif
This was a very simple setup but now with window 7 I can't get it to use the working folder in the batch when making DIR or processing images.
Try this batch file:
#echo off
pushd "%~1"
mkdir temp
FOR %%j in (*.tif) do mr_file -T -S 128 -C j -Q 3 -K g "%%~nj.tif" "temp\%%~nj.tif"
popd
if mr_file is a batch file also then it will need call before the name.
This batch file should work in the SENDTO menu also
but the registry file looks odd to me.
Foxidrive, I tried your suggestion and that work creating temp folder using folder I right clicked on.
Here are the new files.
Batch File (I used a new program called make_pry instead of mr_file:
#echo on
pushd "%~1"
Title %~f1
mkdir temp
FOR %%j in (*.tif) do make_pyr %%~nj.tif -TIFF -JPEG -QFACTOR 97 -tile 128 -out temp\%%~nj.tif
And Reg File (this file was also changed and was the only way I could get it to mkdir command to work in batch. If I remove the %1, /a or /c it will not work:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Folder\shell\BWCOV]
#="BW Compress OV"
[HKEY_CLASSES_ROOT\Folder\shell\BWCOV\command]
#="cmd.exe /a /c Inpho_bwcov.cmd %1"

How to create a loop/skip in batch

I am trying to download a chunk of files from an application. The shell command for it is 'go filename download'.
I have a text file containing all the filenames I have to download. All I want to do is to run a script/command such that when the above command is executed
1. the filenames are picked up from the textfile & executed using the above command
2. existing files/unavailable files are skipped
3. the process then continues with the next files in the list
So far I have this idea of using an operator like go $ download & then feed the operator with the text file containing the filenames list. Thanks in advance.
For Windows, you can use for /f to process the file and create a command from it. The following script supergo.cmd shows how this can be done:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "delims=" %%f in (list.txt) do (
echo go "%%f" download
)
endlocal
The following transcripts shows it in operation:
C:\Pax> type list.txt
file1.txt
file number 2.txt
another file.jpg
C:\Pax> supergo
go "file1.txt" download
go "file number 2.txt" download
go "another file.jpg" download
If you're using a shell like bash, you can use sed to create a temporary script from the input file then run that:
#!/bin/bash
sed -e "s/^/echo go '/" -e "s/$/' download/" list.txt >/tmp/tempexec.$$
chmod u+x /tmp/tempexec.$$
. /tmp/tempexec.$$
rm -rf /tmp/tempexec.$$
This puts an echo go ' at the start of each line, a ' download at the end, then marks it executable and executes it.
In both cases (Windows and bash), remove the echo to get it to do the real work.

seeking alteration in batch file

I have a batch file as under:
#echo off
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "C:\Documents and Settings\vipul\Desktop\vipul.zip" files vipul.xls
copy vipul.zip "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy vipul.zip "E:\Valuations\2009"
exit
HERE vipul.xls is the file on my desktop which is to be copied to my briefcase and same is to be ziiped and then sent to E\valu...folder.
Alteration i want here is as under:
every time the file name is getting changed, e.g. it may be sanj.xls or lago.xls and so on. (in place of vipul.xls), so how i can do this?
Just like there is printdir.bat file in xp
You could have this batch file which works for any file with .xlsextension:
#echo off
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "C:\Documents and Settings\vipul\Desktop\worksheets.zip" files *.xls
copy worksheets.zip "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy worksheets.zip "E:\Valuations\2009"
exit
Or, if just one that files can exist in some time:
#echo off
set filename=
if exists vipul.xls set filename=vipul
if exists sanj.xls set filename=sanj
if exists lago.xls set filename=lago
if "%filename%" == "" goto end
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "C:\Documents and Settings\vipul\Desktop\%filename%.zip" files %filename%.xls
copy %filename%.zip "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy %filename%.zip "E:\Valuations\2009"
exit
:end
Its not tested, but this should help you:
#echo off
REM check if supplied file name actaully exists
IF "%1"=="" GOTO END
IF NOT EXIST "%1" GOTO END
REM define output file name. Use supplied files name without extension and add ".zip"
SET OutputPath=C:\Documents and Settings\vipul\Desktop\%~n1.zip
"C:\Program Files\WinZip\WINZIP32.EXE" -min -a -ex "%OutputPath%" files "%1"
copy "%OutputPath%" "C:\Documents and Settings\vipul\Desktop\My briefcase"
copy "%OutputPath%" "E:\Valuations\2009"
:END
This batch uses the first command line parameter (%1) as input for the file to package and copy.
The first IF statements check if the file is valid. The SET command set a variable with the name of the file to create (the zip file). The remaining part is mainly the code you already have, but now uses the variables.
EDIT:
To call the batch programm, lets name it package.bat, use a syntax like this:
package "C:\Documents and Settings\vipul\Desktop\vipul.xls"
or
package "C:\Documents and Settings\vipul\Desktop\sanj.xls"
You can also use drag 'n drop and simply drop the file you want to process on the batch file package.bat to start it.
If it doesn't work, add a comment.
EDIT:
To use this batch file in your send to context menu do the following steps:
save the above code in a file and name package.bat (or anything else you want)
put in a location you want.
create a link to the batch file package.bat (right click on the file, chose create link)
move the created link file to your Send to folder (e.g. C:\Documents and Settings\vipul\SendTo
now you can select any file you want and chose the batch file command from your context menu->send to
Hope that helps.

Resources