Too many parameters in bat files - batch-file

#echo off
md C:\Users\%username%\Documents\"Backuped Minecraft Mods"
echo C:\Users\%username%\Documents\Backuped Minecraft Mods\Backups > C:\Users\%username%\Documents\"Backuped Minecraft Mods"\path.txt
set /p mpath=<"C:\Users\%username%\Documents\Backuped Minecraft Mods\path.txt"
echo %mpath%
tree /f %mpath%
pause
This is a part of bat file which I am making,
but I there is a problem in command "tree /f %mpath%", I think.
When I run this, there is a error massage which says-
매개 변수가 너무 많습니다 - Minecraft
매개 변수가 너무 많습니다 is "Too many parameters" in English.
I used only ONE parameter in tree command so why "Too many parameters"?

Here's some example code for you.
Echo Off
Set "bpath=%UserProfile%\Documents\Backuped Minecraft Mods"
Set "mpath=%bpath%\Backups"
Set "tpath=%bpath%\path.txt"
If Not Exist "%bpath%\" MD "%bpath%"
(Echo=%mpath%)>"%tpath%"
Rem Set /P "mpath="<"%tpath%"
Tree /F "%mpath%"
Pause
I've Remarked line 9 which isn't required in the provided context of your snippet.

Related

BATCH file reloading autorun buffer

There are things that we prefer not to understand in order to have an easier life to live.
But this is not something I can choose...
I made a batch file (or macro.doskey) to get the charset code. And it worked perfectly for a long time...
Basically it runs chcp:
> chcp
Code page active: 850
and then wraps the return before and after the colon
assigning what comes after to a variable:
FOR /F "tokens=1,* delims=:" %%s in ('CHCP') do (
#ECHO %%t
IF NOT "%1" == "" (SET %1=%%t)
)
For example:
> getCHCP.bat myVar
850
> ECHO %myVar%
850
However it started to lock, waiting for ENTER or displaying several echo messages. For example:
> getchcp myVar
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
850
I started to mix until I decided to change the ECHO %%t to ECHO %%s, and guess what?
No, is that the Bill Gates skull? Is it an easter egg from Microsoft? A virus?
No, none of that, this is just my autorun's welcome message.
This can be configured in
<[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]autorun>
In my case I called a batch file which, among other things, gives several echos showing this skull on the screen.
But the question is, why does it act like it reloads the autorun in background
when I've already opened the command prompt?
And why does it leave
everything in the buffer so that %%s pulls it again to the (Page code active) ':'?
And why are
you giving lots of ECHO is off on %%t when the only thing after
Code page active: is a number?
And the most important: How I solve it?
It's obvious, you already point to the problem.
this is just my autorun's welcome message.
This can be configured in
<[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]autorun>
The line FOR /F %%s in ('CHCP') ... start CHCP but that will be done in a NEW child cmd.exe instance.
And a NEW cmd.exe instance runs the autorun command!
Just before it starts your chcp.
You can disable the autorun at all, or add some code to detect the difference between a new cmd.exe instance for the user against a new instance from a FOR /F.
Put this code at the start of your autorun batch file
#echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
set "_ccl_=!cmdcmdline!"
REM *** %1 contains only data, when the script itself was called from the command line
if "%~1" NEQ "" (
goto :direct_call
)
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** Show your skull or something else
)
exit /b

Randomly choose between two names in batch

I'm trying to get a batch script to randomly choose between two names and output one at a time, kinda something like this:
#echo off
::I would not be prompted to enter these, they would already be programmed into the script.
ChooseRandomSet=Maria
Sean
Matt
Laura
ChooseRandom
So, then on the user side, you would see:
C:\>script.bat
Matt was randomly chosen.
C:\>script.bat
Sean was randomly chosen.
C:\>
Thanks!
First create a text file with all the names on separate lines:
Maria
Sean
Matt
Laura
Then create a .bat file with the following code:
#ECHO OFF
IF "%~1"=="" (ECHO No text file specified & GOTO :EOF)
IF NOT EXIST %1 (ECHO Text file doesn't exist. & GOTO :EOF)
FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
IF %lines%==0 (ECHO Text file is empty or unreadable & GOTO :EOF)
SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
<%1 (
FOR /L %%I IN (1,1,%skip%) DO (
SET /P line=
)
SET line=
SET /P line=
)
ECHO(%line%
This will print out a name from the file, it will also check for certain errors like:
No text file specified
File specified but non-existent
Empty text file
Disclaimer:
I used this bat script for something but I didn't write it, I might've changed a few things over the years but it's largely as I found it a while back. (not a Windows guy myself.. :P)
Update: I also found an answer to this myself just by experimenting. This chooses one option out of two possible choices:
#echo off
if %random% gtr %random% (
echo heads
) else (
echo tails
pause

How to use launch commands in a batch file

Okay, well, I want to make something like OS... But when I start the .bat file there's a set option for going to help and going to the GUI. I have shortcut with -gsys32 - directly to boot the command. Brhfd... Just take a look if the code, I leave some comments.
#ECHO off
echo GraphicalSystem
echo All rights reserved! 2016
echo PLEASE WRITE help TO OPEN THE HELP WINDOW!
set /p command=
if %command% ==help goto help
if %command% ==gsys32 goto interface
cmd /k
:help
cls
echo gsys32 - Open the graphical interface.
echo exithelp - exit the help
set /p command=
if %command% ==exithelp goto start
cmd /k
:interface
cls
color 17
#ECHO OFF
echo PROGRAMS - GSYS32
echo DRIVE A:\
dir
#ECHO OFF
echo *Write dir /name of the directory without the slashes/*
cmd /k
:start
cls
#ECHO off
echo GraphicalSystem
echo All rights reserved! 2016
echo PLEASE WRITE help TO OPEN THE HELP WINDOW!
set /p command=
if %command% ==help goto help
if %command% ==gsys32 goto interface // I want this to be executed with the shortcut.
cmd /k
And now the shortcut:
So... I want the command from the screenshot to be executed in the code...
Seems to be just a section of the code, so it's difficult to tell.
set "command=%1"
if not defined command goto noparms
if /i "%command%"=="gsys" goto interface
if /i "%command%"=="help" goto help
echo parameter "%1" not recognised&pause
rem don't know what you want to do now...
...
:noparms
rem there were no parameters supplied
....your posted code
%1 accesses the first parameter supplied to the routine.
you would need -gsys in place of gsys if you want to detect -gsys as a parameter. The match is literal (/i option makes it case-insensitive)
It is windows convention that switch-parameters use the format /gsys - but that's a convention, not a rule.
set /p "var=Promptstring" is the general form for accepting keyboard input. Parameters are read using %1..%9

How can you create a batch script which runs the user-specified software?

So basically I want to create a batch script that can run any notepad file which the user specifies. I tried this...
#Echo Off
SET /P ANSWER=What is the name of the file to open?
IF /i (%ANSWER%)==('FIND /i "*.txt" %ANSWER%) (goto :Filename)
goto :exit
:Filename
Start *.txt
EXIT
:exit
ECHO FAILLLLLLLL
PAUSE
EXIT
The issue here is the first IF statement. I know its wrong. But, I don't know how to specify the entry of any filename. A different way to do this task is also appreciated.
Thanks for help :)
If your goal is simply to open a file that the user specifies in Notepad, the following works for me in Windows 7:
#echo off
set /P answer=What is the file name?
if exist %answer% (
start notepad.exe %answer%
) else (
echo Unable to locate %answer%
)

Temporarily set file associations

I have a portable development tool that I want to use on other PCs. I'd like to set a file association so that clicking a file opens the tool. Then, when I'm finished, I want to undo or reset the file association on that PC.
Is there a way to do this? Possibly from a batch file?
Well, you can use the ftype and assoc commands to create or delete file type associations:
ASSOC .foo=FooFile
FTYPE FooFile=X:\Foo\foo.exe %1 %*
You can delete them later with
FTYPE FooFile=
ASSOC .foo=
EDIT: I've got a try now with something that enables you to re-set the association back to its default. I put it in my Subversion repo. In its current stage it generates two new batch files: set.cmd and reset.cmd; one of which sets a new association, the other reverses it. Rolling the set.cmd into the actual batch shouldn't be too difficult but it would have made testing here a hell, so I'll leave that as an exercise.
Code follows, it should be commented enough, hopefully.
#echo off
setlocal enableextensions enabledelayedexpansion
rem Debug flag. Generates a little more output; un-set if undesired
set DEBUG=1
rem Parse arguments and help
if [%1]==[] goto Usage
if [%2]==[] goto Usage
rem Find out whether the association is taken
for /f "usebackq tokens=2* delims==" %%x in (`assoc .%1 2^>nul`) do set assoc_old=%%x
if defined DEBUG (
if defined assoc_old (echo Association already defined: [%assoc_old%]) else (echo Association not yet taken)
)
rem Find a new, unused association
rem Note that we assume that we find one, eventually. This isn't guaranteed, but we'll ignore that for the moment
rem Otherwise this loop might run forever
:loop
set assoc_new=My.%1.%RANDOM%
if defined DEBUG echo Trying new association (%assoc_new%)
assoc .%1 >nul 2>nul
if errorlevel 1 (
set assoc_new=
if defined DEBUG echo Didn't work out
) else (
if defined DEBUG echo Found one! \o/
)
if not defined assoc_new goto loop
if defined DEBUG echo Writing reset batch file
echo #echo off>reset.cmd
echo assoc .%1=%assoc_old%>>reset.cmd
echo ftype %assoc_new%=>>reset.cmd
if defined DEBUG echo Writing setting batch file
echo #echo off>set.cmd
echo assoc .%1=%assoc_new%>>set.cmd
echo ftype %assoc_new%=%2 %%1>>set.cmd
goto :eof
:Usage
echo.Usage
echo. %~nx0 type command
echo.
echo. type is the file type to override, such as docx or txt.
echo. No dot before it is necessary.
echo. command is the command to perform on that file.
echo. %%1 is automatically appended at the end.
echo. If the command includes spaces, surround it with quotes.
echo.
echo.Example
echo. %~nx0 txt notepad
exit /b 1

Resources