I have lots of dlls present inside a folder . I want to register all those dlls in a single shot by running one batch file , So that i dont have to register each and every dll one by one .
Please help me in generating a batch file for it .
Thanks in advance .
Okay, this should work:
Code:
#echo off
pushd C:\...[path to file]
for /r %%a in (*.dll) do (
Rem Put the instance of the REG command you want to use here
Rem (refer to file as %%a)
Rem e.g. | REG add %%a
Echo Registering %%a . . .
)
popd
Echo.
Pause | Echo Registration Complete: Program will now Exit
Exit
Pretty much for /r performs a recursive search through the current dir (and sub-dirs).
Type for /? for more info or ask if you want further explanation.
Explanation:
In the above code all commands used followed by and explanation is below:
Echo - Displays a message on the screen.
#echo - Turns Echo to either on or off. Turning it off will suppress the console from displaying the prompt. That is, the user will not see what commands the batch file is executing, only what you tell the batch file to Echo and the output of any other commands used.
pushd - Changes the current directory command prompt searches in to the path specified (and stores the previous direcotry for convienience)
popd - Goes to the last directory in (in relation to pushd)
for /r - Executes the commands specified for each file in the current and sub directory's where the variable created (in this example %%a) is the name of the file
Reg - used to register
Rem - Remarks in a batch file : These lines are comments that are ignored
Pause - Waits for user input
Exit - Exits
Example:
#echo off
pushd c:\MyDll
for /r %%a in (*.dll) do (
Reg /add %%a
Echo Registering %%a . . .
)
popd
Echo.
Pause | Echo Registration Complete: Program will now Exit
Exit
Related
I'm trying to write a batch/cmd file that will rename directories when it finds " - " that is SpaceDashSpace. I thought I had everything I needed by outputting things via Echo and not actually renaming them. Next I tested it on a few directories and it worked perfectly.
When I tried to move it to a networked folder/sub folder I found I needed the PushD command. Again all is well if I run it in a specific directory.
Path information would look something like:
\\MyNetworkStorage\TopLevelFolder\Music
Followed by:
Artist1
Artist1 - Album1
Artist1 - Album2
Artist1 - Album3
Artist2
Artist2 - Album1
Artist2 - Album2
You get the idea.
If I kick the batch file off in either:
\MyNetworkStorate\TopLevelFolder\Music\Artist1 or
\MyNetworkStorate\TopLevelFolder\MusicArtist2
all is good. If I try to do it from one level up, that is:
\MyNetworkStorate\TopLevelFolder\Music
I get the following error for every single directory.:
"The system cannot find the file specified"
Googling tells me the path might be to long and I've followed instructions to
Local Computer Policy > Computer Configuration > Administrative
Templates > System > Filesystem
Double click the Enable Win32 Long
paths option and enable it
Another suggestion was when running my .cmd file on the local machine to preface it with:
cmd.exe /c MyCmdFile.cmd
Nothing I've searched for has fixed my problem.
I'm new to batch files so perhaps the problem is actually in what I've cobbled together the last 1.5 days. At this point I wish I wrote a C# program for it. I thought this would be simple. Any suggestings?
Again if I start it in any specific Artist's directory it will rename all the sub-directories removing the "Artists Name - " portion and leave just the album name as I want it to. When I move up to the directory that contains all the artists, it fails. I'm changing this on line that starts with "PUSHD" of the command file.
#Echo Off
setlocal EnableDelayedExpansion
PUSHD \\MyNetwork\Music\HighRez <<<---This fails
PUSHD \\MyNetwork\Music\HighRez\Any Artist I pick <<<---This works for all sub directories
:: If this fails then exit
If %errorlevel% NEQ 0 goto:eof
:: other commands...
FOR /D /r %%G in ("* - *") DO (
SET DirName=%%~nxG
set "NewName=!DirName:* - =!"
Echo Renaming from "!DirName!" To "!NewName!"
REM ***Remove REM from line below to actually rename once you are happy with results
ren "!DirName!" "!NewName!"
)
popd
This is what ended up working for anyone who might need something similar. It was the combination of using %%G along with quotes around it as in:
ren "%%G" "!NewName!"
In place of the original variable "!DirName!"
Working example, finally ;-)
#Echo Off
setlocal EnableDelayedExpansion
PUSHD \\NASBoxOnNetwork\Music\HighRez
:: If this fails then exit
If %errorlevel% NEQ 0 goto:eof
:: other commands...
FOR /D /r %%G in ("* - *") DO (
SET DirName=%%~nxG
set "NewName=!DirName:* - =!"
Echo Renaming from "%%G" To !NewName!
REM ***Remove REM from line below to actually rename once you are happy with results
ren "%%G" "!NewName!"
)
popd
I have a directory with a bunch of files with a mix of extensions. I only want to work with files with extension *.abc. Each *.abc file should then be handed over to another software with some parameters. The parameters are always the same for each file. One of the parameters needs to be defined by the user, though.
So my first try was this:
#ECHO OFF
set /p value="Enter required imput value: "
for %%f in (*.abc) do (
START C:\"Program Files"\Software\startsoftware.exe -parameter1 "%%~nf.abc" -parameter2 %value% -parameter3
)
PAUSE
The script works but is causing a memory crash, as the software is getting all request basically at once.
However, if I could manage to write all file names in one command line the software would process all files one by one. It needs to be called like this:
START C:\"Program Files"\Software\startsoftware.exe -parameter1 file1.abc -parameter2 %value% -parameter3 -parameter1 file2.abc -parameter2 %value% -parameter3 -parameter1 file3.abc -parameter2 %value% -parameter3 -parameter1 file4.abc -parameter2 %value% -parameter3
My idea was to generate a files.txt with listing all *.abc using:
dir /b /a-d > files.txt
Then read that list into my command. However, I don't know how to read out the files.txt and apply parameters including the variable %value% to each file.
1. Quote inside an argument string
" inside an argument string is usually not correct. The entire argument string must be usually enclosed in double quotes and not just parts of it. So wrong is C:\"Program Files"\Software\startsoftware.exe and correct would be "C:\Program Files\Software\startsoftware.exe".
That can be seen by opening a command prompt, typing C:\Prog and hitting key TAB to let Windows command processor complete the path to "C:\Program Files". The Windows command processor added automatically the required double quotes around entire path string. The path would change to "C:\Program Files (x86)" on pressing once more key TAB. However, continue typing with "C:\Program Files" displayed by entering \soft and press again TAB and displayed is "C:\Program Files\Software". The second " moved to end of new path. Type next \start and press once more TAB. Now is displayed "C:\Program Files\Software\startsoftware.exe" which is the correct fully qualified file name of this executable enclosed in double quotes as required because of the space character in path.
For more information about this feature of Windows command processor run in command prompt window cmd /? and read the output help from top of first page to bottom of last page.
2. START and TITLE string
The help for command START is output on running start /? in a command prompt window.
START interprets the first double quoted string as optional title string for the console window. For that reason it is advisable to specify first after command name START always a title in double quotes. In case of a Windows GUI application is started on which no console window is opened at all or a console application is executed in background without opening a new console window, the title string can be specified with just "" after START which is just an empty title string.
3. Running applications parallel
The command START is used to run an application or script parallel to the Windows command process which is processing the batch file. This is often useful, but definitely not here on which an application should be executed to process a file of a large set of files which need to be processed all.
The following command line would start for each *.abc file the executable startsoftware.exe for execution parallel to cmd.exe which is processing the batch file.
for %%f in (*.abc) do START "" "C:\Program Files\Software\startsoftware.exe" -parameter1 "%%~nf.abc" -parameter2 %value% -parameter3
This results with many *.abc files in current directory in a situation on which Windows fails to run one more process due to a resource problem as too many processes are running already more or less parallel.
4. Running application in series
It is usually better on processing many files to run an application for processing a file and halt processing of the batch file until the application finished and terminated itself. That can be achieved by not using the command START.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist *.abc (
echo ERROR: There are no *.abc in folder: "%CD%"
echo/
pause
goto :EOF
)
set "Value="
:UserPrompt
set /P "Value=Enter required input value: "
if not defined Value goto UserPrompt
set "Value=%Value:"=%"
if not defined Value goto UserPrompt
for %%I in (*.abc) do "C:\Program Files\Software\startsoftware.exe" -parameter1 "%%I" -parameter2 "%Value%" -parameter3
endlocal
The behavior on starting an executable from within a batch file is different to doing that from within a command prompt window. The Windows command processor waits for the self-termination of the started executable on being started during processing of a batch file. Therefore this code runs always just one instance of startsoftware.exe in comparison to the loop above using command START to start multiple instances quickly in a short time.
5. Running application with multiple files
It looks like it is possible to run startsoftware.exe with multiple arguments to process several files at once. But the maximum command line length limit of 8191 characters must be taken into account on writing a batch file which runs the executable with a list of arguments to process multiple files at once.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist *.abc (
echo ERROR: There are no *.abc in folder: "%CD%"
echo/
pause
goto :EOF
)
set "Value="
:UserPrompt
set /P "Value=Enter required input value: "
if not defined Value goto UserPrompt
set "Value=%Value:"=%"
if not defined Value goto UserPrompt
set "Arguments="
set "CmdLineLimit="
for /F "eol=| delims=" %%I in ('dir *.abc /A-D /B 2^>nul') do call :AppendFile "%%I"
if defined Arguments "C:\Program Files\Software\startsoftware.exe"%Arguments%
goto :EOF
:AppendFile
set Arguments=%Arguments% -parameter1 %1 -parameter2 "%Value%" -parameter3
set "CmdLineLimit=%Arguments:~7800,1%"
if not defined CmdLineLimit goto :EOF
"C:\Program Files\Software\startsoftware.exe"%Arguments%
set "Arguments="
set "CmdLineLimit="
goto :EOF
The loop for %%f in (*.abc) do is modified in this code to a for /F loop to get first a list of file names loaded completely into memory instead of processing the directory entries which could change on each execution of startsoftware.exe if it modifies the *.abc files in current directory.
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 /?
dir /?
echo /?
for /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also Where does GOTO :EOF return to?
I am a complete novice when it comes to scripting, but am attempting to write a batch script which runs a command to output a png file to a printer. The script I have works fine for one file, but when there are multiple files it does not.
Can anyone point me in the right direction please?
#echo off
REM ___Change Directory to where Label Is Stored___
pushd C:\AFP\to
REM ___Create Variable to capture filename of any png file___
for /F %%a in ('dir /b *.png') do set FileName=%%~na.png
REM ___Now we have the filename as a variable, send it to printer using Zebra SSDAL___
\\172.16.100.2\nDrive\Prime_DPD_Label_Print\ssdal.exe /p "TSC DA200" send %FileName% >> C:\AFP\Log\Label_Printing_Log.txt
REM ___Copy PNG File to Backup Folder___
XCOPY /y /q /c C:\AFP\to\*.png C:\AFP\backup\
REM ___Delete PNF File from To Folder___
DEL C:\AFP\to\*.png
When the script runs, the first file prints fine. The subsequent files then do not print, I get "File does not exist" back from the ssdal.exe command. Why would the first one work but not the subsequent prints? I would have expected the for to loop through.
#ECHO Off
SETLOCAL
REM ___Change Directory to where Label Is Stored___
pushd C:\AFP\to
REM ___Process all png files___
for /F "delims=" %%a in ('dir /b *.png') do (
REM ___Now we have the filename as "%%a", send it to printer using Zebra SSDAL___
\\172.16.100.2\nDrive\Prime_DPD_Label_Print\ssdal.exe /p "TSC DA200" send "%%a" >> C:\AFP\Log\Label_Printing_Log.txt
IF ERRORLEVEL 1 (
CALL ECHO SSDAL returned ERRORLEVEL %%errorlevel%% FOR "%%a"
) ELSE (
REM ___Move PNG File to Backup Folder___
IF EXIST "c:\afp\backup\%%a" (
ECHO MOVE "%%a" to backup skipped as file already exists IN backup
) ELSE (
MOVE "%%a" C:\AFP\backup\
)
)
REM Two-second delay
TIMEOUT /t 2 >nul 2>nul
)
POPD
GOTO :EOF
Ah! using Zebra printers. Sensible lad!
This replacement script should do what you want.
The setlocal command is used to ensure that any variation made by this batch to the cmd environment is discarded when the batch ends.
The delims= sets "no delimiters" so for/f will set %%a to the entire filename, even if it contains spaces or other delimiters. Quoting %%a ensures such filenames are kept together as a single unit, not interpreted as separate tokens.
I'm assuming that ssdal acts responsibly and returns errorlevel non-zero in the case of errors. The if errorlevel 1 means if the errorlevel is currently 1 or greater than 1 and in that case, the error message is generated. We need to call echo ... %%varname%% ... in order to display the current value of the variable, if we're not using delayed expansion (many SO articles explain this)
Otherwise, if ssdal was successful, check for the existence of the filename in the backup directory, and either move it there or report that it already exists.
Of course, there are many ways in which this could be manipulated if features I've added are not desired. I'm happy to adjust this script to comply.
timeout is a standard utility to wait for a keypress. The redirection takes care of its prompting (it will provide a countdown unless gagged).
I have written a batch file that I use for file management. The batch file parses an .XML database to get a list of base filenames, then allows the user to move/copy those specific files into a new directory. The program prompts the user for a source directory and the name of the .XML file. I would like the program to default the variables to the last used entry, even if the previous CMD session has closed. My solution has been to ask the user for each variable at the beginning of the program, then write those variables to a separate batch file called param.bat at the end like this:
#echo off
set SOURCEDIR=NOT SET
set XMLFILE=NOT SET
if exist param.bat call param.bat
set /p SOURCEDIR=The current source directory is %SOURCEDIR%. Please input new directory or press [Enter] for no change.
set /p XMLFILE=The current XML database is %XMLFILE%. Please input new database or press [Enter] for no change.
REM {Rest of program goes here}
echo #echo off>param.bat
echo set SOURCEDIR=%SOURCEDIR%>>param.bat
echo set XMLFILE=%XMLFILE%>>param.bat
:END
I was hoping for a more elegant solution that does not require a separate batch file and allows me to store the variable data within the primary batch file itself. Any thoughts?
#echo off
setlocal
dir /r "%~f0" | findstr /c:" %~nx0:settings" 2>nul >nul && (
for /f "usebackq delims=" %%A in ("%~f0:settings") do set %%A
)
if defined SOURCEDIR echo The current source directory is %SOURCEDIR%.
set /p "SOURCEDIR= Please input new directory or press [Enter] for no change. "
if defined XMLFILE echo The current XML database is %XMLFILE%.
set /p "XMLFILE=Please input new database or press [Enter] for no change. "
(
echo SOURCEDIR=%SOURCEDIR%
echo XMLFILE=%XMLFILE%
) > "%~f0:settings"
This uses the Alternate Data Stream (ADS) of the batchfile to
save the settings.
NTFS file system is required. The ADS stream is lost if the
batchfile is copied to a file system other than NTFS.
The dir piped to findstr is to determine if the
stream does exist before trying to read from it.
This helps to avoid an error message from the for
loop if the ADS does not exist.
The for loop sets the variable names and values read from the ADS.
Finally, the variables are saved to the ADS.
Note:
%~f0 is full path to the batchfile.
See for /? about all modifiers available.
%~f0:settings is the batchfile with ADS named settings.
dir /r displays files and those with ADS.
Important:
Any idea involving writing to the batchfile could result
in file corruption so would certainly advise a backup of
the batchfile.
There is one way to save variables itself on the bat file, but, you need replace :END to :EOF
:EOF have a good explained in this link .:|:. see Where does GOTO :EOF return to?
Also, this work in fat32/ntfs file system!
You can write the variables in your bat file, and read when needs:
Obs.: Sorry my limited English
#echo off & setlocal enabledelayedexpansion
set "bat_file="%temp%\new_bat_with_new_var.tmp"" & type nul >!bat_file! & set "nop=ot Se"
for /f %%a in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x40"') do set "delim=%%a"
type "%~f0"| findstr "!delim!"| find /v /i "echo" >nul || for %%s in (SOURCEDIR XMLFILE) do set "%%s=N!nop!t"
if defined SOURCEDIR echo/!SOURCEDIR!%delim%!XMLFILE!%delim%>>"%~f0"
for /f "delims=%delim% tokens=1,2" %%a in ('type "%~f0"^| findstr /l "!delim!"^| find /v /i "echo"') do (
set /p "SOURCEDIR=The current source directory is %%~a. Please input new directory or press [Enter] for no change: "
set /p "XMLFILE=The current XML database is %%~b. Please input new database or press [Enter] for no change: "
if /i "!old_string!" neq "!SOURCEDIR!!delim!!XMLFILE!!delim!" (
type "%~f0"| findstr /vic:"%%~a!delim!%%~b!delim!">>!bat_file!"
copy /y !bat_file! "%~f0" >nul
echo/!SOURCEDIR!!delim!!XMLFILE!!delim!>>%~f0"
goto :_continue_:
))
:_continue_:
rem :| Rest of program goes here | replace/change last command [goto :END] to [goto :EOF]
goto :EOF
rem :| Left 2 line blank above, because your variable will be save/read in next line above here |:
I'm very new to coding and iI'm having a problem that is probably trivial, but is making me pull out my hair.
I'm using a batch script to automate mounting a VHD, executing a file inside and then pause until the user presses any key, which makes the VHD get unmounted and the script exits.
This is the main batch file:
#echo off
set fileVHD=Gord
CD /D "%~dp0"
powershell -command "Start-Process mount.cmd '%~dp0%fileVHD%.vhd' -Verb runas"
timeout /t 1
for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "%fileVHD%"') do set usb=%%D
CD /D %usb%
index.html
echo "!!!!!!!!!!!!!!!!!!!!Press any key to fully close this program.!!!!!!!!!!!!!!!!!!!!!!!!!"
pause
CD /D "%~dp0"
powershell -command "Start-Process unmount.cmd '%~dp0%fileVHD%.vhd' -Verb runas"
exit
This is the mount script (Not made by me):
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo Usage: %~nx0 [vhd] [letter]
exit /b 1
)
set "vhdPath=%~dpnx1"
set "driveLetter=%2"
if "!driveLetter!"=="" (
echo Mounting "!vhdPath!"
) else (
echo Mounting "!vhdPath!" to "!driveLetter!":
)
REM
REM create diskpart script
REM
set "diskPartScript=%~nx0.diskpart"
echo select vdisk file="!vhdPath!">"!diskPartScript!"
echo attach vdisk>>"!diskPartScript!"
REM assign the drive letter if requested
if not "!driveLetter!"=="" (
echo select partition 1 >>"!diskPartScript!"
echo assign letter="!driveLetter!">>"!diskPartScript!"
)
REM Show script
echo.
echo Running diskpart script:
type "!diskPartScript!"
REM
REM diskpart
REM
diskpart /s "!diskPartScript!"
del /q "!diskPartScript!"
echo Done!
endlocal
When all the files are located in a system path that contains no spaces, everything works fine. But it breaks where there are spaces.
That means that somewhere in the code a path is badly defined by the lack of quotes, probably in the mount script. The trouble is that i don't fully grasp the mount script when it starts using all the "%~...." variable path names.
I had to mix in some powershell commands because for some reason the script wouldn't work unless executed as Administrator.
If someone could give some insight to a newbie, it would be greatly appreciated.
You need end quotes around your parameters when you change directory, i.e.
CD /D "%~dp0"
You can also see all of the %~ options by running 'help for' in a console window. In those scripts it's getting the path or filename from a variable.
Discovered the root of my problem.
The path from script 1 was not being passed faithfully to script 2, even using using quotes or multiquotes.
Thanks for all the input guys!