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.
Related
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
So, I've been working on a batch script that essentially helps you with youtube-dl, essentially filing out all the data it needs to download into a directory. I want to be able to make a special shortcut that launches it, and instead of doing what it normally does, I want it to go through a text file (for example, let's call it update list.txt) and update playlists when that shortcut is run. I don't want to make another batch file that does this (for simplicity for user).
Here's what I have so far:
#echo off
:loop
title Welcome to CCF_100's youtube-dl helper!
set /A loop=loop+1
echo.Times Looped: %loop%
cd %~dp0
set /p input=Enter YouTube ID, URL, or Playlist ID:
set /p Output_Dir=Enter Directory you want to save in (Directory will be
created if it does not exist):
set /p flags=Enter flags (Optional):
if exist %Output_Dir%\ (goto Do_the_thing) else (goto make_directory)
:make_directory
mkdir "%Output_Dir%"
if /I %loop% LEQ 2 goto Do_the_thing
explorer "%Output_Dir%"
:Do_the_thing
title CCF_100's ytdl helper: currently downloading: %input% to %Output_Dir%
youtube-dl.exe -i -U %flags% -o "%Output_Dir%\%%(title)s - %%(id)s.%%(ext)s"
%input%
set /p loop=Successfully downloaded file(s). Download more?
if /i %loop%==y goto loop
if /i %loop%==Y goto loop
if /i %loop%==n goto end
if /i %loop%==N goto end
:end
exit
And yes I know the last two if statements are unnecessary.
You can get the arguments of a batchfile by reading the value of %n with n being a number between 0 and 9 or an asterisk. 0 is the batch-file itself (in the sense of the path to it) and the asterisk means any additional argument (excluding the batch-file-path).
So with that you can check for the contents of %1 and see if it is the flag you thought of or existent at all:
REM Demo only!
#echo off
if "%1"=="" (
echo no flags set
) ELSE (
echo flag set: %1
)
or change the if in a similar fashion to react to your flag only.
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=
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.
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%
)