Merging txt files with batch - batch-processing

#echo off
title Merge Text Files
color 9f
echo Welcome to the program!
set /p loc=Please enter the location of folder where all txt files are located:
cd %loc%
set /p name=Type the name of new merged file (without adding ".txt"):
echo Press enter button to continue!
pause>nul
copy /b *.txt %name%.txt
echo Done!
pause>nul
echo Press any key to exit!
pause>nul
exit
I'm getting an error incorrect syntax
Can you tell me where my mistake is

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/

How to delete a directory using cmd

i'm new at cmd and this is the first code i've ever written using the program. There are parts of this code i haven't written myself.
My problem is that i'm trying to delete a folder using CMD but i can't get it to work.
I'm using this command:
RM /S /Q "directory"
In this code
cls
#ECHO OFF
color 02
title ija4779fc
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST MyFolder(2) goto MDMyFolder(2)
:CONFIRM
echo Are you sure to lock this folder? (Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo THIS IS NOT AN OPTION.
goto CONFIRM
:LOCK
ren MyFolder(2) "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto UNLOCK
:UNLOCK
echo ENTER PASSWORD:
set/p "pass=>"
if NOT %pass%== ija4779fc goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" MyFolder
echo UNLOCKED
goto PAUSE
:FAIL
Color 04
echo INCORRECT PASSWORD
DEL /S /Q "MyFolder(2)"
echo S
echo FILES DELETED
echo SYSTEM SHUTDOWN
goto end
:CORRECT
echo
:PAUSE
PAUSE
:MDMYFOLDER(2)
:END
end
MD Myfolder(2)
Thnx for the help.
The usual way to delete a subdirectory it to use RMDIR (or RD short short). The system documentation for RD is as follows (generated using RD /?)
Removes (deletes) a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S

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

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

Windows Batch: Automatically upload local CSV file list via FTP

I built a script that reads a CSV file for a list of files and then uploads them to an FTP server. The CSV structure is like this:
local_file,remote_file
The script creates a text file with all the necessary FTP commands and then runs the FTP command. Everything works, except that the for loop executes code that is outside of it's command, i.e., everything below echo put "%1" "%2" >> %Commands% is also executed on every for loop, and instead of getting a nicely formatted file with all the put commands I get this (from the commands file output):
open servername
username
password
binary
put "local_path_to\first_file_on_the_list.php" "remote_path_to\first_file_on_the_list.php"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
...
Here is the script code:
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
I don't understand why everything below :end is getting executed!
Thank you very much in advance
Why shouldn't the code below :end execute? You never put anything in the script to end the :parse routine.
The first ewall answer should almost work, except you need a GOTO :END after the FOR statement. I don't see how a proper implementation of his suggestion can lead to an endless loop.
Another option is to simply move your subroutine after your EXIT statement.
You have other hidden problems. File paths/names can contain spaces, so the default FOR delimiter of space,tab will not preserve the entire line if there are spaces. The default EOL will also cause any line that begins with ; to be ignored. That is a potential (but unlikely) problem, because a valid filename can begin with ;.
The solution is to set EOL to a character that cannot begin a valid file spec, and set DELIMS to nothing: "EOL=: DELIMS="
It is much more efficient to enclose all your file writing lines within parentheses and redirect the output just once. It's also easier to write and looks better.
Edit - The original script was attempting to connect to the server in both the ftp script and the ftp command line. One or the other had to be removed. I removed it from the ftp script.
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
(
echo %UserName%
echo %Password%
echo binary
rem Read the CSV file line by line
for /f "eol=: delims=" %%a in (matches3.csv) do call :parse %%a
rem Add commands to close ftp conn
echo close
echo bye
)>%Commands%
rem Perform the FTP upload
echo logging in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2"
The :end doesn't stop the CALL function from running, it's just a label. Instead, I suspect you meant to use GOTO :eof, which would return to the for-loop:
...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
goto :eof
rem Add commands to close ftp conn
...
Or, rather than using a call, you may find it a lot simpler to use parenthesis, like this:
...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do (
rem Transform CSV line into FTP put command
echo put "%%a" "%%b" >> %Commands%
)
rem Add commands to close ftp conn
...
maybe this works
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit /b
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
exit /b
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
exit /b