Append Command From One Batch to another - batch-file

I am trying to write a batch file that will write a new line with a "MOVE" command to a second batch file. We have a master batch file with a MOVE command for every PC that uses a piece of our software so we can back the records up to a network drive (scheduled to run daily). Data on the local PC's gets deleted after 20 days and we need to create a place to hold these files permanently. Unfortunately this is the best way to keep our data backed up, I'm just trying to automate the process to make the process as easy as I can for my department. I'm trying the command below but I think it's an issue with the quotation marks. Any help would be appreciated, thanks!
:START
ECHO.
SET /p pcid=Please enter the PCID that you would like to setup for Auto-Archiving:
IF "%pcid%"=="%%" (GOTO CONFIRMPC)
IF "%pcid%"=="exit" (GOTO END)
:CONFIRMPC
ECHO.
ECHO Please verify that "%pcid%" is correct...
ECHO.
SET /p verify=Enter y/n...
IF "%verify%"=="y" (GOTO SETUPAUTOARC)
IF "%verify%"=="n" (GOTO START)
IF "%verify%"=="%%" (GOTO VERIFYERROR)
IF "%verify%"=="exit" (GOTO END)
:VERIFYERROR
ECHO.
ECHO Please enter a valid (y/n) response...
(GOTO CONFIRMPC)
:SETUPAUTOARC
ECHO.
ECHO Creating directory...
MKDIR "\\server32\e$\Backup Data\%pcid%"
ECHO.
(HERE IS WHERE I'M RUNNING INTO TROUBLE)
ECHO "MOVE "\\%pcid%\C$\Program Files\Application\Data\*.xml" > "\\server32\c$\scripts\masterbackup.bat
ECHO.
SET /p endresp=Finished! Would you like to run another PCID? (y/n)
IF "%endresp%"=="y" (GOTO START)
IF "%endresp%"=="n" (GOTO END)
:END
exit

ECHO "MOVE "\\%pcid%\C$\Program Files\Application\Data\*.xml" > "\\server32\c$\scripts\masterbackup.bat
---- ^ Remove this quote and add an extra waaaaay up at the very end...................................^.here
The syntax is ECHO string > file
Where quotes should be balanced and need to be placed around any (full-)filename that contains spaces (etc.)
Note also that > will write the data to a NEW file, deleting the exiting (if any). Use >> to APPEND to an existing file.
Having said that, all the command would do is put or add a line
MOVE "\\%pcid%\C$\Program Files\Application\Data\*.xml"
to the file "\\server32\c$\scripts\masterbackup.bat"
That doesn't seem to be particularly rational. Shouldn't you be MOVEing the fileset to somewhere and appending that move command to the batch?

Related

Batch file to output file name from variable

I am trying to assign a file name from the variable which has been previously given by the user. How can I do it? I am a bit stuck with my code (attached below) Could you please give me some advice?
#ECHO OFF
CLS
mode con:cols=90 lines=30
SET /P name="Please enter name: " &:: Asking for name and store it as name
mkdir "C:\%name%"&ECHO Dump folder created
ECHO.
TIMEOUT /T 3 /nobreak>NUL
SET /P decision="Do you want to run all modules? Y-Yes N-No" &:: Decision to execute all modules or just one
IF %decision%==y (GoTo Full)
ELSE IF %decision%==Y (Goto Full)
ELSE IF(GoTo Basic)
:Full
>C:\%name%\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt
#ECHO OFF
[some commands]
GoTo End
:Basic
ECHO "WORKING?" &::This bit need to be completed lol
GoTo End
:End
TIMEOUT /T 5
I am a little confused as to what you are trying to do. I think that you want to create a directory and then put a file in the directory. If I wanted to create a file and put it in a folder that has the same name, I would do
set /p %name%=
mkdir C:\%name%
(
echo Works!
)>C:\%name%\%name%.txt
Hope this helps.

Batch command mixing syntax during execution

I have tried to add a variable into an already existing batch file that I have written. However, when using the new command with the variable the command gets jumbled up.
How it supposed to work.
A user is prompted for an input (one of 3 letters). The input dictates the root directory used for a software execution. This section of code pulls a list of .elf files and is supposed to write them to a .txt file in the root directory.
Issue.
When using the variable directory, the .txt file and directory are switched during execution.
Original (Works)
DIR \\somepath\%VAR%\*.elf /b /o-s>C:\MyRootFolder\unlk.txt
New command with 2nd variable (acts strangely)
DIR \\somepath\%VAR%\*.elf /b /o-s>%root%\unlk.txt
but executes like this!?
DIR \\somepath\25091562033-00\*.elf /b /o-s \unlk.txt 1>C:\MyRootFolder
I am trying to have my root folder selected by an input further up because there are 3 different versions of software that I need to use to perform an operation. Being able to have the folder selected by an input would make things a lot easier.
Any ideas of why this executes semi-backwards?
Edit:
Here is the first section of my batch file all the way down to where the error happens. I have removed some of the code that doesn't relate to the issue. Note that the excessive pauses are for troubleshooting and the section which sets the variable %ECUPN% has been removed as it is working as intended.
ECHO off
:Ask
CLS
ECHO What type of processor does this ECU have?
ECHO F = Freescale_Quasar
ECHO T = TI
ECHO M = Motorola
SET INPUT=
SET /P INPUT=[F/T/M]: %=%
IF /I "%INPUT%"=="F" SET root=C:\Freescale_Quasar && GOTO :Fetch
IF /I "%INPUT%"=="T" SET root=C:\TI_MK100 && GOTO :Fetch
IF /I "%INPUT%"=="M" SET root=C:\MK100_Motorola && GOTO :Fetch
GOTO :Ask
:Fetch
CLS
Echo on
ECHO Fetch
pause
DIR \\mofs01p2.auto.contiwan.com\didf5076\FlashDataFiles\Flashline\%ECUPN%\*.elf /b /o-s>%root%\unlk.txt
pause
SET /P Code=<%root%\unlk.txt
pause
COPY "\\mofs01p2.auto.contiwan.com\didf5076\FlashDataFiles\Flashline\%ECUPN%\%Code%" "%root%\unlock.elf" || GOTO :error_1
pause
ECHO App = %code%>>C:\Unlock\log.txt
pause
GOTO :Build_1

Batch script to Read the filenames in a directory and ask user to select one

Thanks for looking into my concern.
I have five config files in a given directory. In my batch script, I want to read those file names and prompt them to user. Once user has selected a config file, read the variables from it.
Could anyone help me with some logic here. So that, I can take it forward.
Thank you.
A batch or .cmd file like this demonstrates the menu technique (nothing fancy, the user has to enter the filename precisely). Key items:
FOR
SET /P
IF EXIST
Good luck!
#echo off
REM Show the user the list and ask them which one to use
echo.
echo Please select one of:
echo.
for %%F in ("D:\A Given Directory\*.config") do echo %%~nxF
echo.
set SEL_CFGFNM=
set /P SEL_CFGFNM=Which configuration file:
REM Make sure they answered, and that the file exists
if "%SEL_CFGFNM%" == "" goto ENDIT
if NOT EXIST "D:\A Given Directory\%SEL_CFGFNM%" goto NOCFG
REM User has selected file "D:\A Given Directory\%SEL_CFGFNM%" and it exists
REM Do whatever you want to do with that file now
REM Don't fall through the exit messages
goto ENDIT
REM Exit Messages
:NOCFG
echo.
echo ERROR: Configuration file "%SEL_CFGFNM%" is not on the list
echo.
goto ENDIT
REM Cleanup
:ENDIT
set SEL_CFGFNM=

Batch script to move to another section of the script if there is no input from user

I am trying to get this script to jump to another section of the script if there is no input from the user.
Down at the if %input%== area.
What I'm trying to do is skip to the section where the script checks for .mp4 files and moves them if they are there. Am I supposed to set a variable or loop for that section? Thanks for any replies
#echo off
echo Checking for youtube-dl updates.
pause
youtube-dl -U
rem Enter the url or urls that you want to download from
set /p input="Enter the url(s) you want to download:"
rem Uses the youtube-dl continue (-c) option to download multiple files if not in a playlist
youtube-dl -c "%input%"
rem pause
if %input%=="" GOTO:EOF
cls
echo Download complete, please wait while files are transfered to appropiate folder
pause
for %%o in (.mp4) do move "*%%o" "E:\Documents\scripts\videos\"
if not exist do echo .mp4 files are no longer in this directory
pause
How about following script? This script waits for 3 seconds while "file.mp4" doesn't exist. It keeps to wait until the file exists. About "filename", you can change for your script.
#echo off
set waittime=3
set filename=file.mp4
:loop
if not exist %filename% (
Timeout /t %waittime% /nobreak > nul
goto loop
)
echo Find %filename%
When doing string comparison in batch you have to make sure, that both parts are equal, which in your case will never happen! In most languages strings have double quotes around them. In batch they usually do not.
To solve your problem enclose %input% in double quotes as well.
Note that it can be useful to do something like "x%input%"=="x" to prevent certain characters like <>|to be at the beginning of the comparison string.
You can check this on your own with these few lines:
#echo off
set /p input="Input something or nothing here "
echo %input%
echo "%input%"
pause
If you are hitting Return without any input you will see that only the bottom one will output "" which is the string you are comparing to.

How to use parentheses in .bat files, set command and IF EXIST

Ok, I have batch file, just a simple one that hides and unhides folders.
I don't see why it cannot seem to execute accordingly;
Here is extended sample code:
#echo off
color a
title Folder/Drive hider Service
:jiki
echo Loading...
TIMEOUT /T 2 >nul
goto inputs
:inputs
echo Enabling security...
TIMEOUT /T 2 >nul
cls
goto menu
:menu
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
if EXIST "%~dp0\Logan_Documents" (set status=Folder is unlocked, to open it, enter open as your `action.)`
cls
echo.
echo STATUS: %status%
echo.
echo ----------------------------------------
echo FOLDER PROTECTOR by Logan
echo ----------------------------------------
echo.
echo Lock = Lock the folder(s)
echo Unlock = Unlock the folder(s)
echo Credits = For more info
echo V = Display your current version
echo Exit = Close the program
echo.
echo ----------------------------------------
echo For more info, just ask Logan!
echo ----------------------------------------
echo.
echo Select your action, %USERNAME%.
echo.
set /p "menu=>"
if /I %menu%== lock goto lock
if /I %menu%== unlock goto unlock
if /I %menu%== credits goto credits
if /I %menu%== v goto version
if /I %menu%== exit goto exit
goto invalid
and also a lot more, and every time I go to execute the script, it just leaves the status variable blank.
Here's what I've tried.
Reconfiguring all variables through a port, which then sorts based on if exist. doesn't work, just leaves status blank.
Using different variables. (Kinda stupid but I didn't want to think that I have all these errors because of a small typo.) Still left error blank.
Appreciate all efforts to resolve my problem and get this program working!
-Logan
if exist should work fine exactly as you use it. You don't strictly need the quotes, since the names don't include spaces. Also you don't need the parentheses since it is a single command.
But then again, it should work with them as well (I actually tested this), so the only thing I can imagine is that the files or folders are not found because the script is running in the wrong directory. After all you use just the names without any path, so the current directory should contain those files.
The 'current directory' isn't necessarily the directory in which the script is saved. If you are in 'C:\Foo' and you call 'C:\Bar\Script.bat', the current directory will still be 'C:\Foo'. The same goes for starting scripts through a shortcut.
To try this, you can use echo %CD% in your script to echo the current directory.
As a possible solution, you can use %~dp0 to use the actual directory in which the batch script is saved, so you always have a starting point to start from:
REM Check if 'Encryption' exists in the same folder as the batch file.
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
probably neither of the ifs are true, maybe because the active directory is not what you think it is. you can test this easily by inserting a set status=none above the ifs. or insert dir to see what the scrips actually sees at this point.

Resources