I wish to have a batch file in my SendTo menu (win7). I want to right click a bunch of files and send them to this batch file which will then concatenate their file names into one long string with a + sign between, like so
filepath1+filepath2+filepath3....
Something like this is getting close but I don't know how to separate each parameter
for %%i in (%*) do echo %* > names.txt
#echo off
setlocal enabledelayedexpansion
FOR %%A IN (%*) DO (
set VAR=!VAR!+%%A
)
echo %VAR:~1% > v:\1\names.txt
Related
I created a batch file that will echo or print the path of a file. Apparently, I am unable to print the path when I send multiple files as parameters to the said batch file.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set string=%1
set string=!string:\=%%5C!
#echo %string% > D:\Playground\test.txt
Any help would be much appreciated. If there's anything unclear please let me know. Thanks.
Note: I send files to the batch file using the procedure below:
Right Click File
Send to
Choose the menu item that I created in sendTo that will pass the file to batch file
I don't know if this info helps but I didn't want to leave it out of my question.
%* will catch each input string passed separated by whitespace, enabledelayedexpansion should be used as you do set inside of a code block (). We also enclose the variables, including variable name with double quotes:
#echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
set "string=%%~a"
set "string=!string:\=%%5C!"
echo !string! >> D:\Playground\test.txt
)
As you can see, you need to use the for loop to iterate each of the input strings i.e %1 %2 and %3 etc.
As a Side note You can also drag and drop files onto the batch file to get results.
EDIT
Added the quote removal as requested set "string=!string:"=!" but note that using the strings as paths without quoting them will cause an issue in future if the paths contains whitespace.
To pipe to file without newline:
#echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
set "string=%%~a"
set "string=!string:\=%%5C!"
echo|set /p="!string! " >> D:\Playground\test.txt
)
So I know how to save strings to a text file as a list from batch by using
set /p Myvar1="Enter your variable name/value: "
set /p Myvar2="Enter your variable name/value: "
set /p Myvar3="Enter your variable name/value: "
And then append to a text file
echo %Myvar1% %Myvar2% %Myvar3% >> Mylist.txt
So when I open the text file through the batch file, it can be displayed
as a list:
SET "variables="
ECHO =================================================
ECHO My list of stuff
ECHO =================================================
< Mylist.txt (
set /p MyVar1=
set /p MyVar2=
set /p MyVar3=
)
::set line
ECHO - [0] - %Myvar1%
ECHO - [1] - %Myvar1%
ECHO - [2] - %Myvar1%
Now the problem is that:
For each new line on the Mylist.txt text file I have to manually add lines on the batch file. On the provided example the batch is setup so it displays 3 lines of text from the text file. If the text file has 10 lines, it will only show the first 3 lines because that is what is specified. So I would like to accomplish the opposite of this script.
The batch file should be able to:
Batch file reads Mylist.txt.
For each line in Mylist.txt file the batch file creates a "numerated variable".
Each "numerated variable" can be addressable so the user can be prompted to select one of the options on the list 1, 2, 3, 4, etc.
The easiest method to save variable names and their values to a file is redirecting the output of command SET to a file.
set My >"%APPDATA%\MyVariables.txt"
This simple line saves all environment variables starting with My in name with name and value into file MyVariables.txt in application data directory of current user.
And following command line can be used in a batch file to reload those environment variables from file:
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I"
Same command line for usage directly in a command prompt window:
for /F "usebackq delims=" %I in ("%APPDATA%\MyVariables.txt") do set "%I"
A little bit more secure would be checking if each line in the file MyVariables.txt really starts with My as expected and so was not manipulated by someone who wants to override for example PATH by adding manually to file MyVariables.txt a line starting with PATH=.
It might be useful to know how many variables were loaded from file. This can be achieved with a simple extension:
set "VariablesCount=0"
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I" & set /A VariablesCount+=1
Of course if there is an environment variable MyVariablesCount starting with My then this variable with its value would be also saved into the file and reloaded from file which would make it unnecessary to count the number of variables set from file.
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.
for /?
set /?
Read also the Microsoft article about Using Command Redirection Operators.
simple thing with a for loop plus a counter:
setlocal enabledelayedexpansion
set i=0
for /f "delims=" %%a in (mylist.txt) do (
set /a i+=1
set var[!i!]=%%a
)
echo number of variables: %i%:
set var[
echo ---------
for /l %%a in (1,1,%i%) do echo variable %%a = %var[%%a]%
I have a batch file, which I will post below, I am trying to have it copy the first line in a file and then stores it in a variable.
#echo off
setlocal enabledelayedexpansion
set SEPARATOR=/
set filecontent=
for /f "delims=" %%a in ("MavenInstructions.txt") do (
set currentline=%%a
set filecontent=!filecontent!%SEPARATOR%!currentline!
)
echo The file contents are: %filecontent%
echo %filecontent%
pause
The first line of the MavenInstructions.txt is TEST LINE but what I get when I run my batch file is:
The file contents are: /MavenInstructions.txt
/MavenInstructions.txt
Press any key to continue . . .
for /f "usebackqdelims=" %%a in ("MavenInstructions.txt") do (
usebackq is required if the filename is "quoted".
btw - set filecontent=!filecontent!%SEPARATOR%%%a
would suffice.
I am trying to achieve some file control with batch processing.
The base idea is a text file is supplied which contains a list of files and folder paths.
What I then need to do is is move these names files to another location referencing the same folder structure. The folder paths also vary some can be 2 deep others up to 4.
The reason for batch is that it can be hundreds of files at a time thus making manual processing time consuming and hard.
An example of the text file input is:
copier\spc240\parts.cat\M095_M096_CHN_V1.05.pdf
copier\spc240\parts.cat\M095_M096_NA_V1.06.pdf
copier\spc240\parts.cat\M099_M100_NA_V1.10.pdf
copier\spc240\parts.cat\M100_CHN_V1.10.pdf
options\df3090\D779_21_V1.01.pdf
options\pb3190\D747_27_V1.00.pdf
Below is what I have managed to do so far:
#echo off
set "file=deletes.txt"
set /A i=0
for /F "usebackq delims=*" %%a in ("%file%") do (
set /A i+=1
call set array[%%i%%]=%%a
call set n=%%i%%
)
for /L %%i in (1,1,%n%) do (
echo|set /p="move %%array[%%i]%% moved\%%array[%%i]%%">>Test.txt
echo.>>Test.txt
)
echo pause>>Test.txt
rename Test.txt RunMe2Move.bat
pause
What this currently does for me is make a batch file that could be run seperately, however this is where I stumble down. This errors as the destination folders do not exist and I am not familiar enough with arrays in batch to split the text lines to enable me to use the mkdir for the missing folders or trim the file names.
In essence I am trying to get the following lines of code either processed or output (using copier\spc240\parts.cat\M095_M096_CHN_V1.05.pdf as example) for each line in the text file.
mkdir moved\copier\spc240\parts.cat\
move copier\spc240\parts.cat\M095_M096_CHN_V1.05.pdf moved\copier\spc240\parts.cat\
Ultimately I am trying to get all of this done in one file if possible, the output is just there so I can check things through.
Can anyone help?
for /f "delims=" %%a in (%file%) do (
SET "fpath=%%~a"
SETLOCAL enableDelayedExpansion
SET "fpath=!fpath:%%~nxa=!"
md "moved\!fpath!"
move "%%~a" "moved\!fpath!"
ENDLOCAL
)
I have a folder full of images which have a format filename.com_XXX_IMG_000.jpg. The issue comes because the file name has a .com in it, it confuses the the software that I am using to upload it to a webspace.
I need to create a batch file that gets all the images in a folder and renames all of them from filename.com_XXX_IMG_000.jpg => filename_XXX_IMG_000.jpg.
Any help would be greatful, thanks in advance.
Since you want to do it in a batch file:
#echo off
for /f "delims=. tokens=1,2,3" %%f in ('dir /b *.jpg') do (
setlocal enabledelayedexpansion
set part=%%g
if "!part:~0,3!"=="com" (
set oldname=%%f.%%g.%%h
set newname=%%f.!part:~4!.%%h
echo "!oldname!" -^> "!newname!"
ren "!oldname!" "!newname!"
)
endlocal
)
A few notes
for loop variables are single letter, like this: %f
in a batch file, the % must be escaped, so %f becomes %%f
delims=. splits the filenames at the ., in your case into three parts
tokens=1,2,3 returns three variables containing the individual name parts (%f, %g and %h)
enabledelayedexpansion switches on dynamic variable handling
you can do string manipulation with variables: %foo:~0,3% returns the first three characters of %foo%.
with delayed expansion enabled, you can access variables that change their values by using ! instead of %
the > must be escaped or echo won't print it, hence ^>
read some more on String Manipulation in DOS
read HELP FOR and try the following....
#echo off
setlocal enabledelayedexpansion
for %%a in (*.jpg) do (
set fn=%%~na
set fn=!fn:.com=!
echo REN "%%a" "!fn!.jpg"
)
you need to enable delayed expansion because you need to expand a variable inside the for loop.
the loops iterates over all the jpg files in the current directory and for each file it extracts its filename using the ~n syntax, and then it removes all the occurences of .com by replacing them with an empty string. Read HELP SET
after careful testing, remove the echo command