Batch file which saves user input path with spaces to a text file. (win 7) - input

First of all i am a noob in programming.
I am trying to make a batch file which takes an installed directory of a program as user input when run for the first time (means it should not ask for the directory the second time it is run). By searching for various scripts, i reached till here,
#echo off
Echo =============================================
echo Directory
Echo =============================================
setlocal enableextensions enabledelayedexpansion
set /p mypath=Please specify install directory;
Echo %mypath% ----was what you typed
pause
echo start>temp.txt
echo %mypath%>>temp.txt
echo \programfolder\program.exe>>temp.txt
echo -argument -argument>>temp.txt
setlocal enabledelayedexpansion
set FINAL=
for /f %%a in (temp.txt) do (
set FINAL=!FINAL!%%a
)
echo %FINAL%>input.txt
del /q temp.txt
Pause
start "<input.txt"
This saves the input path in the "input.txt" text file, and runs the program the next time it is launched.
I want the text file to have the saved path as "start driveletter:\foldername\foldername with spaces\programfolder\program.exe" -arguments
However the "start", "program folder", "program.exe" and "-arguments" are fixed.
The user input path should get saved in- %mypath%.

The does what you asked, I think:
#echo off
if exist "go.bat" go.bat
set /p "mypath=Please specify install directory; "
Echo "%mypath%" ----was what you typed
pause
>"go.bat" echo #start "" "%mypath%\programfolder\program.exe" -argument -argument

Related

"Echo is OFF" text always inserted into batch file when SET variable is written to a batch file IF the file is made by another batch program

I am trying to create a batch file using another batch program using:
#echo {code here}>>batch-program.bat, but whenever I try to write code to write the contents of a SET variable to a text file, the batch program does not write the code into the other batch file, but instead writes "Echo is OFF."
Code is here:
#echo off
#echo #echo off>>apt.bat
#echo color 2A>>apt.bat
#echo echo example-batch>>apt.bat
#echo cd C:/Users/Default/apt/assets>>apt.bat
#echo mkdir cmdInput>>apt.bat
#echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
#echo set /p cmdInput= cmd->>apt.bat
#echo %cmdInput%>>used-cmdInput.txt>>apt.bat
#echo pause>>apt.bat
This should have created a batch file named apt.bat, and written into the batch file:
#echo off
echo color 2A
echo example-batch
cd C:/Users/Default/apt/assets
mkdir cmdInput
cd C:/Users/Default/apt/assets/cmdInput
set /p cmdInput= cmd-
%cmdInput%>>used-cmdInput.txt
pause
but the 9th line (%cmdInput%>>used-cmdInput.txt)
is instead converted into the text line "Echo is OFF".
Have I done anything wrong, or is it just a really weird bug?
EDIT: I found another problem in the program, that because mkdir cmdInput is always run when apt.bat is run, so it displays a error message because of apt.bat trying to create the directory cmdInput though it already exists. apt/assets. So I have changed the code a bit, so that the directory cmdInput is created in the first "creation" batch file (the program that was used to create apt.bat). mkdir cmdInput has been removed from apt.bat.
You need to escape > with ^ but you need to escape % with %
#echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
unless you want to output the contents of cmdinput where you need
#echo %cmdInput%^>^>used-cmdInput.txt>>apt.bat
You can add 2>nul to the end of a md command to suppress the error message generated if the directory already exists.
You should use backslashes \ in directorynames, not forward slashes /. In winbat, the forward slash is often used for command switches. Sometimes forward slashes will work for directorynames, but backslashes always work.
You just have to "escape" the percent-signs with another % and other special chars like > with a caret) to prevent evaluation of your variable %cmdInput% (which probably is empty - therefore the Echo is off).
Also a single #echo off is sufficient. No need to add a # to every line.
#echo off
echo #echo off>>apt.bat
echo color 2A>>apt.bat
echo echo example-batch>>apt.bat
echo cd C:/Users/Default/apt/assets>>apt.bat
echo mkdir cmdInput>>apt.bat
echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
echo set /p cmdInput= cmd->>apt.bat
echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
echo pause>>apt.bat
A more elegant way is to use only a single redirection (cmdhas to open the file for writing just once):
#echo off
(
echo #echo off
echo color 2A
echo echo example-batch
echo cd C:/Users/Default/apt/assets
echo mkdir cmdInput
echo cd C:/Users/Default/apt/assets/cmdInput
echo set /p cmdInput= cmd-
echo %%cmdInput%%^>^>used-cmdInput.txt
echo pause
)>apt.bat

batch script how to check if variable exist then replace variable with another

I've tried looking every where how to do this but I cant find out how.
How do I check if a variable is defined and if it is replace every in variable in that file with a different variable. thanks
okay here is the code
#echo off
set/p "directoryname= Name of directory (to run on startup type "startup"). : "
if %directoryname%==startup goto startup
:namedirectory
if not exist "%directoryname%" (
mkdir "%directoryname%"
if "!errorlevel!" EQU "0" (
echo Folder created successfully
) else (
echo Error while creating folder
)
) else (
echo Folder already exists
)
goto skip_startup
:startup
:: here i would like to check if the variable %directoryname% is defined,
:: and it is, replace the variable named %directoryname% with
:: (Not changing the variables name) with the directory,
:: "C:\Documents and Settings\All Users\Start Menu\Programs\Startup". that
:: way when ever i can recall my variables in my file i can change my folder
:: to the startup directory.
:skip_startup
cd %directoryname%
:: ^
:: |_ replaced variable
Here is the commented batch code with the requested and some more enhancements:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define as default directory name "startup". The user has to hit just
rem ENTER or RETURN with this default value and variable DirectoryName
rem keeps the default value "startup". Then ask user for directory name
rem and remove all double quotes from the entered directory name.
:EnterDirectory
set "DirectoryName=startup"
echo Please enter the name of the directory.
echo/
echo Hit just RETURN or ENTER to run on startup directory.
echo/
set /P "DirectoryName=Name of directory: "
set "DirectoryName=%DirectoryName:"=%"
rem Check if something other than just one or more double quotes was entered.
rem Prompt user once more after clearing screen if this is really the case.
if not defined DirectoryName cls & goto EnterDirectory
if "%DirectoryName%" == "startup" goto Startup
rem Check if entered directory name does not contain only spaces.
if "%DirectoryName: =%" == "" cls & goto EnterDirectory
echo/
if not exist "%DirectoryName%" (
mkdir "%DirectoryName%"
if errorlevel 1 (
echo Error on creating folder: "%DirectoryName%"
) else (
echo Folder "%DirectoryName%" created successfully.
)
) else (
echo Folder "%DirectoryName%" already exists.
)
goto SetDirectory
rem The variable DirectoryName has the default value "startup". Replace
rem this value by directory path of "Startup" folder in all users start
rem menu of Windows.
:Startup
set "DirectoryName=%ALLUSERSPROFILE%\Start Menu\Programs\Startup"
rem Set current directory to "Startup" directory or entered directory.
:SetDirectory
endlocal & cd /D "%DirectoryName%"
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.
cd /?
cls /?
echo /?
endlocal /?
goto /?
if /?
mkdir /?
rem /?
set /?
setlocal /?
See also:
Single line with multiple commands using Windows batch file
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/

Batch Help -- Move File To Directory, If Exist Filename, Rename 001

I have a batch file that checks a root directory every 10 seconds for PDF files and copies those files to their corresponding subfolders of the exact same name.
What I'm missing is that I need my batch file to check the target subdirectory for the named file, and if the named file exists, to rename the new file to be copied to !filename!001.pdf and have the 001 get incremented as duplicate file names are copied. Here's what I got:
:loop
setlocal enabledelayedexpansion
cls
pushd c:\files\
for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.pdf') do (
set filename=%%~n1&set dirname=!filename:~0,7!
dir c:\files /b *.pdf > location1list.tmp
for /f %%a in (location1list.tmp) do dir c:\files\%%a > location2list.tmp
if not exist c:\files\!dirname! (md c:\files\!dirname!)
move %%1 c:\files\!dirname!\>nul
)
timeout /t 10
goto:loop
Any suggestions how I can get the files renamed?
I.e. if Bob.pdf exists in the Bob folder and another Bob.pdf is added to the C:\files folder, I want it to be copied to the C:\files\bob folder as Bob001.pdf.
Something like this should do what you want. May need some tweaking. Also I have not tested it, just wrote it from memory. Need any explanations, just ask.
Script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd C:\files
:Main
for /f "tokens=*" %%A in ('dir /a-d /b *.pdf') do (
if not exist "%%~dpnA\*" md "%%~nA"
set "File=%%~dpnA\%%~nxA"
if exist "!File!" call :Name "!File!" File
echo !File!
move "%%~fA" "!File!"
)
goto Wait
:Name <Target> <Variable>
set "Count=0"
:Count
set /a "Count+=1"
set "Number=00%Count%"
if exist "%~dpn1%Number:~-3%%~x1" goto Count
set "%~2=%~dpn1%Number:~-3%%~x1"
goto :eof
:Wait
timeout /t 10
goto Main
:End
popd
endlocal
Limitations
Each target subdirectory can only handle a maximum of 1000 files as this is written. If the subdirectory hits this maximum then the script will get stuck in an infinite loop.
Update
Fixed an error in my initial script

Looping folder names to a variable using Batch?

I'm wanting to change the icon of every sub folder in a particular directory, can I do this by means of a loop similar to the %%~nxf used for files (I’m after folders not files)
attrib -h -r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo [.ShellClassInfo] >C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconFile= C:\ICT\AutoCAD_2010\Customisations\AO2\ICO\FolderIcon.ico >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo InfoTip=EXE file location... >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconIndex=0 >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +h +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE
Of cause do this a couple of hundred times in a batch and well you get the idea.
You may process folders (not files) with /D switch of for command:
for /D %%a in (*) do echo Folder: %%a
You may also combine /R with /D to process every subfolder beneath the given path. For further details, type: for /?

Batch script file to convert every file in every folder one at a time

What im trying to do is convert .ogg files stored in many different folders. What i NEED the batch script to do is go from the first folder and file , convert it and only then move on to the next file in the folder. Also during the conversion process a wave file is created, so i need it to be deleted at the end of the files conversion along with the original .ogg file. This is my code so far. It goes through every folder and file performing the oggdec producing massive amounts of wave files. I need the script to only do one folder and one file at a time
for /r %%i in (*.ogg) do oggdec "%%i"
for /r %%i in (*.ogg) do del "%%i"
for /r %%i in (*.wav) do lame -m m "%%i"
for /r %%i in (*.wav) do del "%%i"
#echo off
etlocal enabledelayedexpansion
set deletestring=.wav
echo Ready to start
echo.
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
set oldfilename=%%~nxF
set pathname=%%~dpF
set newfilename=!oldfilename:%deletestring%=!
Ren "!pathname!!oldfilename!" "!newfilename!"
)
echo.
echo All done
(
This code above almost works in that the end result is working, but unfortunately i need it to convert only one file at a time and delete the original .ogg and .wav before moving to the next .ogg file.So i need it to follow this rule
Take the first folder and the first .ogg file do oggdec on file producing the .wav del the .ogg file do lame -m m on the .wav file producing the .mp3 file del the .wav file move on to the next .ogg file until all files in the folder are converted then move on to the next folder which may not be in the same folder.
An example folder structure.
Sounds/voice/greek
Sounds/voice/English
Sounds/voice/russion
Using enhanced substitution for FOR variable:
%%~di - drive letter
%%~pi - file path
%%~ni - file name without extension
Resulting script is:
for /r %%i in (*.ogg) do (
oggdec "%%i"
del "%%i"
lame -m m "%%~dpni.wav"
del "%%~dpni.wav"
)